-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
bpo-45324: Capture data in FrozenImporter.find_spec() to use in exec_module(). #28633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 17 commits
97bdaa2
e2e676d
6333212
8cd239b
7724019
0dd3870
1a0ad16
fdf7689
385d412
e0fe501
7221e0e
5f68ee8
7b85a63
cdbc611
c6e70cc
f86dcd5
d1b1868
4e105a9
5d6af1e
017c932
1a833ed
5363708
07c947f
cddf089
ca0500f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -826,10 +826,15 @@ def module_repr(m): | |
|
|
||
| @classmethod | ||
| def find_spec(cls, fullname, path=None, target=None): | ||
| if _imp.is_frozen(fullname): | ||
| return spec_from_loader(fullname, cls, origin=cls._ORIGIN) | ||
| else: | ||
| info = _call_with_frames_removed(_imp.find_frozen, fullname) | ||
| if info is None: | ||
| return None | ||
| data, ispkg = info | ||
| spec = spec_from_loader(fullname, cls, | ||
| origin=cls._ORIGIN, | ||
| is_package=ispkg) | ||
| spec.loader_state = (data,) | ||
| return spec | ||
|
|
||
| @classmethod | ||
| def find_module(cls, fullname, path=None): | ||
|
|
@@ -849,11 +854,19 @@ def create_module(spec): | |
|
|
||
| @staticmethod | ||
| def exec_module(module): | ||
| name = module.__spec__.name | ||
| if not _imp.is_frozen(name): | ||
| raise ImportError('{!r} is not a frozen module'.format(name), | ||
| name=name) | ||
| code = _call_with_frames_removed(_imp.get_frozen_object, name) | ||
| spec = module.__spec__ | ||
| name = spec.name | ||
| try: | ||
| data, = spec.loader_state | ||
| except Exception: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is extremely broad for just reading an attribute. What are you specifically guarding against? If you're worried about the attribute not being set can't you just check for
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I thought I'd changed that to AttributeError. I'll fix it. |
||
| if not _imp.is_frozen(name): | ||
| raise ImportError('{!r} is not a frozen module'.format(name), | ||
| name=name) | ||
| data = None | ||
| else: | ||
| # We clear the extra data we got from the finder, to save memory. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the loader is one-time use? What happens if you call exec_module() again (e.g.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should work fine. If the data is set then the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added to that comment to clarify. |
||
| spec.loader_state = None | ||
| code = _call_with_frames_removed(_imp.get_frozen_object, name, data) | ||
| exec(code, module.__dict__) | ||
|
|
||
| @classmethod | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,13 +1,51 @@ | ||||||
| from .. import abc | ||||||
| import os.path | ||||||
| from .. import util | ||||||
|
|
||||||
| machinery = util.import_importlib('importlib.machinery') | ||||||
|
|
||||||
| import _imp | ||||||
| import marshal | ||||||
| import os.path | ||||||
| import unittest | ||||||
| import warnings | ||||||
|
|
||||||
| from test.support import import_helper | ||||||
| from test.support import import_helper, REPO_ROOT, STDLIB_DIR | ||||||
|
|
||||||
|
|
||||||
| def get_frozen_code(name, source=None, ispkg=False, *, useimp=True): | ||||||
| """Return the code object for the given module. | ||||||
|
|
||||||
| This should match the data stored in the frozen .h file used | ||||||
| for the module. | ||||||
|
|
||||||
| "source" is the original module name or a .py filename. | ||||||
| """ | ||||||
| if useimp: | ||||||
| with import_helper.frozen_modules(): | ||||||
| return _imp.get_frozen_object(name) | ||||||
|
|
||||||
| if not source: | ||||||
| source = name | ||||||
| else: | ||||||
| ispkg = source.startswith('<') and source.endswith('>') | ||||||
| if ispkg: | ||||||
| source = source[1:-1] | ||||||
| filename = resolve_filename(source, ispkg) | ||||||
| origname = name if filename == source else source | ||||||
| with open(filename) as infile: | ||||||
| text = infile.read() | ||||||
| return compile(text, f'<frozen {origname}>', 'exec') | ||||||
|
|
||||||
|
|
||||||
| def resolve_filename(source, ispkg=False): | ||||||
| assert source | ||||||
| if source.endswith('.py'): | ||||||
| return source | ||||||
| name = source | ||||||
| if ispkg: | ||||||
| return os.path.join(STDLIB_DIR, *name.split('.'), '__init__.py') | ||||||
| else: | ||||||
| return os.path.join(STDLIB_DIR, *name.split('.')) + '.py' | ||||||
|
|
||||||
|
|
||||||
| class FindSpecTests(abc.FinderTests): | ||||||
|
|
@@ -19,39 +57,67 @@ def find(self, name, **kwargs): | |||||
| with import_helper.frozen_modules(): | ||||||
| return finder.find_spec(name, **kwargs) | ||||||
|
|
||||||
| def check(self, spec, name): | ||||||
| def check_basic(self, spec, name, ispkg=False): | ||||||
| self.assertEqual(spec.name, name) | ||||||
| self.assertIs(spec.loader, self.machinery.FrozenImporter) | ||||||
| self.assertEqual(spec.origin, 'frozen') | ||||||
| self.assertFalse(spec.has_location) | ||||||
| if ispkg: | ||||||
| self.assertIsNotNone(spec.submodule_search_locations) | ||||||
| else: | ||||||
| self.assertIsNone(spec.submodule_search_locations) | ||||||
| self.assertIsNotNone(spec.loader_state) | ||||||
|
|
||||||
| def check_search_location(self, spec, source=None): | ||||||
| # For now frozen packages do not have any path entries. | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| # (See https://bugs.python.org/issue21736.) | ||||||
| expected = [] | ||||||
| self.assertListEqual(spec.submodule_search_locations, expected) | ||||||
|
|
||||||
| def check_data(self, spec, source=None): | ||||||
| ispkg = spec.submodule_search_locations is not None | ||||||
| expected = get_frozen_code(spec.name, source, ispkg) | ||||||
| data, = spec.loader_state | ||||||
| # We can't compare the marshaled data directly because | ||||||
| # marshal.dumps() would mark "expected" as a ref, which slightly | ||||||
| # changes the output. (See https://bugs.python.org/issue34093.) | ||||||
| code = marshal.loads(data) | ||||||
| self.assertEqual(code, expected) | ||||||
|
|
||||||
| def test_module(self): | ||||||
| names = [ | ||||||
| '__hello__', | ||||||
| '__hello_alias__', | ||||||
| '__hello_only__', | ||||||
| '__phello__.__init__', | ||||||
| '__phello__.spam', | ||||||
| '__phello__.ham.__init__', | ||||||
| '__phello__.ham.eggs', | ||||||
| ] | ||||||
| for name in names: | ||||||
| modules = { | ||||||
| '__hello__': None, | ||||||
| '__phello__.__init__': '<__phello__>', | ||||||
| '__phello__.spam': None, | ||||||
| '__phello__.ham.__init__': '<__phello__.ham>', | ||||||
| '__phello__.ham.eggs': None, | ||||||
| '__hello_alias__': '__hello__', | ||||||
| } | ||||||
| for name, source in modules.items(): | ||||||
| with self.subTest(name): | ||||||
| spec = self.find(name) | ||||||
| self.check(spec, name) | ||||||
| self.assertEqual(spec.submodule_search_locations, None) | ||||||
| self.check_basic(spec, name) | ||||||
| self.check_data(spec, source) | ||||||
|
|
||||||
| def test_package(self): | ||||||
| names = [ | ||||||
| '__phello__', | ||||||
| '__phello__.ham', | ||||||
| '__phello_alias__', | ||||||
| ] | ||||||
| for name in names: | ||||||
| modules = { | ||||||
| '__phello__': None, | ||||||
| '__phello__.ham': None, | ||||||
| '__phello_alias__': '__hello__', | ||||||
| } | ||||||
| for name, source in modules.items(): | ||||||
| with self.subTest(name): | ||||||
| spec = self.find(name) | ||||||
| self.check(spec, name) | ||||||
| self.assertEqual(spec.submodule_search_locations, []) | ||||||
| self.check_basic(spec, name, ispkg=True) | ||||||
| self.check_search_location(spec, source) | ||||||
| self.check_data(spec, source) | ||||||
|
|
||||||
| def test_frozen_only(self): | ||||||
| name = '__hello_only__' | ||||||
| source = os.path.join(REPO_ROOT, 'Tools', 'freeze', 'flag.py') | ||||||
| spec = self.find(name) | ||||||
| self.check_basic(spec, name) | ||||||
| self.check_data(spec, source) | ||||||
|
|
||||||
| # These are covered by test_module() and test_package(). | ||||||
| test_module_in_package = None | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| In FrozenImporter.find_spec(), we now preserve the information needed in | ||
| exec_module() to load the module. This change mostly impacts internal | ||
| details, rather than changing the importer's behavior. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the tuple? It isn't future-proofing anything as you will still have to change any other accesses unless you index into this (which you don't; see line 860). Otherwise I dictionary could also be used if you're trying to avoid issues w/ the future needing more than one piece of data.
But honestly, I would just keep it simple for now and directly assign to the attribute.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed.