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
Prev Previous commit
Next Next commit
Update expected result
  • Loading branch information
jacobtylerwalls committed Feb 24, 2024
commit 8092f01e04d6ce32b7fd61c4a461d7aa84d43e73
8 changes: 6 additions & 2 deletions Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,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 == '' or parent not in sys.modules:
if dot == '':
# 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 All @@ -1367,7 +1367,11 @@ def _find_parent_path_names(self):

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:
return None
return getattr(module, path_attr_name)
Comment thread
brettcannon marked this conversation as resolved.
Outdated

def _recalculate(self):
# If the parent's path has changed, recalculate _path
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_importlib/test_namespace_pkgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,9 @@ class DirectoryHomonym(NamespacePackageTest):
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.assertEqual(got.name, 'homonym')
self.assertIsNone(got.loader)
self.assertTrue(got.submodule_search_locations[0].endswith('homonym'))
self.assertNotIn('project4', got.submodule_search_locations[0])


class ReloadTests(NamespacePackageTest):
Expand Down