Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 16 additions & 1 deletion Lib/modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,15 @@ def _find_module(name, path=None):

file_path = spec.origin

if spec.loader.is_package(name):
# On namespace packages, spec.loader might be None, but
# spec.submodule_search_locations should always be set — check it instead.
#
# TODO: Update the type check once GH-119669 is merged.
# if isinstance(spec.submodule_search_locations, importlib.machinery.NamespacePath):
if 'NamespacePath' in type(spec.submodule_search_locations).__name__:
return None, spec.submodule_search_locations, ("", "", _PKG_DIRECTORY)

if spec.loader.is_package(name): # non-namespace package
return None, os.path.dirname(file_path), ("", "", _PKG_DIRECTORY)

if isinstance(spec.loader, importlib.machinery.SourceFileLoader):
Expand Down Expand Up @@ -454,6 +462,13 @@ def load_package(self, fqname, pathname):
if newname:
fqname = newname
m = self.add_module(fqname)

# TODO: Update the type check once GH-119669 is merged.
# if isinstance(pathname, importlib.machinery.NamespacePath):
if 'NamespacePath' in type(pathname).__name__:
m.__path__ = pathname
return m

m.__file__ = pathname
m.__path__ = [pathname]

Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@
from sys import version_info
"""]

namespace_package_test = [
"module",
["a", "module"],
["a.c", "blahblah"], [],
"""\
module.py
import a
import a.c
import blahblah
a/b.py
"""]

absolute_import_test = [
"a.module",
["a", "a.module",
Expand Down Expand Up @@ -353,6 +365,9 @@ def _do_test(self, info, report=False, debug=0, replace_paths=[], modulefinder_c
def test_package(self):
self._do_test(package_test)

def test_namespace_package(self):
self._do_test(namespace_package_test)

def test_maybe(self):
self._do_test(maybe_test)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix support for namespace packages in :mod:`modulefinder`.
Loading