@@ -85,6 +85,54 @@ def load_b():
8585 self .assertEqual (b_load_count , 1 )
8686
8787
88+ class FindLoaderTests (unittest .TestCase ):
89+
90+ class FakeMetaFinder :
91+ @staticmethod
92+ def find_module (name , path = None ): return name , path
93+
94+ def test_sys_modules (self ):
95+ # If a module with __loader__ is in sys.modules, then return it.
96+ name = 'some_mod'
97+ with util .uncache (name ):
98+ module = imp .new_module (name )
99+ loader = 'a loader!'
100+ module .__loader__ = loader
101+ sys .modules [name ] = module
102+ found = importlib .find_loader (name )
103+ self .assertEqual (loader , found )
104+
105+ def test_sys_modules_loader_is_None (self ):
106+ # If sys.modules[name].__loader__ is None, raise ValueError.
107+ name = 'some_mod'
108+ with util .uncache (name ):
109+ module = imp .new_module (name )
110+ module .__loader__ = None
111+ sys .modules [name ] = module
112+ with self .assertRaises (ValueError ):
113+ importlib .find_loader (name )
114+
115+ def test_success (self ):
116+ # Return the loader found on sys.meta_path.
117+ name = 'some_mod'
118+ with util .uncache (name ):
119+ with util .import_state (meta_path = [self .FakeMetaFinder ]):
120+ self .assertEqual ((name , None ), importlib .find_loader (name ))
121+
122+ def test_success_path (self ):
123+ # Searching on a path should work.
124+ name = 'some_mod'
125+ path = 'path to some place'
126+ with util .uncache (name ):
127+ with util .import_state (meta_path = [self .FakeMetaFinder ]):
128+ self .assertEqual ((name , path ),
129+ importlib .find_loader (name , path ))
130+
131+ def test_nothing (self ):
132+ # None is returned upon failure to find a loader.
133+ self .assertIsNone (importlib .find_loader ('nevergoingtofindthismodule' ))
134+
135+
88136class InvalidateCacheTests (unittest .TestCase ):
89137
90138 def test_method_called (self ):
@@ -114,7 +162,7 @@ def test_method_lacking(self):
114162
115163def test_main ():
116164 from test .support import run_unittest
117- run_unittest (ImportModuleTests )
165+ run_unittest (ImportModuleTests , FindLoaderTests , InvalidateCacheTests )
118166
119167
120168if __name__ == '__main__' :
0 commit comments