Skip to content

Commit 92b2a8a

Browse files
gh-93334: Fix homonym edge case in PathFinder.find_spec() (GH-98100)
1 parent 8368895 commit 92b2a8a

File tree

5 files changed

+26
-1
lines changed

5 files changed

+26
-1
lines changed

Lib/importlib/_bootstrap_external.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,15 @@ def _find_parent_path_names(self):
11161116

11171117
def _get_parent_path(self):
11181118
parent_module_name, path_attr_name = self._find_parent_path_names()
1119-
return getattr(sys.modules[parent_module_name], path_attr_name)
1119+
try:
1120+
module = sys.modules[parent_module_name]
1121+
except KeyError as e:
1122+
raise ModuleNotFoundError(
1123+
f"{parent_module_name!r} must be imported before finding {self._name!r}.",
1124+
name=parent_module_name,
1125+
) from e
1126+
else:
1127+
return getattr(module, path_attr_name)
11201128

11211129
def _recalculate(self):
11221130
# If the parent's path has changed, recalculate _path
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This directory should not be a package, but should share a name with an
2+
unrelated subpackage.

Lib/test/test_importlib/test_namespace_pkgs.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,17 @@ def test_module_before_namespace_package(self):
318318
self.assertEqual(a_test.attr, 'in module')
319319

320320

321+
class NamespaceSubpackageSameName(NamespacePackageTest):
322+
paths = ['']
323+
324+
def test_namespace_subpackage_shares_name_with_directory(self):
325+
submodule_path = 'project4.foo'
326+
with self.assertRaises(ModuleNotFoundError) as cm:
327+
importlib.machinery.PathFinder.find_spec(submodule_path)
328+
329+
self.assertEqual(cm.exception.name, 'project4')
330+
331+
321332
class ReloadTests(NamespacePackageTest):
322333
paths = ['portion1']
323334

Makefile.pre.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2648,6 +2648,7 @@ TESTSUBDIRS= idlelib/idle_test \
26482648
test/test_importlib/namespace_pkgs \
26492649
test/test_importlib/namespace_pkgs/both_portions \
26502650
test/test_importlib/namespace_pkgs/both_portions/foo \
2651+
test/test_importlib/namespace_pkgs/foo \
26512652
test/test_importlib/namespace_pkgs/module_and_namespace_package \
26522653
test/test_importlib/namespace_pkgs/module_and_namespace_package/a_test \
26532654
test/test_importlib/namespace_pkgs/not_a_namespace_pkg \
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Reraise :exc:`KeyError` as :exc:`ModuleNotFoundError` when
2+
:meth:`importlib.machinery.PathFinder.find_spec` is called on a submodule
3+
without importing the parent (and without a ``path`` argument).

0 commit comments

Comments
 (0)