summaryrefslogtreecommitdiffstats
path: root/Lib/importlib/_bootstrap.py
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2018-02-02 20:15:58 (GMT)
committerGitHub <noreply@github.com>2018-02-02 20:15:58 (GMT)
commitbbbcf8693b876daae4469765aa62f8924f39a7d2 (patch)
tree25bcb5f1a52c5c6177cc3aaabb626fd42068fe7b /Lib/importlib/_bootstrap.py
parent383b32fe108ea627699cc9c644fba5f8bae95d73 (diff)
downloadcpython-bbbcf8693b876daae4469765aa62f8924f39a7d2.zip
cpython-bbbcf8693b876daae4469765aa62f8924f39a7d2.tar.gz
cpython-bbbcf8693b876daae4469765aa62f8924f39a7d2.tar.bz2
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
Diffstat (limited to 'Lib/importlib/_bootstrap.py')
-rw-r--r--Lib/importlib/_bootstrap.py12
1 files changed, 12 insertions, 0 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index 5df6aa0..2bdd192 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -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: