Skip to content

Commit dc152db

Browse files
Stop storing the marshaled data in spec.loader_state.
1 parent 4f5160e commit dc152db

File tree

5 files changed

+42
-52
lines changed

5 files changed

+42
-52
lines changed

Lib/importlib/_bootstrap.py

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,6 @@ def _fix_up_module(cls, module):
838838
assert _imp.is_frozen_package(module.__name__) == ispkg, ispkg
839839
filename, pkgdir = cls._resolve_filename(origname, spec.name, ispkg)
840840
spec.loader_state = state = type(sys.implementation)(
841-
data=None,
842841
filename=filename,
843842
origname=origname,
844843
)
@@ -866,8 +865,7 @@ def _fix_up_module(cls, module):
866865
# These checks ensure that _fix_up_module() is only called
867866
# in the right places.
868867
assert state is not None
869-
assert sorted(vars(state)) == ['data', 'filename', 'origname'], state
870-
assert state.data is None, state.data
868+
assert sorted(vars(state)) == ['filename', 'origname'], state
871869
assert state.origname
872870
__path__ = spec.submodule_search_locations
873871
ispkg = __path__ is not None
@@ -917,13 +915,12 @@ def find_spec(cls, fullname, path=None, target=None):
917915
info = _call_with_frames_removed(_imp.find_frozen, fullname)
918916
if info is None:
919917
return None
920-
data, ispkg, origname = info
918+
_, ispkg, origname = info
921919
spec = spec_from_loader(fullname, cls,
922920
origin=cls._ORIGIN,
923921
is_package=ispkg)
924922
filename, pkgdir = cls._resolve_filename(origname, fullname, ispkg)
925923
spec.loader_state = type(sys.implementation)(
926-
data=data,
927924
filename=filename,
928925
origname=origname,
929926
)
@@ -960,20 +957,7 @@ def create_module(spec):
960957
def exec_module(module):
961958
spec = module.__spec__
962959
name = spec.name
963-
try:
964-
data = spec.loader_state.data
965-
except AttributeError:
966-
if not _imp.is_frozen(name):
967-
raise ImportError('{!r} is not a frozen module'.format(name),
968-
name=name)
969-
data = None
970-
else:
971-
# We clear the extra data we got from the finder, to save memory.
972-
# Note that if this method is called again (e.g. by
973-
# importlib.reload()) then _imp.get_frozen_object() will notice
974-
# no data was provided and will look it up.
975-
spec.loader_state.data = None
976-
code = _call_with_frames_removed(_imp.get_frozen_object, name, data)
960+
code = _call_with_frames_removed(_imp.get_frozen_object, name)
977961
exec(code, module.__dict__)
978962

979963
@classmethod

Lib/test/test_importlib/frozen/test_finder.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,6 @@ def check_loader_state(self, spec, origname=None, filename=None):
4848

4949
actual = dict(vars(spec.loader_state))
5050

51-
# Check the code object used to import the frozen module.
52-
# We can't compare the marshaled data directly because
53-
# marshal.dumps() would mark "expected" (below) as a ref,
54-
# which slightly changes the output.
55-
# (See https://bugs.python.org/issue34093.)
56-
data = actual.pop('data')
57-
with import_helper.frozen_modules():
58-
expected = _imp.get_frozen_object(spec.name)
59-
code = marshal.loads(data)
60-
self.assertEqual(code, expected)
61-
6251
# Check the rest of spec.loader_state.
6352
expected = dict(
6453
origname=origname,

Lib/test/test_importlib/frozen/test_loader.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,12 @@ class ExecModuleTests(abc.LoaderTests):
4444
def exec_module(self, name, origname=None):
4545
with import_helper.frozen_modules():
4646
is_package = self.machinery.FrozenImporter.is_package(name)
47-
code = _imp.get_frozen_object(name)
4847
spec = self.machinery.ModuleSpec(
4948
name,
5049
self.machinery.FrozenImporter,
5150
origin='frozen',
5251
is_package=is_package,
5352
loader_state=types.SimpleNamespace(
54-
data=marshal.dumps(code),
5553
origname=origname or name,
5654
filename=resolve_stdlib_file(origname or name, is_package),
5755
),
@@ -78,7 +76,6 @@ def test_module(self):
7876
self.assertEqual(getattr(module, attr), value)
7977
self.assertEqual(output, 'Hello world!\n')
8078
self.assertTrue(hasattr(module, '__spec__'))
81-
self.assertIsNone(module.__spec__.loader_state.data)
8279
self.assertEqual(module.__spec__.loader_state.origname, name)
8380

8481
def test_package(self):
@@ -92,7 +89,6 @@ def test_package(self):
9289
name=name, attr=attr, given=attr_value,
9390
expected=value))
9491
self.assertEqual(output, 'Hello world!\n')
95-
self.assertIsNone(module.__spec__.loader_state.data)
9692
self.assertEqual(module.__spec__.loader_state.origname, name)
9793

9894
def test_lacking_parent(self):

Python/clinic/import.c.h

Lines changed: 27 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/import.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,6 +2050,8 @@ _imp.find_frozen
20502050
20512051
name: unicode
20522052
/
2053+
*
2054+
withdata: bool = False
20532055
20542056
Return info about the corresponding frozen module (if there is one) or None.
20552057
@@ -2063,8 +2065,8 @@ The returned info (a 2-tuple):
20632065
[clinic start generated code]*/
20642066

20652067
static PyObject *
2066-
_imp_find_frozen_impl(PyObject *module, PyObject *name)
2067-
/*[clinic end generated code: output=3fd17da90d417e4e input=6aa7b9078a89280a]*/
2068+
_imp_find_frozen_impl(PyObject *module, PyObject *name, int withdata)
2069+
/*[clinic end generated code: output=8c1c3c7f925397a5 input=22a8847c201542fd]*/
20682070
{
20692071
struct frozen_info info;
20702072
frozen_status status = find_frozen(name, &info);
@@ -2079,10 +2081,12 @@ _imp_find_frozen_impl(PyObject *module, PyObject *name)
20792081
return NULL;
20802082
}
20812083

2082-
PyObject *data = PyMemoryView_FromMemory((char *)info.data, info.size,
2083-
PyBUF_READ);
2084-
if (data == NULL) {
2085-
return NULL;
2084+
PyObject *data = NULL;
2085+
if (withdata) {
2086+
data = PyMemoryView_FromMemory((char *)info.data, info.size, PyBUF_READ);
2087+
if (data == NULL) {
2088+
return NULL;
2089+
}
20862090
}
20872091

20882092
PyObject *origname = NULL;
@@ -2094,11 +2098,11 @@ _imp_find_frozen_impl(PyObject *module, PyObject *name)
20942098
}
20952099
}
20962100

2097-
PyObject *result = PyTuple_Pack(3, data,
2101+
PyObject *result = PyTuple_Pack(3, data ? data : Py_None,
20982102
info.is_package ? Py_True : Py_False,
20992103
origname ? origname : Py_None);
21002104
Py_XDECREF(origname);
2101-
Py_DECREF(data);
2105+
Py_XDECREF(data);
21022106
return result;
21032107
}
21042108

0 commit comments

Comments
 (0)