Commits

Greg Slodkowicz  committed 960eba0

Replaced imp.NullImporter with a Python-based NullImporter which accepts an optional engine parameter.

  • Participants
  • Parent commits ff1514b

Comments (0)

Files changed (4)

File Lib/importlib/_bootstrap.py

 
 # Loaders #####################################################################
 
+class NullImporter:
+    def __init__(self, path):
+        if not len(path):
+            raise ImportError("empty pathname")
+        if _path_isdir(path):
+            raise ImportError("existing directory")
+    
+    def find_module(cls, fullname, engine=None):
+        return None
+
 class BuiltinImporter:
 
     """Meta path import for built-in modules.
         try:
             return super()._path_hooks(path)
         except ImportError:
-            implicit_hooks = [_DEFAULT_PATH_HOOK, imp.NullImporter]
+            implicit_hooks = [_DEFAULT_PATH_HOOK, NullImporter]
             return super()._path_hooks(path, implicit_hooks)
 
     @classmethod

File Lib/importlib/engine.py

 
 # Loaders #####################################################################
 
+class NullImporter:
+    def __init__(self, path):
+        if not len(path):
+            raise ImportError("empty pathname")
+        if _path_isdir(path):
+            raise ImportError("existing directory")
+    
+    def find_module(cls, fullname, engine=None):
+        return None
+
 class BuiltinImporter:
 
     """Meta path import for built-in modules.
             except ImportError:
                 continue
             if finder:
-                # Ugly, ugly kludge to get around the fact imp doesn't know
-                # about import engines
-                if isinstance(finder, imp.NullImporter):
-                    loader = finder.find_module(fullname)
-                else:
-                    loader = finder.find_module(fullname, engine=engine)
+                print(type(finder))
+                loader = finder.find_module(fullname, engine=engine)
                 if loader:
                     return loader
         else:
         try:
             return super()._path_hooks(path, engine=engine)
         except ImportError:
-            implicit_hooks = [_DEFAULT_PATH_HOOK, imp.NullImporter]
+            implicit_hooks = [_DEFAULT_PATH_HOOK, NullImporter]
             return super()._path_hooks(path, implicit_hooks, engine=engine)
 
     @classmethod

File Lib/importlib/test/engine/test_path.py

                 self.assertTrue(nothing is None)
                 self.assertTrue(existing_path in self.engine.path_importer_cache)
                 result = isinstance(self.engine.path_importer_cache[existing_path],
-                                    imp.NullImporter)
+                                    engine.NullImporter)
                 self.assertFalse(result)
                 nothing = engine._DefaultPathFinder.find_module(module,
                                                             path=[bad_path],
                 self.assertTrue(nothing is None)
                 self.assertTrue(bad_path in self.engine.path_importer_cache)
                 self.assertTrue(isinstance(self.engine.path_importer_cache[bad_path],
-                                           imp.NullImporter))
+                                           engine.NullImporter))
         finally:
             os.rmdir(existing_path)
 

File Lib/importlib/test/import_/test_path.py

                 self.assertTrue(nothing is None)
                 self.assertTrue(existing_path in sys.path_importer_cache)
                 result = isinstance(sys.path_importer_cache[existing_path],
-                                    imp.NullImporter)
+                                    _bootstrap.NullImporter)
                 self.assertFalse(result)
                 nothing = _bootstrap._DefaultPathFinder.find_module(module,
                                                             path=[bad_path])
                 self.assertTrue(nothing is None)
                 self.assertTrue(bad_path in sys.path_importer_cache)
                 self.assertTrue(isinstance(sys.path_importer_cache[bad_path],
-                                           imp.NullImporter))
+                                           _bootstrap.NullImporter))
         finally:
             os.rmdir(existing_path)