Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 69091cb

Browse files
nnjawarsaw
authored andcommitted
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.
1 parent 89427cd commit 69091cb

4 files changed

Lines changed: 673 additions & 658 deletions

File tree

Lib/importlib/_bootstrap.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -786,19 +786,21 @@ class FrozenImporter:
786786
787787
"""
788788

789+
_ORIGIN = "frozen"
790+
789791
@staticmethod
790792
def module_repr(m):
791793
"""Return repr for the module.
792794
793795
The method is deprecated. The import machinery does the job itself.
794796
795797
"""
796-
return '<module {!r} (frozen)>'.format(m.__name__)
798+
return '<module {!r} ({})>'.format(m.__name__, FrozenImporter._ORIGIN)
797799

798800
@classmethod
799801
def find_spec(cls, fullname, path=None, target=None):
800802
if _imp.is_frozen(fullname):
801-
return spec_from_loader(fullname, cls, origin='frozen')
803+
return spec_from_loader(fullname, cls, origin=cls._ORIGIN)
802804
else:
803805
return None
804806

Lib/test/test_imp.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,17 @@ class BadSpec:
332332
with self.assertRaises(TypeError):
333333
create_dynamic(BadSpec())
334334

335+
def test_issue_35321(self):
336+
# Both _frozen_importlib and _frozen_importlib_external
337+
# should have a spec origin of "frozen" and
338+
# no need to clean up imports in this case.
339+
340+
import _frozen_importlib_external
341+
self.assertEqual(_frozen_importlib_external.__spec__.origin, "frozen")
342+
343+
import _frozen_importlib
344+
self.assertEqual(_frozen_importlib.__spec__.origin, "frozen")
345+
335346
def test_source_hash(self):
336347
self.assertEqual(_imp.source_hash(42, b'hi'), b'\xc6\xe7Z\r\x03:}\xab')
337348
self.assertEqual(_imp.source_hash(43, b'hi'), b'\x85\x9765\xf8\x9a\x8b9')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Set ``__spec__.origin`` of ``_frozen_importlib`` to frozen so that it matches the behavior of ``_frozen_importlib_external``. Patch by Nina Zakharenko.

0 commit comments

Comments
 (0)