]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
consider that namespace package specs might have the loader set
authorFilipe Laíns <lains@riseup.net>
Mon, 25 Oct 2021 22:44:47 +0000 (23:44 +0100)
committerFilipe Laíns <lains@riseup.net>
Mon, 25 Oct 2021 22:44:47 +0000 (23:44 +0100)
Signed-off-by: Filipe Laíns <lains@riseup.net>
Lib/importlib/_bootstrap.py

index afb95f4e1df8692164aed45a885124a4e66b9b3c..4e3fa14037f81bc3fcce6a5d52e7049932054587 100644 (file)
@@ -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: