@@ -47,12 +47,36 @@ def load_module(self, fullname):
4747 mock .load_module = MethodType (load_module , mock )
4848 return mock
4949
50- def test_using_loader_return (self ):
51- loader_return = 'hi there!'
52- with self .create_mock ('module' , return_ = loader_return ) as mock :
50+ # __import__ inconsistent between loaders and built-in import when it comes
51+ # to when to use the module in sys.modules and when not to.
52+ @import_util .importlib_only
53+ def test_using_cache_after_loader (self ):
54+ # [from cache on return]
55+ with self .create_mock ('module' ) as mock :
5356 with util .import_state (meta_path = [mock ]):
5457 module = import_util .import_ ('module' )
55- self .assertEqual (module , loader_return )
58+ self .assertEqual (id (module ), id (sys .modules ['module' ]))
59+
60+ # See test_using_cache_after_loader() for reasoning.
61+ @import_util .importlib_only
62+ def test_using_cache_for_assigning_to_attribute (self ):
63+ # [from cache to attribute]
64+ with self .create_mock ('pkg.__init__' , 'pkg.module' ) as importer :
65+ with util .import_state (meta_path = [importer ]):
66+ module = import_util .import_ ('pkg.module' )
67+ self .assertTrue (hasattr (module , 'module' ))
68+ self .assertTrue (id (module .module ), id (sys .modules ['pkg.module' ]))
69+
70+ # See test_using_cache_after_loader() for reasoning.
71+ @import_util .importlib_only
72+ def test_using_cache_for_fromlist (self ):
73+ # [from cache for fromlist]
74+ with self .create_mock ('pkg.__init__' , 'pkg.module' ) as importer :
75+ with util .import_state (meta_path = [importer ]):
76+ module = import_util .import_ ('pkg' , fromlist = ['module' ])
77+ self .assertTrue (hasattr (module , 'module' ))
78+ self .assertEqual (id (module .module ),
79+ id (sys .modules ['pkg.module' ]))
5680
5781
5882def test_main ():
0 commit comments