From 7610327fe59fab8b70189462f296ae4d03b46354 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filipe=20La=C3=ADns?= Date: Sat, 23 Oct 2021 21:42:28 +0100 Subject: [PATCH 1/2] bpo-40350: fix namespace package support in modulefinder MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Filipe Laíns --- Lib/importlib/_bootstrap_external.py | 1 + Lib/modulefinder.py | 6 +++++- Lib/test/test_modulefinder.py | 15 +++++++++++++++ .../2021-10-23-22-12-13.bpo-40350.t0dQMY.rst | 1 + 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2021-10-23-22-12-13.bpo-40350.t0dQMY.rst diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py index ef4f23a4b499f1..5c910635a705f3 100644 --- a/Lib/importlib/_bootstrap_external.py +++ b/Lib/importlib/_bootstrap_external.py @@ -1455,6 +1455,7 @@ def find_spec(cls, fullname, path=None, target=None): # 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 a0a020f9eeb9b4..908e40905a5063 100644 --- a/Lib/modulefinder.py +++ b/Lib/modulefinder.py @@ -17,6 +17,7 @@ _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 ca1058b8d4087c..21a893af064b57 100644 --- a/Lib/test/test_modulefinder.py +++ b/Lib/test/test_modulefinder.py @@ -79,6 +79,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", @@ -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_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 00000000000000..a5236cc57a021d --- /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`. From 23fb4e0de38466217e932115143db1a9584e35da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filipe=20La=C3=ADns?= Date: Mon, 25 Oct 2021 23:44:47 +0100 Subject: [PATCH 2/2] consider that namespace package specs might have the loader set MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Filipe Laíns --- Lib/importlib/_bootstrap.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index afb95f4e1df869..4e3fa14037f81b 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -512,17 +512,18 @@ def _init_module_attrs(spec, module, *, override=False): loader = NamespaceLoader.__new__(NamespaceLoader) loader._path = spec.submodule_search_locations spec.loader = loader - # While the docs say that module.__file__ is not set for - # built-in modules, and the code below will avoid setting it if - # spec.has_location is false, this is incorrect for namespace - # packages. Namespace packages have no location, but their - # __spec__.origin is None, and thus their module.__file__ - # should also be None for consistency. While a bit of a hack, - # this is the best place to ensure this consistency. - # - # See # https://docs.python.org/3/library/importlib.html#importlib.abc.Loader.load_module - # and bpo-32305 - module.__file__ = None + if _bootstrap_external and isinstance(loader, _bootstrap_external.NamespaceLoader): + # While the docs say that module.__file__ is not set for + # built-in modules, and the code below will avoid setting it if + # spec.has_location is false, this is incorrect for namespace + # packages. Namespace packages have no location, but their + # __spec__.origin is None, and thus their module.__file__ + # should also be None for consistency. While a bit of a hack, + # this is the best place to ensure this consistency. + # + # See # https://docs.python.org/3/library/importlib.html#importlib.abc.Loader.load_module + # and bpo-32305 + module.__file__ = None try: module.__loader__ = loader except AttributeError: pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy