From: Filipe Laíns Date: Sat, 23 Oct 2021 20:42:28 +0000 (+0100) Subject: bpo-40350: fix namespace package support in modulefinder X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7610327fe59fab8b70189462f296ae4d03b46354;p=thirdparty%2FPython%2Fcpython.git bpo-40350: fix namespace package support in modulefinder Signed-off-by: Filipe Laíns --- diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py index ef4f23a4b499..5c910635a705 100644 --- a/Lib/importlib/_bootstrap_external.py +++ b/Lib/importlib/_bootstrap_external.py @@ -1455,6 +1455,7 @@ class PathFinder: # can create the namespace package. spec.origin = None spec.submodule_search_locations = _NamespacePath(fullname, namespace_path, cls._get_spec) + spec.loader = NamespaceLoader(fullname, namespace_path, cls._get_spec) return spec else: return None diff --git a/Lib/modulefinder.py b/Lib/modulefinder.py index a0a020f9eeb9..908e40905a50 100644 --- a/Lib/modulefinder.py +++ b/Lib/modulefinder.py @@ -17,6 +17,7 @@ _C_EXTENSION = 3 _PKG_DIRECTORY = 5 _C_BUILTIN = 6 _PY_FROZEN = 7 +_NAMESPACE = 8 # Modulefinder does a good job at simulating Python's, but it can not # handle __path__ modifications packages make at runtime. Therefore there @@ -66,7 +67,10 @@ def _find_module(name, path=None): file_path = spec.origin - if spec.loader.is_package(name): + if isinstance(spec.loader, importlib.machinery.NamespaceLoader): + return None, spec.submodule_search_locations, ("", "", _NAMESPACE) + + 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): diff --git a/Lib/test/test_modulefinder.py b/Lib/test/test_modulefinder.py index ca1058b8d408..21a893af064b 100644 --- a/Lib/test/test_modulefinder.py +++ b/Lib/test/test_modulefinder.py @@ -79,6 +79,18 @@ a/c.py 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", @@ -352,6 +364,9 @@ class ModuleFinderTest(unittest.TestCase): 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) diff --git a/Misc/NEWS.d/next/Library/2021-10-23-22-12-13.bpo-40350.t0dQMY.rst b/Misc/NEWS.d/next/Library/2021-10-23-22-12-13.bpo-40350.t0dQMY.rst new file mode 100644 index 000000000000..a5236cc57a02 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-10-23-22-12-13.bpo-40350.t0dQMY.rst @@ -0,0 +1 @@ +Fix support for namespace packages in :mod:`modulefinder`.