-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
GH-119668: expose importlib.machinery.NamespacePath #119669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
3c5af73
668b8ff
02ceeb5
67cfa22
29df08f
fb73b9f
0dd0520
06b8906
cd506a4
6f441c4
137652a
ab0930f
d779a7f
be415ab
bb16ae1
9f52661
0494d7b
8ccb7ba
8bc2d6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1006,6 +1006,14 @@ find and load modules. | |
| :exc:`ImportError` is raised. | ||
|
|
||
|
|
||
| .. class:: NamespacePath(name, path, path_finder) | ||
|
|
||
| Represents a namespace package's path. It uses the module name to find its | ||
FFY00 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| parent module, and from there it looks up the parent's ``__path__``. When | ||
| this changes, the module's own path is recomputed, using path_finder. For | ||
FFY00 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| top-level modules, the parent module's path is :py:data:`sys.path`. | ||
FFY00 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The attribute is set on |
||
| .. class:: SourceFileLoader(fullname, path) | ||
|
|
||
| A concrete implementation of :class:`importlib.abc.SourceLoader` by | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1347,7 +1347,7 @@ def get_filename(self, fullname): | |
| return self.path | ||
|
|
||
|
|
||
| class _NamespacePath: | ||
| class NamespacePath: | ||
| """Represents a namespace package's path. It uses the module name | ||
| to find its parent module, and from there it looks up the parent's | ||
| __path__. When this changes, the module's own path is recomputed, | ||
|
|
@@ -1406,7 +1406,7 @@ def __len__(self): | |
| return len(self._recalculate()) | ||
|
|
||
| def __repr__(self): | ||
| return f'_NamespacePath({self._path!r})' | ||
| return f'NamespacePath({self._path!r})' | ||
|
|
||
| def __contains__(self, item): | ||
| return item in self._recalculate() | ||
|
|
@@ -1415,12 +1415,15 @@ def append(self, item): | |
| self._path.append(item) | ||
|
|
||
|
|
||
| _NamespacePath = NamespacePath # for backwards compatibility | ||
|
||
|
|
||
|
|
||
| # This class is actually exposed publicly in a namespace package's __loader__ | ||
| # attribute, so it should be available through a non-private name. | ||
| # https://github.com/python/cpython/issues/92054 | ||
| class NamespaceLoader: | ||
| def __init__(self, name, path, path_finder): | ||
| self._path = _NamespacePath(name, path, path_finder) | ||
| self._path = NamespacePath(name, path, path_finder) | ||
|
|
||
| def is_package(self, fullname): | ||
| return True | ||
|
|
@@ -1475,9 +1478,9 @@ def invalidate_caches(): | |
| del sys.path_importer_cache[name] | ||
| elif hasattr(finder, 'invalidate_caches'): | ||
| finder.invalidate_caches() | ||
| # Also invalidate the caches of _NamespacePaths | ||
| # Also invalidate the caches of NamespacePaths | ||
| # https://bugs.python.org/issue45703 | ||
| _NamespacePath._epoch += 1 | ||
| NamespacePath._epoch += 1 | ||
|
|
||
| from importlib.metadata import MetadataPathFinder | ||
| MetadataPathFinder.invalidate_caches() | ||
|
|
@@ -1563,7 +1566,7 @@ def find_spec(cls, fullname, path=None, target=None): | |
| # We found at least one namespace path. Return a spec which | ||
| # can create the namespace package. | ||
| spec.origin = None | ||
| spec.submodule_search_locations = _NamespacePath(fullname, namespace_path, cls._get_spec) | ||
| spec.submodule_search_locations = NamespacePath(fullname, namespace_path, cls._get_spec) | ||
| return spec | ||
| else: | ||
| return None | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Expose :py:class:`importlib.NamespacePath`. | ||
FFY00 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Uh oh!
There was an error while loading. Please reload this page.