Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 876fc7f

Browse files
authoredOct 20, 2021
bpo-35673: Add a public alias for namespace package __loader__ attribute (python#29049)
Rename namespace package __loader__ class to be public. Make the old name, i.e. _NamespaceLoader, an alias for the public name, for backward compatibility.
1 parent 6270d3e commit 876fc7f

File tree

7 files changed

+42
-6
lines changed

7 files changed

+42
-6
lines changed
 

‎Doc/library/importlib.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,24 @@ find and load modules.
13941394
.. versionadded:: 3.4
13951395

13961396

1397+
.. class:: NamespaceLoader(name, path, path_finder):
1398+
1399+
A concrete implementation of :class:`importlib.abc.InspectLoader` for
1400+
namespace packages. This is an alias for a private class and is only made
1401+
public for introspecting the ``__loader__`` attribute on namespace
1402+
packages::
1403+
1404+
>>> from importlib.machinery import NamespaceLoader
1405+
>>> import my_namespace
1406+
>>> isinstance(my_namespace.__loader__, NamespaceLoader)
1407+
True
1408+
>>> import importlib.abc
1409+
>>> isinstance(my_namespace.__loader__, importlib.abc.Loader)
1410+
True
1411+
1412+
.. versionadded:: 3.11
1413+
1414+
13971415
.. class:: ModuleSpec(name, loader, *, origin=None, loader_state=None, is_package=None)
13981416

13991417
A specification for a module's import-system-related state. This is

‎Lib/importlib/_bootstrap.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,9 +507,9 @@ def _init_module_attrs(spec, module, *, override=False):
507507
if spec.submodule_search_locations is not None:
508508
if _bootstrap_external is None:
509509
raise NotImplementedError
510-
_NamespaceLoader = _bootstrap_external._NamespaceLoader
510+
NamespaceLoader = _bootstrap_external.NamespaceLoader
511511

512-
loader = _NamespaceLoader.__new__(_NamespaceLoader)
512+
loader = NamespaceLoader.__new__(NamespaceLoader)
513513
loader._path = spec.submodule_search_locations
514514
spec.loader = loader
515515
# While the docs say that module.__file__ is not set for

‎Lib/importlib/_bootstrap_external.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,8 +1279,10 @@ def append(self, item):
12791279
self._path.append(item)
12801280

12811281

1282-
# We use this exclusively in module_from_spec() for backward-compatibility.
1283-
class _NamespaceLoader:
1282+
# This class is actually exposed publicly in a namespace package's __loader__
1283+
# attribute, so it should be available through a non-private name.
1284+
# https://bugs.python.org/issue35673
1285+
class NamespaceLoader:
12841286
def __init__(self, name, path, path_finder):
12851287
self._path = _NamespacePath(name, path, path_finder)
12861288

@@ -1291,7 +1293,7 @@ def module_repr(module):
12911293
The method is deprecated. The import machinery does the job itself.
12921294
12931295
"""
1294-
_warnings.warn("_NamespaceLoader.module_repr() is deprecated and "
1296+
_warnings.warn("NamespaceLoader.module_repr() is deprecated and "
12951297
"slated for removal in Python 3.12", DeprecationWarning)
12961298
return '<module {!r} (namespace)>'.format(module.__name__)
12971299

@@ -1327,6 +1329,10 @@ def get_resource_reader(self, module):
13271329
return NamespaceReader(self._path)
13281330

13291331

1332+
# We use this exclusively in module_from_spec() for backward-compatibility.
1333+
_NamespaceLoader = NamespaceLoader
1334+
1335+
13301336
# Finders #####################################################################
13311337

13321338
class PathFinder:

‎Lib/importlib/abc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def source_to_code(data, path='<string>'):
213213
exec_module = _bootstrap_external._LoaderBasics.exec_module
214214
load_module = _bootstrap_external._LoaderBasics.load_module
215215

216-
_register(InspectLoader, machinery.BuiltinImporter, machinery.FrozenImporter)
216+
_register(InspectLoader, machinery.BuiltinImporter, machinery.FrozenImporter, machinery.NamespaceLoader)
217217

218218

219219
class ExecutionLoader(InspectLoader):

‎Lib/importlib/machinery.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from ._bootstrap_external import SourceFileLoader
1313
from ._bootstrap_external import SourcelessFileLoader
1414
from ._bootstrap_external import ExtensionFileLoader
15+
from ._bootstrap_external import NamespaceLoader
1516

1617

1718
def all_suffixes():

‎Lib/test/test_importlib/test_namespace_pkgs.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import contextlib
22
import importlib
3+
import importlib.abc
4+
import importlib.machinery
35
import os
46
import sys
57
import unittest
@@ -342,6 +344,11 @@ def test_path_indexable(self):
342344
expected_path = os.path.join(self.root, 'portion1', 'foo')
343345
self.assertEqual(foo.__path__[0], expected_path)
344346

347+
def test_loader_abc(self):
348+
import foo
349+
self.assertTrue(isinstance(foo.__loader__, importlib.abc.Loader))
350+
self.assertTrue(isinstance(foo.__loader__, importlib.machinery.NamespaceLoader))
351+
345352

346353
if __name__ == "__main__":
347354
unittest.main()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Improve the introspectability of the ``__loader__`` attribute for namespace
2+
packages. :class:`importlib.machinery.NamespaceLoader` is now public, and
3+
implements the :class:`importlib.abc.InspectLoader` interface. ``_NamespaceLoader``
4+
is kept for backward compatibility.

0 commit comments

Comments
 (0)
Please sign in to comment.