]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-35673: Add a public alias for namespace package __loader__ attribute (#29049)
authorBarry Warsaw <barry@python.org>
Wed, 20 Oct 2021 21:05:29 +0000 (14:05 -0700)
committerGitHub <noreply@github.com>
Wed, 20 Oct 2021 21:05:29 +0000 (14:05 -0700)
Rename namespace package __loader__ class to be public.

Make the old name, i.e. _NamespaceLoader, an alias for the public name, for backward compatibility.

Doc/library/importlib.rst
Lib/importlib/_bootstrap.py
Lib/importlib/_bootstrap_external.py
Lib/importlib/abc.py
Lib/importlib/machinery.py
Lib/test/test_importlib/test_namespace_pkgs.py
Misc/NEWS.d/next/Library/2021-10-18-18-12-47.bpo-35673.KOkHWe.rst [new file with mode: 0644]

index 19230570f2d5fc088285c2ce4354d5e50273009d..a25f5145cac835cf79431ce7226f0c2541b65dc7 100644 (file)
@@ -1394,6 +1394,24 @@ find and load modules.
       .. versionadded:: 3.4
 
 
+.. class:: NamespaceLoader(name, path, path_finder):
+
+   A concrete implementation of :class:`importlib.abc.InspectLoader` for
+   namespace packages.  This is an alias for a private class and is only made
+   public for introspecting the ``__loader__`` attribute on namespace
+   packages::
+
+       >>> from importlib.machinery import NamespaceLoader
+       >>> import my_namespace
+       >>> isinstance(my_namespace.__loader__, NamespaceLoader)
+       True
+       >>> import importlib.abc
+       >>> isinstance(my_namespace.__loader__, importlib.abc.Loader)
+       True
+
+   .. versionadded:: 3.11
+
+
 .. class:: ModuleSpec(name, loader, *, origin=None, loader_state=None, is_package=None)
 
    A specification for a module's import-system-related state.  This is
index 889f08f8aeec1f1c5ac7f4c8e19523c2b009ad40..afb95f4e1df8692164aed45a885124a4e66b9b3c 100644 (file)
@@ -507,9 +507,9 @@ def _init_module_attrs(spec, module, *, override=False):
             if spec.submodule_search_locations is not None:
                 if _bootstrap_external is None:
                     raise NotImplementedError
-                _NamespaceLoader = _bootstrap_external._NamespaceLoader
+                NamespaceLoader = _bootstrap_external.NamespaceLoader
 
-                loader = _NamespaceLoader.__new__(_NamespaceLoader)
+                loader = NamespaceLoader.__new__(NamespaceLoader)
                 loader._path = spec.submodule_search_locations
                 spec.loader = loader
                 # While the docs say that module.__file__ is not set for
index c9692b542a5ccaceeb3df276d7e079558ba601b2..ef4f23a4b499f135b99ff4f82cf9423b04cb09fd 100644 (file)
@@ -1279,8 +1279,10 @@ class _NamespacePath:
         self._path.append(item)
 
 
-# We use this exclusively in module_from_spec() for backward-compatibility.
-class _NamespaceLoader:
+# This class is actually exposed publicly in a namespace package's __loader__
+# attribute, so it should be available through a non-private name.
+# https://bugs.python.org/issue35673
+class NamespaceLoader:
     def __init__(self, name, path, path_finder):
         self._path = _NamespacePath(name, path, path_finder)
 
@@ -1291,7 +1293,7 @@ class _NamespaceLoader:
         The method is deprecated.  The import machinery does the job itself.
 
         """
-        _warnings.warn("_NamespaceLoader.module_repr() is deprecated and "
+        _warnings.warn("NamespaceLoader.module_repr() is deprecated and "
                        "slated for removal in Python 3.12", DeprecationWarning)
         return '<module {!r} (namespace)>'.format(module.__name__)
 
@@ -1327,6 +1329,10 @@ class _NamespaceLoader:
         return NamespaceReader(self._path)
 
 
+# We use this exclusively in module_from_spec() for backward-compatibility.
+_NamespaceLoader = NamespaceLoader
+
+
 # Finders #####################################################################
 
 class PathFinder:
index 0b4a3f80717502c0dcea57d2aa43edd98c4a2210..1d6843b2ddd447f069d604966ff54ba0f2465c90 100644 (file)
@@ -213,7 +213,7 @@ class InspectLoader(Loader):
     exec_module = _bootstrap_external._LoaderBasics.exec_module
     load_module = _bootstrap_external._LoaderBasics.load_module
 
-_register(InspectLoader, machinery.BuiltinImporter, machinery.FrozenImporter)
+_register(InspectLoader, machinery.BuiltinImporter, machinery.FrozenImporter, machinery.NamespaceLoader)
 
 
 class ExecutionLoader(InspectLoader):
index 9a7757fb6e4494e46c30a5e7feef6da2bde768a4..d9a19a13f7b275d11cbe882398e7a8d22f45a2c6 100644 (file)
@@ -12,6 +12,7 @@ from ._bootstrap_external import FileFinder
 from ._bootstrap_external import SourceFileLoader
 from ._bootstrap_external import SourcelessFileLoader
 from ._bootstrap_external import ExtensionFileLoader
+from ._bootstrap_external import NamespaceLoader
 
 
 def all_suffixes():
index 3fe3ddc5898448c49490d4ff8a3d2b1090914db4..f80283233f9770908a7595761c8d9ae4b381949e 100644 (file)
@@ -1,5 +1,7 @@
 import contextlib
 import importlib
+import importlib.abc
+import importlib.machinery
 import os
 import sys
 import unittest
@@ -342,6 +344,11 @@ class LoaderTests(NamespacePackageTest):
         expected_path = os.path.join(self.root, 'portion1', 'foo')
         self.assertEqual(foo.__path__[0], expected_path)
 
+    def test_loader_abc(self):
+        import foo
+        self.assertTrue(isinstance(foo.__loader__, importlib.abc.Loader))
+        self.assertTrue(isinstance(foo.__loader__, importlib.machinery.NamespaceLoader))
+
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2021-10-18-18-12-47.bpo-35673.KOkHWe.rst b/Misc/NEWS.d/next/Library/2021-10-18-18-12-47.bpo-35673.KOkHWe.rst
new file mode 100644 (file)
index 0000000..e7d6a5f
--- /dev/null
@@ -0,0 +1,4 @@
+Improve the introspectability of the ``__loader__`` attribute for namespace
+packages.  :class:`importlib.machinery.NamespaceLoader` is now public, and
+implements the :class:`importlib.abc.InspectLoader` interface.  ``_NamespaceLoader``
+is kept for backward compatibility.