Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-93334: Fix homonym edge case in PathFinder.find_spec
  • Loading branch information
jacobtylerwalls committed Oct 8, 2022
commit 17ddcd0a5211099e3010f9d72b84629082a0d8e6
2 changes: 1 addition & 1 deletion Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -1347,7 +1347,7 @@ def __init__(self, name, path, path_finder):
def _find_parent_path_names(self):
"""Returns a tuple of (parent-module-name, parent-path-attr-name)"""
parent, dot, me = self._name.rpartition('.')
if dot == '':
if dot == '' or parent not in sys.modules:
# This is a top-level module. sys.path contains the parent path.
return 'sys', 'path'
# Not a top-level module. parent-module.__path__ contains the
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_importlib/namespace_pkgs/homonym/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This directory should not be a package, but should share a name with an
Comment thread
brettcannon marked this conversation as resolved.
unrelated subpackage.
1 change: 1 addition & 0 deletions Lib/test/test_importlib/namespace_pkgs/project4/homonym.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
attr = 'homonym'
Comment thread
brettcannon marked this conversation as resolved.
Outdated
11 changes: 11 additions & 0 deletions Lib/test/test_importlib/test_namespace_pkgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,17 @@ def test_module_before_namespace_package(self):
self.assertEqual(a_test.attr, 'in module')


class DirectoryHomonym(NamespacePackageTest):
paths = ['']

def test_namespace_package_submodule_homonym(self):
submodule_path = 'project4.homonym'
got = importlib.machinery.PathFinder.find_spec(submodule_path)
self.assertEqual(got.name, submodule_path)
self.assertIsNone(got.loader)
self.assertTrue(got.submodule_search_locations[0].endswith('homonym'))


class ReloadTests(NamespacePackageTest):
paths = ['portion1']

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Prevent :exc:`KeyError` thrown by :meth:`importlib.machinery.PathFinder.find_spec`
when a directory that is not a module shares a name with a namespace package
submodule, and where a `path` argument is not provided.