Issue #20763: Fix importlib.machinery.PathFinder to support
authorBrett Cannon <brett@python.org>
Wed, 26 Feb 2014 23:26:49 +0000 (18:26 -0500)
committerBrett Cannon <brett@python.org>
Wed, 26 Feb 2014 23:26:49 +0000 (18:26 -0500)
PathEntryFinder instances which only define find_module().

Reported by Yukihiro Nakadaira.

Lib/importlib/_bootstrap.py
Lib/test/test_importlib/import_/test_path.py
Python/importlib.h

index 4864024ae2311ebd91221961c83b659fd9252463..beaa9b3758c6f779357e6136a430abe9cca3047f 100644 (file)
@@ -1869,7 +1869,7 @@ class PathFinder:
             loader, portions = finder.find_loader(fullname)
         else:
             loader = finder.find_module(fullname)
-            portions = None
+            portions = []
         if loader is not None:
             return spec_from_loader(fullname, loader)
         spec = ModuleSpec(fullname, None)
index 713e75447a0b50ed614c2f7219f4795ed1e8d263..1274f8cb9e1208b56ed5b3d95d7c315f4fd81d92 100644 (file)
@@ -116,5 +116,29 @@ Frozen_FinderTests, Source_FinderTests = util.test_both(
         FinderTests, importlib=importlib, machinery=machinery)
 
 
+class PathEntryFinderTests:
+
+    def test_finder_with_failing_find_module(self):
+        # PathEntryFinder with find_module() defined should work.
+        # Issue #20763.
+        class Finder:
+            path_location = 'test_finder_with_find_module'
+            def __init__(self, path):
+                if path != self.path_location:
+                    raise ImportError
+
+            @staticmethod
+            def find_module(fullname):
+                return None
+
+
+        with util.import_state(path=[Finder.path_location]+sys.path[:],
+                               path_hooks=[Finder]):
+            self.machinery.PathFinder.find_spec('importlib')
+
+Frozen_PEFTests, Source_PEFTests = util.test_both(
+        PathEntryFinderTests, machinery=machinery)
+
+
 if __name__ == '__main__':
     unittest.main()
index f55c450d2c548f0f41abc43c047b01fd06db88a6..2644b1d94e3fdd76efe46f79943ab8fbc013e36d 100644 (file)
@@ -3365,7 +3365,7 @@ const unsigned char _Py_M__importlib[] = {
     116,0,0,124,2,0,100,1,0,131,2,0,114,39,0,124,
     2,0,106,1,0,124,1,0,131,1,0,92,2,0,125,3,
     0,125,4,0,110,21,0,124,2,0,106,2,0,124,1,0,
-    131,1,0,125,3,0,100,0,0,125,4,0,124,3,0,100,
+    131,1,0,125,3,0,103,0,0,125,4,0,124,3,0,100,
     0,0,107,9,0,114,85,0,116,3,0,124,1,0,124,3,
     0,131,2,0,83,116,4,0,124,1,0,100,0,0,131,2,
     0,125,5,0,124,4,0,124,5,0,95,5,0,124,5,0,