Skip to content

Commit 0d9b565

Browse files
Propagate errors (however unlikely) from _Py_Deepfreeze_Init() (pythonGH-31596)
1 parent edbee56 commit 0d9b565

File tree

7 files changed

+25
-13
lines changed

7 files changed

+25
-13
lines changed

Include/internal/pycore_code.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ extern void _Py_Specialize_UnpackSequence(PyObject *seq, _Py_CODEUNIT *instr,
317317
/* Deallocator function for static codeobjects used in deepfreeze.py */
318318
extern void _PyStaticCode_Dealloc(PyCodeObject *co);
319319
/* Function to intern strings of codeobjects */
320-
extern void _PyStaticCode_InternStrings(PyCodeObject *co);
320+
extern int _PyStaticCode_InternStrings(PyCodeObject *co);
321321

322322
#ifdef Py_STATS
323323

Include/internal/pycore_pylifecycle.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ extern PyStatus _Py_HashRandomization_Init(const PyConfig *);
6565
extern PyStatus _PyImportZip_Init(PyThreadState *tstate);
6666
extern PyStatus _PyGC_Init(PyInterpreterState *interp);
6767
extern PyStatus _PyAtExit_Init(PyInterpreterState *interp);
68-
extern void _Py_Deepfreeze_Init(void);
68+
extern int _Py_Deepfreeze_Init(void);
6969

7070
/* Various internal finalizers */
7171

Objects/codeobject.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,14 +1931,20 @@ _PyStaticCode_Dealloc(PyCodeObject *co)
19311931
}
19321932
}
19331933

1934-
void
1934+
int
19351935
_PyStaticCode_InternStrings(PyCodeObject *co)
19361936
{
19371937
int res = intern_strings(co->co_names);
1938-
assert(res == 0);
1938+
if (res < 0) {
1939+
return -1;
1940+
}
19391941
res = intern_string_constants(co->co_consts, NULL);
1940-
assert(res == 0);
1942+
if (res < 0) {
1943+
return -1;
1944+
}
19411945
res = intern_strings(co->co_localsplusnames);
1942-
assert(res == 0);
1943-
(void)res;
1946+
if (res < 0) {
1947+
return -1;
1948+
}
1949+
return 0;
19441950
}

Programs/_bootstrap_python.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
/* End includes */
1616

1717
/* Empty initializer for deepfrozen modules */
18-
void _Py_Deepfreeze_Init(void)
18+
int _Py_Deepfreeze_Init(void)
1919
{
20+
return 0;
2021
}
2122
/* Empty finalizer for deepfrozen modules */
2223
void

Programs/_freeze_module.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
#endif
2424

2525
/* Empty initializer for deepfrozen modules */
26-
void _Py_Deepfreeze_Init(void)
26+
int _Py_Deepfreeze_Init(void)
2727
{
28+
return 0;
2829
}
2930
/* Empty finalizer for deepfrozen modules */
3031
void

Python/pylifecycle.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,9 @@ pycore_interp_init(PyThreadState *tstate)
828828
}
829829
// Intern strings in deep-frozen modules first so that others
830830
// can use it instead of creating a heap allocated string.
831-
_Py_Deepfreeze_Init();
831+
if (_Py_Deepfreeze_Init() < 0) {
832+
return _PyStatus_ERR("failed to initialize deep-frozen modules");
833+
}
832834

833835
status = pycore_init_types(interp);
834836
if (_PyStatus_EXCEPTION(status)) {

Tools/scripts/deepfreeze.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def generate_code(self, name: str, code: types.CodeType) -> str:
283283
self.write(f".co_cellvars = {co_cellvars},")
284284
self.write(f".co_freevars = {co_freevars},")
285285
self.deallocs.append(f"_PyStaticCode_Dealloc(&{name});")
286-
self.interns.append(f"_PyStaticCode_InternStrings(&{name});")
286+
self.interns.append(f"_PyStaticCode_InternStrings(&{name})")
287287
return f"& {name}.ob_base"
288288

289289
def generate_tuple(self, name: str, t: Tuple[object, ...]) -> str:
@@ -450,9 +450,11 @@ def generate(args: list[str], output: TextIO) -> None:
450450
with printer.block(f"void\n_Py_Deepfreeze_Fini(void)"):
451451
for p in printer.deallocs:
452452
printer.write(p)
453-
with printer.block(f"void\n_Py_Deepfreeze_Init(void)"):
453+
with printer.block(f"int\n_Py_Deepfreeze_Init(void)"):
454454
for p in printer.interns:
455-
printer.write(p)
455+
with printer.block(f"if ({p} < 0)"):
456+
printer.write("return -1;")
457+
printer.write("return 0;")
456458
if verbose:
457459
print(f"Cache hits: {printer.hits}, misses: {printer.misses}")
458460

0 commit comments

Comments
 (0)