Skip to content

gh-84530: Introduce a new type for namespace packages #19917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions Lib/modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
_PKG_DIRECTORY = 5
_C_BUILTIN = 6
_PY_FROZEN = 7
_NSP_DIRECTORY = 8

# Modulefinder does a good job at simulating Python's, but it can not
# handle __path__ modifications packages make at runtime. Therefore there
Expand Down Expand Up @@ -72,6 +73,12 @@ def _find_module(name, path=None):
if spec.loader is importlib.machinery.FrozenImporter:
return None, None, ("", "", _PY_FROZEN)

if spec.loader is None and spec.submodule_search_locations:
return (
None, os.path.dirname(str(spec.submodule_search_locations)),
("", "", _NSP_DIRECTORY)
)

file_path = spec.origin

if spec.loader.is_package(name):
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 @@ -79,6 +79,18 @@
from sys import version_info
"""]

namespace_pkg_test = [
"a.module",
["a", "a.module", "b"],
[], [],
"""\
a/__init__.py
a/module.py
from b import c
b/c.py
import sys
"""]

absolute_import_test = [
"a.module",
["a", "a.module",
Expand Down Expand Up @@ -352,6 +364,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_pkg(self):
self._do_test(namespace_pkg_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 @@
Add a new constant `_NSP_DIRECTORY` to help `modulefinder.py` distinguish namespace packages.