Skip to content

Commit 86ea851

Browse files
authored
Revert "[3.6] bpo-32303 - Consistency fixes for namespace loaders (GH-5481) (#5504)" (#5911)
This reverts commit a71397f.
1 parent 1278c21 commit 86ea851

File tree

9 files changed

+1439
-1473
lines changed

9 files changed

+1439
-1473
lines changed

Doc/library/importlib.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ find and load modules.
10771077
Name of the place from which the module is loaded, e.g. "builtin" for
10781078
built-in modules and the filename for modules loaded from source.
10791079
Normally "origin" should be set, but it may be ``None`` (the default)
1080-
which indicates it is unspecified (e.g. for namespace packages).
1080+
which indicates it is unspecified.
10811081

10821082
.. attribute:: submodule_search_locations
10831083

Lib/importlib/_bootstrap.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -522,18 +522,6 @@ def _init_module_attrs(spec, module, *, override=False):
522522

523523
loader = _NamespaceLoader.__new__(_NamespaceLoader)
524524
loader._path = spec.submodule_search_locations
525-
spec.loader = loader
526-
# While the docs say that module.__file__ is not set for
527-
# built-in modules, and the code below will avoid setting it if
528-
# spec.has_location is false, this is incorrect for namespace
529-
# packages. Namespace packages have no location, but their
530-
# __spec__.origin is None, and thus their module.__file__
531-
# should also be None for consistency. While a bit of a hack,
532-
# this is the best place to ensure this consistency.
533-
#
534-
# See # https://docs.python.org/3/library/importlib.html#importlib.abc.Loader.load_module
535-
# and bpo-32305
536-
module.__file__ = None
537525
try:
538526
module.__loader__ = loader
539527
except AttributeError:

Lib/importlib/_bootstrap_external.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,9 +1160,9 @@ def find_spec(cls, fullname, path=None, target=None):
11601160
elif spec.loader is None:
11611161
namespace_path = spec.submodule_search_locations
11621162
if namespace_path:
1163-
# We found at least one namespace path. Return a spec which
1164-
# can create the namespace package.
1165-
spec.origin = None
1163+
# We found at least one namespace path. Return a
1164+
# spec which can create the namespace package.
1165+
spec.origin = 'namespace'
11661166
spec.submodule_search_locations = _NamespacePath(fullname, namespace_path, cls._get_spec)
11671167
return spec
11681168
else:

Lib/test/test_importlib/test_api.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,6 @@ def test_reload_namespace_changed(self):
307307
expected = {'__name__': name,
308308
'__package__': name,
309309
'__doc__': None,
310-
'__file__': None,
311310
}
312311
os.mkdir(name)
313312
with open(bad_path, 'w') as init_file:
@@ -319,9 +318,8 @@ def test_reload_namespace_changed(self):
319318
spec = ns.pop('__spec__')
320319
ns.pop('__builtins__', None) # An implementation detail.
321320
self.assertEqual(spec.name, name)
322-
self.assertIsNotNone(spec.loader)
323-
self.assertIsNotNone(loader)
324-
self.assertEqual(spec.loader, loader)
321+
self.assertIs(spec.loader, None)
322+
self.assertIsNot(loader, None)
325323
self.assertEqual(set(path),
326324
set([os.path.dirname(bad_path)]))
327325
with self.assertRaises(AttributeError):

Lib/test/test_importlib/test_namespace_pkgs.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -317,21 +317,5 @@ def test_dynamic_path(self):
317317
self.assertEqual(foo.two.attr, 'portion2 foo two')
318318

319319

320-
class LoaderTests(NamespacePackageTest):
321-
paths = ['portion1']
322-
323-
def test_namespace_loader_consistency(self):
324-
# bpo-32303
325-
import foo
326-
self.assertEqual(foo.__loader__, foo.__spec__.loader)
327-
self.assertIsNotNone(foo.__loader__)
328-
329-
def test_namespace_origin_consistency(self):
330-
# bpo-32305
331-
import foo
332-
self.assertIsNone(foo.__spec__.origin)
333-
self.assertIsNone(foo.__file__)
334-
335-
336320
if __name__ == "__main__":
337321
unittest.main()

Misc/NEWS.d/next/Core and Builtins/2018-02-01-10-16-28.bpo-32303.VsvhSl.rst

Lines changed: 0 additions & 1 deletion
This file was deleted.

Misc/NEWS.d/next/Core and Builtins/2018-02-01-10-56-41.bpo-32305.dkU9Qa.rst

Lines changed: 0 additions & 2 deletions
This file was deleted.

Python/importlib.h

Lines changed: 945 additions & 946 deletions
Large diffs are not rendered by default.

Python/importlib_external.h

Lines changed: 488 additions & 488 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)