| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4444291 commit 08285d5
10 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,6 +10,13 @@ extern PyStatus _PyImport_ReInitLock(void); | |||
| 10 | 10 | #endif | |
| 11 | 11 | extern PyObject* _PyImport_BootstrapImp(PyThreadState *tstate); | |
| 12 | 12 | ||
| 13 | + struct _module_alias { | ||
| 14 | + const char *name; /* ASCII encoded string */ | ||
| 15 | + const char *orig; /* ASCII encoded string */ | ||
| 16 | + }; | ||
| 17 | + | ||
| 18 | + extern const struct _module_alias * _PyImport_FrozenAliases; | ||
| 19 | + | ||
| 13 | 20 | #ifdef __cplusplus | |
| 14 | 21 | } | |
| 15 | 22 | #endif | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -824,16 +824,39 @@ def module_repr(m): | |||
| 824 | 824 | "slated for removal in Python 3.12", DeprecationWarning) | |
| 825 | 825 | return '<module {!r} ({})>'.format(m.__name__, FrozenImporter._ORIGIN) | |
| 826 | 826 | ||
| 827 | + @classmethod | ||
| 828 | + def _setup_module(cls, module): | ||
| 829 | + assert not hasattr(module, '__file__'), module.__file__ | ||
| 830 | + ispkg = hasattr(module, '__path__') | ||
| 831 | + assert not ispkg or not module.__path__, module.__path__ | ||
| 832 | + spec = module.__spec__ | ||
| 833 | + assert not ispkg or not spec.submodule_search_locations | ||
| 834 | + | ||
| 835 | + if spec.loader_state is None: | ||
| 836 | + spec.loader_state = type(sys.implementation)( | ||
| 837 | + data=None, | ||
| 838 | + origname=None, | ||
| 839 | + ) | ||
| 840 | + elif not hasattr(spec.loader_state, 'data'): | ||
| 841 | + spec.loader_state.data = None | ||
| 842 | + if not getattr(spec.loader_state, 'origname', None): | ||
| 843 | + origname = vars(module).pop('__origname__', None) | ||
| 844 | + assert origname, 'see PyImport_ImportFrozenModuleObject()' | ||
| 845 | + spec.loader_state.origname = origname | ||
| 846 | + | ||
| 827 | 847 | @classmethod | |
| 828 | 848 | def find_spec(cls, fullname, path=None, target=None): | |
| 829 | 849 | info = _call_with_frames_removed(_imp.find_frozen, fullname) | |
| 830 | 850 | if info is None: | |
| 831 | 851 | return None | |
| 832 | - data, ispkg = info | ||
| 852 | + data, ispkg, origname = info | ||
| 833 | 853 | spec = spec_from_loader(fullname, cls, | |
| 834 | 854 | origin=cls._ORIGIN, | |
| 835 | 855 | is_package=ispkg) | |
| 836 | - spec.loader_state = data | ||
| 856 | + spec.loader_state = type(sys.implementation)( | ||
| 857 | + data=data, | ||
| 858 | + origname=origname, | ||
| 859 | + ) | ||
| 837 | 860 | return spec | |
| 838 | 861 | ||
| 839 | 862 | @classmethod | |
@@ -857,7 +880,7 @@ def exec_module(module): | |||
| 857 | 880 | spec = module.__spec__ | |
| 858 | 881 | name = spec.name | |
| 859 | 882 | try: | |
| 860 | - data = spec.loader_state | ||
| 883 | + data = spec.loader_state.data | ||
| 861 | 884 | except AttributeError: | |
| 862 | 885 | if not _imp.is_frozen(name): | |
| 863 | 886 | raise ImportError('{!r} is not a frozen module'.format(name), | |
@@ -868,7 +891,7 @@ def exec_module(module): | |||
| 868 | 891 | # Note that if this method is called again (e.g. by | |
| 869 | 892 | # importlib.reload()) then _imp.get_frozen_object() will notice | |
| 870 | 893 | # no data was provided and will look it up. | |
| 871 | - spec.loader_state = None | ||
| 894 | + spec.loader_state.data = None | ||
| 872 | 895 | code = _call_with_frames_removed(_imp.get_frozen_object, name, data) | |
| 873 | 896 | exec(code, module.__dict__) | |
| 874 | 897 | ||
@@ -1220,6 +1243,8 @@ def _setup(sys_module, _imp_module): | |||
| 1220 | 1243 | continue | |
| 1221 | 1244 | spec = _spec_from_module(module, loader) | |
| 1222 | 1245 | _init_module_attrs(spec, module) | |
| 1246 | + if loader is FrozenImporter: | ||
| 1247 | + loader._setup_module(module) | ||
| 1223 | 1248 | ||
| 1224 | 1249 | # Directly load built-in modules needed during bootstrap. | |
| 1225 | 1250 | self_module = sys.modules[__name__] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,7 +9,15 @@ | |||
| 9 | 9 | import unittest | |
| 10 | 10 | import warnings | |
| 11 | 11 | ||
| 12 | - from test.support import import_helper, REPO_ROOT | ||
| 12 | + from test.support import import_helper, REPO_ROOT, STDLIB_DIR | ||
| 13 | + | ||
| 14 | + | ||
| 15 | + def resolve_stdlib_file(name, ispkg=False): | ||
| 16 | + assert name | ||
| 17 | + if ispkg: | ||
| 18 | + return os.path.join(STDLIB_DIR, *name.split('.'), '__init__.py') | ||
| 19 | + else: | ||
| 20 | + return os.path.join(STDLIB_DIR, *name.split('.')) + '.py' | ||
| 13 | 21 | ||
| 14 | 22 | ||
| 15 | 23 | class FindSpecTests(abc.FinderTests): | |
@@ -32,16 +40,30 @@ def check_basic(self, spec, name, ispkg=False): | |||
| 32 | 40 | self.assertIsNone(spec.submodule_search_locations) | |
| 33 | 41 | self.assertIsNotNone(spec.loader_state) | |
| 34 | 42 | ||
| 35 | - def check_data(self, spec): | ||
| 43 | + def check_loader_state(self, spec, origname=None, filename=None): | ||
| 44 | + if not filename: | ||
| 45 | + if not origname: | ||
| 46 | + origname = spec.name | ||
| 47 | + | ||
| 48 | + actual = dict(vars(spec.loader_state)) | ||
| 49 | + | ||
| 50 | + # Check the code object used to import the frozen module. | ||
| 51 | + # We can't compare the marshaled data directly because | ||
| 52 | + # marshal.dumps() would mark "expected" (below) as a ref, | ||
| 53 | + # which slightly changes the output. | ||
| 54 | + # (See https://bugs.python.org/issue34093.) | ||
| 55 | + data = actual.pop('data') | ||
| 36 | 56 | with import_helper.frozen_modules(): | |
| 37 | 57 | expected = _imp.get_frozen_object(spec.name) | |
| 38 | - data = spec.loader_state | ||
| 39 | - # We can't compare the marshaled data directly because | ||
| 40 | - # marshal.dumps() would mark "expected" as a ref, which slightly | ||
| 41 | - # changes the output. (See https://bugs.python.org/issue34093.) | ||
| 42 | 58 | code = marshal.loads(data) | |
| 43 | 59 | self.assertEqual(code, expected) | |
| 44 | 60 | ||
| 61 | + # Check the rest of spec.loader_state. | ||
| 62 | + expected = dict( | ||
| 63 | + origname=origname, | ||
| 64 | + ) | ||
| 65 | + self.assertDictEqual(actual, expected) | ||
| 66 | + | ||
| 45 | 67 | def check_search_locations(self, spec): | |
| 46 | 68 | # Frozen packages do not have any path entries. | |
| 47 | 69 | # (See https://bugs.python.org/issue21736.) | |
@@ -58,7 +80,7 @@ def test_module(self): | |||
| 58 | 80 | with self.subTest(f'{name} -> {name}'): | |
| 59 | 81 | spec = self.find(name) | |
| 60 | 82 | self.check_basic(spec, name) | |
| 61 | - self.check_data(spec) | ||
| 83 | + self.check_loader_state(spec) | ||
| 62 | 84 | modules = { | |
| 63 | 85 | '__hello_alias__': '__hello__', | |
| 64 | 86 | '_frozen_importlib': 'importlib._bootstrap', | |
@@ -67,46 +89,50 @@ def test_module(self): | |||
| 67 | 89 | with self.subTest(f'{name} -> {origname}'): | |
| 68 | 90 | spec = self.find(name) | |
| 69 | 91 | self.check_basic(spec, name) | |
| 70 | - self.check_data(spec) | ||
| 92 | + self.check_loader_state(spec, origname) | ||
| 71 | 93 | modules = [ | |
| 72 | 94 | '__phello__.__init__', | |
| 73 | 95 | '__phello__.ham.__init__', | |
| 74 | 96 | ] | |
| 75 | 97 | for name in modules: | |
| 76 | - origname = name.rpartition('.')[0] | ||
| 98 | + origname = '<' + name.rpartition('.')[0] | ||
| 99 | + filename = resolve_stdlib_file(name) | ||
| 77 | 100 | with self.subTest(f'{name} -> {origname}'): | |
| 78 | 101 | spec = self.find(name) | |
| 79 | 102 | self.check_basic(spec, name) | |
| 80 | - self.check_data(spec) | ||
| 103 | + self.check_loader_state(spec, origname, filename) | ||
| 81 | 104 | modules = { | |
| 82 | 105 | '__hello_only__': ('Tools', 'freeze', 'flag.py'), | |
| 83 | 106 | } | |
| 84 | 107 | for name, path in modules.items(): | |
| 108 | + origname = None | ||
| 85 | 109 | filename = os.path.join(REPO_ROOT, *path) | |
| 86 | 110 | with self.subTest(f'{name} -> {filename}'): | |
| 87 | 111 | spec = self.find(name) | |
| 88 | 112 | self.check_basic(spec, name) | |
| 89 | - self.check_data(spec) | ||
| 113 | + self.check_loader_state(spec, origname, filename) | ||
| 90 | 114 | ||
| 91 | 115 | def test_package(self): | |
| 92 | 116 | packages = [ | |
| 93 | 117 | '__phello__', | |
| 94 | 118 | '__phello__.ham', | |
| 95 | 119 | ] | |
| 96 | 120 | for name in packages: | |
| 121 | + filename = resolve_stdlib_file(name, ispkg=True) | ||
| 97 | 122 | with self.subTest(f'{name} -> {name}'): | |
| 98 | 123 | spec = self.find(name) | |
| 99 | 124 | self.check_basic(spec, name, ispkg=True) | |
| 100 | - self.check_data(spec) | ||
| 125 | + self.check_loader_state(spec, name, filename) | ||
| 101 | 126 | self.check_search_locations(spec) | |
| 102 | 127 | packages = { | |
| 103 | 128 | '__phello_alias__': '__hello__', | |
| 104 | 129 | } | |
| 105 | 130 | for name, origname in packages.items(): | |
| 131 | + filename = resolve_stdlib_file(origname, ispkg=False) | ||
| 106 | 132 | with self.subTest(f'{name} -> {origname}'): | |
| 107 | 133 | spec = self.find(name) | |
| 108 | 134 | self.check_basic(spec, name, ispkg=True) | |
| 109 | - self.check_data(spec) | ||
| 135 | + self.check_loader_state(spec, origname, filename) | ||
| 110 | 136 | self.check_search_locations(spec) | |
| 111 | 137 | ||
| 112 | 138 | # These are covered by test_module() and test_package(). | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -32,17 +32,19 @@ def fresh(name, *, oldapi=False): | |||
| 32 | 32 | ||
| 33 | 33 | class ExecModuleTests(abc.LoaderTests): | |
| 34 | 34 | ||
| 35 | - def exec_module(self, name): | ||
| 35 | + def exec_module(self, name, origname=None): | ||
| 36 | 36 | with import_helper.frozen_modules(): | |
| 37 | 37 | is_package = self.machinery.FrozenImporter.is_package(name) | |
| 38 | 38 | code = _imp.get_frozen_object(name) | |
| 39 | - data = marshal.dumps(code) | ||
| 40 | 39 | spec = self.machinery.ModuleSpec( | |
| 41 | 40 | name, | |
| 42 | 41 | self.machinery.FrozenImporter, | |
| 43 | 42 | origin='frozen', | |
| 44 | 43 | is_package=is_package, | |
| 45 | - loader_state=data, | ||
| 44 | + loader_state=types.SimpleNamespace( | ||
| 45 | + data=marshal.dumps(code), | ||
| 46 | + origname=origname or name, | ||
| 47 | + ), | ||
| 46 | 48 | ) | |
| 47 | 49 | module = types.ModuleType(name) | |
| 48 | 50 | module.__spec__ = spec | |
@@ -66,7 +68,8 @@ def test_module(self): | |||
| 66 | 68 | self.assertEqual(getattr(module, attr), value) | |
| 67 | 69 | self.assertEqual(output, 'Hello world!\n') | |
| 68 | 70 | self.assertTrue(hasattr(module, '__spec__')) | |
| 69 | - self.assertIsNone(module.__spec__.loader_state) | ||
| 71 | + self.assertIsNone(module.__spec__.loader_state.data) | ||
| 72 | + self.assertEqual(module.__spec__.loader_state.origname, name) | ||
| 70 | 73 | ||
| 71 | 74 | def test_package(self): | |
| 72 | 75 | name = '__phello__' | |
@@ -79,7 +82,8 @@ def test_package(self): | |||
| 79 | 82 | name=name, attr=attr, given=attr_value, | |
| 80 | 83 | expected=value)) | |
| 81 | 84 | self.assertEqual(output, 'Hello world!\n') | |
| 82 | - self.assertIsNone(module.__spec__.loader_state) | ||
| 85 | + self.assertIsNone(module.__spec__.loader_state.data) | ||
| 86 | + self.assertEqual(module.__spec__.loader_state.origname, name) | ||
| 83 | 87 | ||
| 84 | 88 | def test_lacking_parent(self): | |
| 85 | 89 | name = '__phello__.spam' | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,5 @@ | |||
| 1 | + For frozen stdlib modules, record the original module name as | ||
| 2 | + ``module.__spec__.loader_state.origname``. If the value is different than | ||
| 3 | + ``module.__spec__.name`` then the module was defined as an alias in | ||
| 4 | + Tools/scripts/freeze_modules.py. If it is ``None`` then the module comes | ||
| 5 | + from a source file outside the stdlib. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,6 +9,7 @@ | |||
| 9 | 9 | ||
| 10 | 10 | #include <Python.h> | |
| 11 | 11 | #include <marshal.h> | |
| 12 | + #include <pycore_import.h> | ||
| 12 | 13 | ||
| 13 | 14 | #include <stdio.h> | |
| 14 | 15 | #include <sys/types.h> | |
@@ -24,8 +25,12 @@ | |||
| 24 | 25 | static const struct _frozen _PyImport_FrozenModules[] = { | |
| 25 | 26 | {0, 0, 0} /* sentinel */ | |
| 26 | 27 | }; | |
| 28 | + static const struct _module_alias aliases[] = { | ||
| 29 | + {0, 0} /* sentinel */ | ||
| 30 | + }; | ||
| 27 | 31 | ||
| 28 | 32 | const struct _frozen *PyImport_FrozenModules; | |
| 33 | + const struct _module_alias *_PyImport_FrozenAliases; | ||
| 29 | 34 | ||
| 30 | 35 | static const char header[] = | |
| 31 | 36 | "/* Auto-generated by Programs/_freeze_module.c */"; | |
@@ -183,6 +188,7 @@ main(int argc, char *argv[]) | |||
| 183 | 188 | const char *name, *inpath, *outpath; | |
| 184 | 189 | ||
| 185 | 190 | PyImport_FrozenModules = _PyImport_FrozenModules; | |
| 191 | + _PyImport_FrozenAliases = aliases; | ||
| 186 | 192 | ||
| 187 | 193 | if (argc != 4) { | |
| 188 | 194 | fprintf(stderr, "need to specify the name, input and output paths\n"); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -36,6 +36,7 @@ | |||
| 36 | 36 | and __phello__.spam. Loading any will print some famous words... */ | |
| 37 | 37 | ||
| 38 | 38 | #include "Python.h" | |
| 39 | + #include "pycore_import.h" | ||
| 39 | 40 | ||
| 40 | 41 | /* Includes for frozen modules: */ | |
| 41 | 42 | #include "frozen_modules/importlib._bootstrap.h" | |
@@ -102,9 +103,24 @@ static const struct _frozen _PyImport_FrozenModules[] = { | |||
| 102 | 103 | {"__phello__.spam", _Py_M____phello___spam, | |
| 103 | 104 | (int)sizeof(_Py_M____phello___spam)}, | |
| 104 | 105 | {"__hello_only__", _Py_M__frozen_only, (int)sizeof(_Py_M__frozen_only)}, | |
| 105 | - {0, 0, 0} /* sentinel */ | ||
| 106 | + {0, 0, 0} /* modules sentinel */ | ||
| 106 | 107 | }; | |
| 107 | 108 | ||
| 109 | + static const struct _module_alias aliases[] = { | ||
| 110 | + {"_frozen_importlib", "importlib._bootstrap"}, | ||
| 111 | + {"_frozen_importlib_external", "importlib._bootstrap_external"}, | ||
| 112 | + {"os.path", "posixpath"}, | ||
| 113 | + {"__hello_alias__", "__hello__"}, | ||
| 114 | + {"__phello_alias__", "__hello__"}, | ||
| 115 | + {"__phello_alias__.spam", "__hello__"}, | ||
| 116 | + {"__phello__.__init__", "<__phello__"}, | ||
| 117 | + {"__phello__.ham.__init__", "<__phello__.ham"}, | ||
| 118 | + {"__hello_only__", NULL}, | ||
| 119 | + {0, 0} /* aliases sentinel */ | ||
| 120 | + }; | ||
| 121 | + const struct _module_alias *_PyImport_FrozenAliases = aliases; | ||
| 122 | + | ||
| 123 | + | ||
| 108 | 124 | /* Embedding apps may change this pointer to point to their favorite | |
| 109 | 125 | collection of frozen modules: */ | |
| 110 | 126 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments