mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
bpo-45324: Capture data in FrozenImporter.find_spec() to use in exec_module(). (gh-28633)
Before this change we end up duplicating effort and throwing away data in FrozenImporter.find_spec(). Now we do the work once in find_spec() and the only thing we do in FrozenImporter.exec_module() is turn the raw frozen data into a code object and then exec it. We've added _imp.find_frozen(), add an arg to _imp.get_frozen_object(), and updated FrozenImporter. We've also moved some code around to reduce duplication, get a little more consistency in outcomes, and be more efficient. Note that this change is mostly necessary if we want to set __file__ on frozen stdlib modules. (See https://bugs.python.org/issue21736.) https://bugs.python.org/issue45324
This commit is contained in:
parent
b9bb74871b
commit
c3d9ac8b34
6 changed files with 331 additions and 120 deletions
|
@ -4,7 +4,9 @@ from .. import util
|
|||
machinery = util.import_importlib('importlib.machinery')
|
||||
|
||||
from test.support import captured_stdout, import_helper
|
||||
import _imp
|
||||
import contextlib
|
||||
import marshal
|
||||
import types
|
||||
import unittest
|
||||
import warnings
|
||||
|
@ -33,11 +35,14 @@ class ExecModuleTests(abc.LoaderTests):
|
|||
def exec_module(self, name):
|
||||
with import_helper.frozen_modules():
|
||||
is_package = self.machinery.FrozenImporter.is_package(name)
|
||||
code = _imp.get_frozen_object(name)
|
||||
data = marshal.dumps(code)
|
||||
spec = self.machinery.ModuleSpec(
|
||||
name,
|
||||
self.machinery.FrozenImporter,
|
||||
origin='frozen',
|
||||
is_package=is_package,
|
||||
loader_state=data,
|
||||
)
|
||||
module = types.ModuleType(name)
|
||||
module.__spec__ = spec
|
||||
|
@ -61,6 +66,7 @@ class ExecModuleTests(abc.LoaderTests):
|
|||
self.assertEqual(getattr(module, attr), value)
|
||||
self.assertEqual(output, 'Hello world!\n')
|
||||
self.assertTrue(hasattr(module, '__spec__'))
|
||||
self.assertIsNone(module.__spec__.loader_state)
|
||||
|
||||
def test_package(self):
|
||||
name = '__phello__'
|
||||
|
@ -73,6 +79,7 @@ class ExecModuleTests(abc.LoaderTests):
|
|||
name=name, attr=attr, given=attr_value,
|
||||
expected=value))
|
||||
self.assertEqual(output, 'Hello world!\n')
|
||||
self.assertIsNone(module.__spec__.loader_state)
|
||||
|
||||
def test_lacking_parent(self):
|
||||
name = '__phello__.spam'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue