]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-93334: Fix homonym edge case in PathFinder.find_spec() (GH-98100)
authorJacob Walls <jacobtylerwalls@gmail.com>
Fri, 5 Sep 2025 22:41:01 +0000 (18:41 -0400)
committerGitHub <noreply@github.com>
Fri, 5 Sep 2025 22:41:01 +0000 (15:41 -0700)
Lib/importlib/_bootstrap_external.py
Lib/test/test_importlib/namespace_pkgs/foo/README.md [new file with mode: 0644]
Lib/test/test_importlib/test_namespace_pkgs.py
Makefile.pre.in
Misc/NEWS.d/next/Library/2022-10-08-14-56-07.gh-issue-93334.0KUm8d.rst [new file with mode: 0644]

index 8a1437a2cc5d1e8d7c32dcdc99e70ce12db23e98..9269bb77806c83587b042508dad0ed516f8b416d 100644 (file)
@@ -1116,7 +1116,15 @@ class _NamespacePath:
 
     def _get_parent_path(self):
         parent_module_name, path_attr_name = self._find_parent_path_names()
-        return getattr(sys.modules[parent_module_name], path_attr_name)
+        try:
+            module = sys.modules[parent_module_name]
+        except KeyError as e:
+            raise ModuleNotFoundError(
+                f"{parent_module_name!r} must be imported before finding {self._name!r}.",
+                name=parent_module_name,
+            ) from e
+        else:
+            return getattr(module, path_attr_name)
 
     def _recalculate(self):
         # If the parent's path has changed, recalculate _path
diff --git a/Lib/test/test_importlib/namespace_pkgs/foo/README.md b/Lib/test/test_importlib/namespace_pkgs/foo/README.md
new file mode 100644 (file)
index 0000000..9f6bc74
--- /dev/null
@@ -0,0 +1,2 @@
+This directory should not be a package, but should share a name with an
+unrelated subpackage.
index 6ca0978f9bca69a026858530ce77751aef9bd660..522ed9fa43f5f9590fb5623d682d12360f3bb476 100644 (file)
@@ -318,6 +318,17 @@ class ModuleAndNamespacePackageInSameDir(NamespacePackageTest):
         self.assertEqual(a_test.attr, 'in module')
 
 
+class NamespaceSubpackageSameName(NamespacePackageTest):
+    paths = ['']
+
+    def test_namespace_subpackage_shares_name_with_directory(self):
+        submodule_path = 'project4.foo'
+        with self.assertRaises(ModuleNotFoundError) as cm:
+            importlib.machinery.PathFinder.find_spec(submodule_path)
+
+        self.assertEqual(cm.exception.name, 'project4')
+
+
 class ReloadTests(NamespacePackageTest):
     paths = ['portion1']
 
index eb07f66f14ffc54170f50c113735e1e64e7f2d70..edc6c5aa812e2772f93c37a6d96d3d462d9d5110 100644 (file)
@@ -2648,6 +2648,7 @@ TESTSUBDIRS=      idlelib/idle_test \
                test/test_importlib/namespace_pkgs \
                test/test_importlib/namespace_pkgs/both_portions \
                test/test_importlib/namespace_pkgs/both_portions/foo \
+               test/test_importlib/namespace_pkgs/foo \
                test/test_importlib/namespace_pkgs/module_and_namespace_package \
                test/test_importlib/namespace_pkgs/module_and_namespace_package/a_test \
                test/test_importlib/namespace_pkgs/not_a_namespace_pkg \
diff --git a/Misc/NEWS.d/next/Library/2022-10-08-14-56-07.gh-issue-93334.0KUm8d.rst b/Misc/NEWS.d/next/Library/2022-10-08-14-56-07.gh-issue-93334.0KUm8d.rst
new file mode 100644 (file)
index 0000000..c3a3b7a
--- /dev/null
@@ -0,0 +1,3 @@
+Reraise :exc:`KeyError` as :exc:`ModuleNotFoundError` when
+:meth:`importlib.machinery.PathFinder.find_spec` is called on a submodule
+without importing the parent (and without a ``path`` argument).