bpo-35321: Set the spec origin to frozen in frozen modules (#11732)

* bpo-35321: Set the spec origin to frozen in frozen modules

This fix correctly sets the spec origin to
"frozen" for the _frozen_importlib module. Note that the
origin was already correctly set in _frozen_importlib_external.

* 📜🤖 Added by blurb_it.
This commit is contained in:
Nina Zakharenko 2019-02-04 16:56:26 -08:00 committed by Barry Warsaw
parent 89427cd0fe
commit 69091cb497
4 changed files with 673 additions and 658 deletions

View file

@ -786,6 +786,8 @@ class FrozenImporter:
"""
_ORIGIN = "frozen"
@staticmethod
def module_repr(m):
"""Return repr for the module.
@ -793,12 +795,12 @@ def module_repr(m):
The method is deprecated. The import machinery does the job itself.
"""
return '<module {!r} (frozen)>'.format(m.__name__)
return '<module {!r} ({})>'.format(m.__name__, FrozenImporter._ORIGIN)
@classmethod
def find_spec(cls, fullname, path=None, target=None):
if _imp.is_frozen(fullname):
return spec_from_loader(fullname, cls, origin='frozen')
return spec_from_loader(fullname, cls, origin=cls._ORIGIN)
else:
return None

View file

@ -332,6 +332,17 @@ class BadSpec:
with self.assertRaises(TypeError):
create_dynamic(BadSpec())
def test_issue_35321(self):
# Both _frozen_importlib and _frozen_importlib_external
# should have a spec origin of "frozen" and
# no need to clean up imports in this case.
import _frozen_importlib_external
self.assertEqual(_frozen_importlib_external.__spec__.origin, "frozen")
import _frozen_importlib
self.assertEqual(_frozen_importlib.__spec__.origin, "frozen")
def test_source_hash(self):
self.assertEqual(_imp.source_hash(42, b'hi'), b'\xc6\xe7Z\r\x03:}\xab')
self.assertEqual(_imp.source_hash(43, b'hi'), b'\x85\x9765\xf8\x9a\x8b9')

View file

@ -0,0 +1 @@
Set ``__spec__.origin`` of ``_frozen_importlib`` to frozen so that it matches the behavior of ``_frozen_importlib_external``. Patch by Nina Zakharenko.

1313
Python/importlib.h generated

File diff suppressed because it is too large Load diff