mirror of
https://github.com/python/cpython.git
synced 2025-10-06 23:21:06 +00:00
bpo-32303 - Consistency fixes for namespace loaders (#5481)
* Make sure ``__spec__.loader`` matches ``__loader__`` for namespace packages. * Make sure ``__spec__.origin` matches ``__file__`` for namespace packages. https://bugs.python.org/issue32303 https://bugs.python.org/issue32305
This commit is contained in:
parent
383b32fe10
commit
bbbcf8693b
10 changed files with 1478 additions and 1436 deletions
|
@ -522,6 +522,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
|
||||
try:
|
||||
module.__loader__ = loader
|
||||
except AttributeError:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue