Skip to content

Commit 15d4c9f

Browse files
authored
GH-108716: Turn off deep-freezing of code objects. (GH-108722)
1 parent 00cf626 commit 15d4c9f

File tree

15 files changed

+50
-86
lines changed

15 files changed

+50
-86
lines changed

Include/cpython/import.h

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ struct _frozen {
1717
const unsigned char *code;
1818
int size;
1919
int is_package;
20-
PyObject *(*get_code)(void);
2120
};
2221

2322
/* Embedding apps may change this pointer to point to their favorite

Include/internal/pycore_code.h

-2
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,6 @@ adaptive_counter_backoff(uint16_t counter) {
465465
return adaptive_counter_bits(value, backoff);
466466
}
467467

468-
extern uint32_t _Py_next_func_version;
469-
470468

471469
/* Comparison bit masks. */
472470

Include/internal/pycore_interp.h

+1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ struct _is {
186186
_PyOptimizerObject *optimizer;
187187
uint16_t optimizer_resume_threshold;
188188
uint16_t optimizer_backedge_threshold;
189+
uint32_t next_func_version;
189190

190191
_Py_GlobalMonitors monitors;
191192
bool f_opcode_trace_set;

Lib/test/test_ctypes/test_values.py

-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ class struct_frozen(Structure):
5858
("code", POINTER(c_ubyte)),
5959
("size", c_int),
6060
("is_package", c_int),
61-
("get_code", POINTER(c_ubyte)), # Function ptr
6261
]
6362
FrozenTable = POINTER(struct_frozen)
6463

Makefile.pre.in

-1
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,6 @@ LIBRARY_OBJS_OMIT_FROZEN= \
505505

506506
LIBRARY_OBJS= \
507507
$(LIBRARY_OBJS_OMIT_FROZEN) \
508-
$(DEEPFREEZE_OBJS) \
509508
Modules/getpath.o \
510509
Python/frozen.o
511510

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Turn off deep-freezing of code objects. Modules are still frozen, so that a
2+
file system search is not needed for common modules.

Objects/codeobject.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,10 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
427427
co->co_framesize = nlocalsplus + con->stacksize + FRAME_SPECIALS_SIZE;
428428
co->co_ncellvars = ncellvars;
429429
co->co_nfreevars = nfreevars;
430-
co->co_version = _Py_next_func_version;
431-
if (_Py_next_func_version != 0) {
432-
_Py_next_func_version++;
430+
PyInterpreterState *interp = _PyInterpreterState_GET();
431+
co->co_version = interp->next_func_version;
432+
if (interp->next_func_version != 0) {
433+
interp->next_func_version++;
433434
}
434435
co->_co_monitoring = NULL;
435436
co->_co_instrumentation_version = 0;

Objects/funcobject.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#include "Python.h"
55
#include "pycore_ceval.h" // _PyEval_BuiltinsFromGlobals()
6-
#include "pycore_code.h" // _Py_next_func_version
76
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
87
#include "pycore_pyerrors.h" // _PyErr_Occurred()
98

@@ -252,11 +251,9 @@ When the function version is 0, the `CALL` bytecode is not specialized.
252251
Code object versions
253252
--------------------
254253
255-
So where to code objects get their `co_version`? There is a single
256-
static global counter, `_Py_next_func_version`. This is initialized in
257-
the generated (!) file `Python/deepfreeze/deepfreeze.c`, to 1 plus the
258-
number of deep-frozen function objects in that file.
259-
(In `_bootstrap_python.c` and `freeze_module.c` it is initialized to 1.)
254+
So where to code objects get their `co_version`?
255+
There is a per-interpreter counter, `next_func_version`.
256+
This is initialized to 1 when the interpreter is created.
260257
261258
Code objects get a new `co_version` allocated from this counter upon
262259
creation. Since code objects are nominally immutable, `co_version` can

Programs/_bootstrap_python.c

-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
#include "Python/frozen_modules/zipimport.h"
1616
/* End includes */
1717

18-
uint32_t _Py_next_func_version = 1;
19-
2018
/* Empty initializer for deepfrozen modules */
2119
int _Py_Deepfreeze_Init(void)
2220
{

Programs/_freeze_module.c

-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
# include <unistd.h>
2323
#endif
2424

25-
uint32_t _Py_next_func_version = 1;
26-
2725
/* Empty initializer for deepfrozen modules */
2826
int _Py_Deepfreeze_Init(void)
2927
{

Python/frozen.c

+29-29
Original file line numberDiff line numberDiff line change
@@ -101,46 +101,46 @@ extern PyObject *_Py_get_frozen_only_toplevel(void);
101101
/* End extern declarations */
102102

103103
static const struct _frozen bootstrap_modules[] = {
104-
{"_frozen_importlib", _Py_M__importlib__bootstrap, (int)sizeof(_Py_M__importlib__bootstrap), false, GET_CODE(importlib__bootstrap)},
105-
{"_frozen_importlib_external", _Py_M__importlib__bootstrap_external, (int)sizeof(_Py_M__importlib__bootstrap_external), false, GET_CODE(importlib__bootstrap_external)},
106-
{"zipimport", _Py_M__zipimport, (int)sizeof(_Py_M__zipimport), false, GET_CODE(zipimport)},
104+
{"_frozen_importlib", _Py_M__importlib__bootstrap, (int)sizeof(_Py_M__importlib__bootstrap), false},
105+
{"_frozen_importlib_external", _Py_M__importlib__bootstrap_external, (int)sizeof(_Py_M__importlib__bootstrap_external), false},
106+
{"zipimport", _Py_M__zipimport, (int)sizeof(_Py_M__zipimport), false},
107107
{0, 0, 0} /* bootstrap sentinel */
108108
};
109109
static const struct _frozen stdlib_modules[] = {
110110
/* stdlib - startup, without site (python -S) */
111-
{"abc", _Py_M__abc, (int)sizeof(_Py_M__abc), false, GET_CODE(abc)},
112-
{"codecs", _Py_M__codecs, (int)sizeof(_Py_M__codecs), false, GET_CODE(codecs)},
113-
{"io", _Py_M__io, (int)sizeof(_Py_M__io), false, GET_CODE(io)},
111+
{"abc", _Py_M__abc, (int)sizeof(_Py_M__abc), false},
112+
{"codecs", _Py_M__codecs, (int)sizeof(_Py_M__codecs), false},
113+
{"io", _Py_M__io, (int)sizeof(_Py_M__io), false},
114114

115115
/* stdlib - startup, with site */
116-
{"_collections_abc", _Py_M___collections_abc, (int)sizeof(_Py_M___collections_abc), false, GET_CODE(_collections_abc)},
117-
{"_sitebuiltins", _Py_M___sitebuiltins, (int)sizeof(_Py_M___sitebuiltins), false, GET_CODE(_sitebuiltins)},
118-
{"genericpath", _Py_M__genericpath, (int)sizeof(_Py_M__genericpath), false, GET_CODE(genericpath)},
119-
{"ntpath", _Py_M__ntpath, (int)sizeof(_Py_M__ntpath), false, GET_CODE(ntpath)},
120-
{"posixpath", _Py_M__posixpath, (int)sizeof(_Py_M__posixpath), false, GET_CODE(posixpath)},
121-
{"os.path", _Py_M__posixpath, (int)sizeof(_Py_M__posixpath), false, GET_CODE(posixpath)},
122-
{"os", _Py_M__os, (int)sizeof(_Py_M__os), false, GET_CODE(os)},
123-
{"site", _Py_M__site, (int)sizeof(_Py_M__site), false, GET_CODE(site)},
124-
{"stat", _Py_M__stat, (int)sizeof(_Py_M__stat), false, GET_CODE(stat)},
116+
{"_collections_abc", _Py_M___collections_abc, (int)sizeof(_Py_M___collections_abc), false},
117+
{"_sitebuiltins", _Py_M___sitebuiltins, (int)sizeof(_Py_M___sitebuiltins), false},
118+
{"genericpath", _Py_M__genericpath, (int)sizeof(_Py_M__genericpath), false},
119+
{"ntpath", _Py_M__ntpath, (int)sizeof(_Py_M__ntpath), false},
120+
{"posixpath", _Py_M__posixpath, (int)sizeof(_Py_M__posixpath), false},
121+
{"os.path", _Py_M__posixpath, (int)sizeof(_Py_M__posixpath), false},
122+
{"os", _Py_M__os, (int)sizeof(_Py_M__os), false},
123+
{"site", _Py_M__site, (int)sizeof(_Py_M__site), false},
124+
{"stat", _Py_M__stat, (int)sizeof(_Py_M__stat), false},
125125

126126
/* runpy - run module with -m */
127-
{"importlib.util", _Py_M__importlib_util, (int)sizeof(_Py_M__importlib_util), false, GET_CODE(importlib_util)},
128-
{"importlib.machinery", _Py_M__importlib_machinery, (int)sizeof(_Py_M__importlib_machinery), false, GET_CODE(importlib_machinery)},
129-
{"runpy", _Py_M__runpy, (int)sizeof(_Py_M__runpy), false, GET_CODE(runpy)},
127+
{"importlib.util", _Py_M__importlib_util, (int)sizeof(_Py_M__importlib_util), false},
128+
{"importlib.machinery", _Py_M__importlib_machinery, (int)sizeof(_Py_M__importlib_machinery), false},
129+
{"runpy", _Py_M__runpy, (int)sizeof(_Py_M__runpy), false},
130130
{0, 0, 0} /* stdlib sentinel */
131131
};
132132
static const struct _frozen test_modules[] = {
133-
{"__hello__", _Py_M____hello__, (int)sizeof(_Py_M____hello__), false, GET_CODE(__hello__)},
134-
{"__hello_alias__", _Py_M____hello__, (int)sizeof(_Py_M____hello__), false, GET_CODE(__hello__)},
135-
{"__phello_alias__", _Py_M____hello__, (int)sizeof(_Py_M____hello__), true, GET_CODE(__hello__)},
136-
{"__phello_alias__.spam", _Py_M____hello__, (int)sizeof(_Py_M____hello__), false, GET_CODE(__hello__)},
137-
{"__phello__", _Py_M____phello__, (int)sizeof(_Py_M____phello__), true, GET_CODE(__phello__)},
138-
{"__phello__.__init__", _Py_M____phello__, (int)sizeof(_Py_M____phello__), false, GET_CODE(__phello__)},
139-
{"__phello__.ham", _Py_M____phello___ham, (int)sizeof(_Py_M____phello___ham), true, GET_CODE(__phello___ham)},
140-
{"__phello__.ham.__init__", _Py_M____phello___ham, (int)sizeof(_Py_M____phello___ham), false, GET_CODE(__phello___ham)},
141-
{"__phello__.ham.eggs", _Py_M____phello___ham_eggs, (int)sizeof(_Py_M____phello___ham_eggs), false, GET_CODE(__phello___ham_eggs)},
142-
{"__phello__.spam", _Py_M____phello___spam, (int)sizeof(_Py_M____phello___spam), false, GET_CODE(__phello___spam)},
143-
{"__hello_only__", _Py_M__frozen_only, (int)sizeof(_Py_M__frozen_only), false, GET_CODE(frozen_only)},
133+
{"__hello__", _Py_M____hello__, (int)sizeof(_Py_M____hello__), false},
134+
{"__hello_alias__", _Py_M____hello__, (int)sizeof(_Py_M____hello__), false},
135+
{"__phello_alias__", _Py_M____hello__, (int)sizeof(_Py_M____hello__), true},
136+
{"__phello_alias__.spam", _Py_M____hello__, (int)sizeof(_Py_M____hello__), false},
137+
{"__phello__", _Py_M____phello__, (int)sizeof(_Py_M____phello__), true},
138+
{"__phello__.__init__", _Py_M____phello__, (int)sizeof(_Py_M____phello__), false},
139+
{"__phello__.ham", _Py_M____phello___ham, (int)sizeof(_Py_M____phello___ham), true},
140+
{"__phello__.ham.__init__", _Py_M____phello___ham, (int)sizeof(_Py_M____phello___ham), false},
141+
{"__phello__.ham.eggs", _Py_M____phello___ham_eggs, (int)sizeof(_Py_M____phello___ham_eggs), false},
142+
{"__phello__.spam", _Py_M____phello___spam, (int)sizeof(_Py_M____phello___spam), false},
143+
{"__hello_only__", _Py_M__frozen_only, (int)sizeof(_Py_M__frozen_only), false},
144144
{0, 0, 0} /* test sentinel */
145145
};
146146
const struct _frozen *_PyImport_FrozenBootstrap = bootstrap_modules;

Python/import.c

+1-12
Original file line numberDiff line numberDiff line change
@@ -2006,7 +2006,6 @@ look_up_frozen(const char *name)
20062006
struct frozen_info {
20072007
PyObject *nameobj;
20082008
const char *data;
2009-
PyObject *(*get_code)(void);
20102009
Py_ssize_t size;
20112010
bool is_package;
20122011
bool is_alias;
@@ -2040,7 +2039,6 @@ find_frozen(PyObject *nameobj, struct frozen_info *info)
20402039
if (info != NULL) {
20412040
info->nameobj = nameobj; // borrowed
20422041
info->data = (const char *)p->code;
2043-
info->get_code = p->get_code;
20442042
info->size = p->size;
20452043
info->is_package = p->is_package;
20462044
if (p->size < 0) {
@@ -2052,10 +2050,6 @@ find_frozen(PyObject *nameobj, struct frozen_info *info)
20522050
info->is_alias = resolve_module_alias(name, _PyImport_FrozenAliases,
20532051
&info->origname);
20542052
}
2055-
if (p->code == NULL && p->size == 0 && p->get_code != NULL) {
2056-
/* It is only deepfrozen. */
2057-
return FROZEN_OKAY;
2058-
}
20592053
if (p->code == NULL) {
20602054
/* It is frozen but marked as un-importable. */
20612055
return FROZEN_EXCLUDED;
@@ -2070,11 +2064,6 @@ find_frozen(PyObject *nameobj, struct frozen_info *info)
20702064
static PyObject *
20712065
unmarshal_frozen_code(PyInterpreterState *interp, struct frozen_info *info)
20722066
{
2073-
if (info->get_code && _Py_IsMainInterpreter(interp)) {
2074-
PyObject *code = info->get_code();
2075-
assert(code != NULL);
2076-
return code;
2077-
}
20782067
PyObject *co = PyMarshal_ReadObjectFromString(info->data, info->size);
20792068
if (co == NULL) {
20802069
/* Does not contain executable code. */
@@ -3567,7 +3556,7 @@ _imp_get_frozen_object_impl(PyObject *module, PyObject *name,
35673556
if (info.nameobj == NULL) {
35683557
info.nameobj = name;
35693558
}
3570-
if (info.size == 0 && info.get_code == NULL) {
3559+
if (info.size == 0) {
35713560
/* Does not contain executable code. */
35723561
set_frozen_error(FROZEN_INVALID, name);
35733562
return NULL;

Python/pylifecycle.c

-6
Original file line numberDiff line numberDiff line change
@@ -832,11 +832,6 @@ pycore_interp_init(PyThreadState *tstate)
832832
if (_PyStatus_EXCEPTION(status)) {
833833
return status;
834834
}
835-
// Intern strings in deep-frozen modules first so that others
836-
// can use it instead of creating a heap allocated string.
837-
if (_Py_Deepfreeze_Init() < 0) {
838-
return _PyStatus_ERR("failed to initialize deep-frozen modules");
839-
}
840835

841836
status = pycore_init_types(interp);
842837
if (_PyStatus_EXCEPTION(status)) {
@@ -1743,7 +1738,6 @@ finalize_interp_clear(PyThreadState *tstate)
17431738
_Py_HashRandomization_Fini();
17441739
_PyArg_Fini();
17451740
_Py_ClearFileSystemEncoding();
1746-
_Py_Deepfreeze_Fini();
17471741
_PyPerfTrampoline_Fini();
17481742
}
17491743

Python/pystate.c

+1
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ init_interpreter(PyInterpreterState *interp,
697697
interp->optimizer = &_PyOptimizer_Default;
698698
interp->optimizer_backedge_threshold = _PyOptimizer_Default.backedge_threshold;
699699
interp->optimizer_resume_threshold = _PyOptimizer_Default.backedge_threshold;
700+
interp->next_func_version = 1;
700701
if (interp != &runtime->_main_interpreter) {
701702
/* Fix the self-referential, statically initialized fields. */
702703
interp->dtoa = (struct _dtoa_state)_dtoa_state_INIT(interp);

Tools/build/freeze_modules.py

+9-21
Original file line numberDiff line numberDiff line change
@@ -467,15 +467,14 @@ def replace_block(lines, start_marker, end_marker, replacements, file):
467467
return lines[:start_pos + 1] + replacements + lines[end_pos:]
468468

469469

470-
def regen_frozen(modules, frozen_modules: bool):
470+
def regen_frozen(modules):
471471
headerlines = []
472472
parentdir = os.path.dirname(FROZEN_FILE)
473-
if frozen_modules:
474-
for src in _iter_sources(modules):
475-
# Adding a comment to separate sections here doesn't add much,
476-
# so we don't.
477-
header = relpath_for_posix_display(src.frozenfile, parentdir)
478-
headerlines.append(f'#include "{header}"')
473+
for src in _iter_sources(modules):
474+
# Adding a comment to separate sections here doesn't add much,
475+
# so we don't.
476+
header = relpath_for_posix_display(src.frozenfile, parentdir)
477+
headerlines.append(f'#include "{header}"')
479478

480479
externlines = []
481480
bootstraplines = []
@@ -504,14 +503,9 @@ def regen_frozen(modules, frozen_modules: bool):
504503
get_code_name = "_Py_get_%s_toplevel" % code_name
505504
externlines.append("extern PyObject *%s(void);" % get_code_name)
506505

507-
symbol = mod.symbol
508506
pkg = 'true' if mod.ispkg else 'false'
509-
if not frozen_modules:
510-
line = ('{"%s", NULL, 0, %s, GET_CODE(%s)},'
511-
) % (mod.name, pkg, code_name)
512-
else:
513-
line = ('{"%s", %s, (int)sizeof(%s), %s, GET_CODE(%s)},'
514-
) % (mod.name, symbol, symbol, pkg, code_name)
507+
size = f"(int)sizeof({mod.symbol})"
508+
line = f'{{"{mod.name}", {mod.symbol}, {size}, {pkg}}},'
515509
lines.append(line)
516510

517511
if mod.isalias:
@@ -718,20 +712,14 @@ def regen_pcbuild(modules):
718712
#######################################
719713
# the script
720714

721-
parser = argparse.ArgumentParser()
722-
parser.add_argument("--frozen-modules", action="store_true",
723-
help="Use both frozen and deepfrozen modules. (default: uses only deepfrozen modules)")
724-
725715
def main():
726-
args = parser.parse_args()
727-
frozen_modules: bool = args.frozen_modules
728716
# Expand the raw specs, preserving order.
729717
modules = list(parse_frozen_specs())
730718

731719
# Regen build-related files.
732720
regen_makefile(modules)
733721
regen_pcbuild(modules)
734-
regen_frozen(modules, frozen_modules)
722+
regen_frozen(modules)
735723

736724

737725
if __name__ == '__main__':

0 commit comments

Comments
 (0)