diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/importlib/_bootstrap.py | 12 | ||||
-rw-r--r-- | Lib/importlib/_bootstrap_external.py | 6 | ||||
-rw-r--r-- | Lib/test/test_importlib/test_api.py | 6 | ||||
-rw-r--r-- | Lib/test/test_importlib/test_namespace_pkgs.py | 16 |
4 files changed, 35 insertions, 5 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index e2343dd430..dfcdb99463 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 9feec50842..62da085f87 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: diff --git a/Lib/test/test_importlib/test_api.py b/Lib/test/test_importlib/test_api.py index b0a94aaff5..446ed6c6fb 100644 --- a/Lib/test/test_importlib/test_api.py +++ b/Lib/test/test_importlib/test_api.py @@ -307,6 +307,7 @@ class ReloadTests: expected = {'__name__': name, '__package__': name, '__doc__': None, + '__file__': None, } os.mkdir(name) with open(bad_path, 'w') as init_file: @@ -318,8 +319,9 @@ class ReloadTests: spec = ns.pop('__spec__') ns.pop('__builtins__', None) # An implementation detail. self.assertEqual(spec.name, name) - self.assertIs(spec.loader, None) - self.assertIsNot(loader, None) + self.assertIsNotNone(spec.loader) + self.assertIsNotNone(loader) + self.assertEqual(spec.loader, loader) self.assertEqual(set(path), set([os.path.dirname(bad_path)])) with self.assertRaises(AttributeError): diff --git a/Lib/test/test_importlib/test_namespace_pkgs.py b/Lib/test/test_importlib/test_namespace_pkgs.py index e37d8a18f4..8e4b5d66fe 100644 --- a/Lib/test/test_importlib/test_namespace_pkgs.py +++ b/Lib/test/test_importlib/test_namespace_pkgs.py @@ -317,5 +317,21 @@ class ReloadTests(NamespacePackageTest): self.assertEqual(foo.two.attr, 'portion2 foo two') +class LoaderTests(NamespacePackageTest): + paths = ['portion1'] + + def test_namespace_loader_consistency(self): + # bpo-32303 + import foo + self.assertEqual(foo.__loader__, foo.__spec__.loader) + self.assertIsNotNone(foo.__loader__) + + def test_namespace_origin_consistency(self): + # bpo-32305 + import foo + self.assertIsNone(foo.__spec__.origin) + self.assertIsNone(foo.__file__) + + if __name__ == "__main__": unittest.main() |