diff options
author | Barry Warsaw <barry@python.org> | 2018-02-03 04:21:14 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-03 04:21:14 (GMT) |
commit | a71397fb6603d0fe673acd7765c74699cd28fe7b (patch) | |
tree | b861446d1911d5861a98df799f209dc12893fa45 /Lib/importlib | |
parent | 7e4cf8e95d2971ae0d5fb417152183070184293f (diff) | |
download | cpython-a71397fb6603d0fe673acd7765c74699cd28fe7b.zip cpython-a71397fb6603d0fe673acd7765c74699cd28fe7b.tar.gz cpython-a71397fb6603d0fe673acd7765c74699cd28fe7b.tar.bz2 |
[3.6] bpo-32303 - Consistency fixes for namespace loaders (GH-5481) (#5504)
* 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.
(cherry picked from commit bbbcf8693b876daae4469765aa62f8924f39a7d2)
Co-authored-by: Barry Warsaw <barry@python.org>
Diffstat (limited to 'Lib/importlib')
-rw-r--r-- | Lib/importlib/_bootstrap.py | 12 | ||||
-rw-r--r-- | Lib/importlib/_bootstrap_external.py | 6 |
2 files changed, 15 insertions, 3 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index e2343dd..dfcdb99 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: diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py index 9feec50..62da085 100644 --- a/Lib/importlib/_bootstrap_external.py +++ b/Lib/importlib/_bootstrap_external.py @@ -1160,9 +1160,9 @@ class PathFinder: elif spec.loader is None: namespace_path = spec.submodule_search_locations if namespace_path: - # We found at least one namespace path. Return a - # spec which can create the namespace package. - spec.origin = 'namespace' + # 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) return spec else: |