Propagate errors (however unlikely) from _Py_Deepfreeze_Init() (GH-31596)

This commit is contained in:
Kumar Aditya 2022-02-26 22:05:03 +05:30 committed by GitHub
parent edbee56d69
commit 0d9b565e62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 25 additions and 13 deletions

View File

@ -317,7 +317,7 @@ extern void _Py_Specialize_UnpackSequence(PyObject *seq, _Py_CODEUNIT *instr,
/* Deallocator function for static codeobjects used in deepfreeze.py */
extern void _PyStaticCode_Dealloc(PyCodeObject *co);
/* Function to intern strings of codeobjects */
extern void _PyStaticCode_InternStrings(PyCodeObject *co);
extern int _PyStaticCode_InternStrings(PyCodeObject *co);
#ifdef Py_STATS

View File

@ -65,7 +65,7 @@ extern PyStatus _Py_HashRandomization_Init(const PyConfig *);
extern PyStatus _PyImportZip_Init(PyThreadState *tstate);
extern PyStatus _PyGC_Init(PyInterpreterState *interp);
extern PyStatus _PyAtExit_Init(PyInterpreterState *interp);
extern void _Py_Deepfreeze_Init(void);
extern int _Py_Deepfreeze_Init(void);
/* Various internal finalizers */

View File

@ -1931,14 +1931,20 @@ _PyStaticCode_Dealloc(PyCodeObject *co)
}
}
void
int
_PyStaticCode_InternStrings(PyCodeObject *co)
{
int res = intern_strings(co->co_names);
assert(res == 0);
if (res < 0) {
return -1;
}
res = intern_string_constants(co->co_consts, NULL);
assert(res == 0);
if (res < 0) {
return -1;
}
res = intern_strings(co->co_localsplusnames);
assert(res == 0);
(void)res;
if (res < 0) {
return -1;
}
return 0;
}

View File

@ -15,8 +15,9 @@
/* End includes */
/* Empty initializer for deepfrozen modules */
void _Py_Deepfreeze_Init(void)
int _Py_Deepfreeze_Init(void)
{
return 0;
}
/* Empty finalizer for deepfrozen modules */
void

View File

@ -23,8 +23,9 @@
#endif
/* Empty initializer for deepfrozen modules */
void _Py_Deepfreeze_Init(void)
int _Py_Deepfreeze_Init(void)
{
return 0;
}
/* Empty finalizer for deepfrozen modules */
void

View File

@ -828,7 +828,9 @@ pycore_interp_init(PyThreadState *tstate)
}
// Intern strings in deep-frozen modules first so that others
// can use it instead of creating a heap allocated string.
_Py_Deepfreeze_Init();
if (_Py_Deepfreeze_Init() < 0) {
return _PyStatus_ERR("failed to initialize deep-frozen modules");
}
status = pycore_init_types(interp);
if (_PyStatus_EXCEPTION(status)) {

View File

@ -283,7 +283,7 @@ class Printer:
self.write(f".co_cellvars = {co_cellvars},")
self.write(f".co_freevars = {co_freevars},")
self.deallocs.append(f"_PyStaticCode_Dealloc(&{name});")
self.interns.append(f"_PyStaticCode_InternStrings(&{name});")
self.interns.append(f"_PyStaticCode_InternStrings(&{name})")
return f"& {name}.ob_base"
def generate_tuple(self, name: str, t: Tuple[object, ...]) -> str:
@ -450,9 +450,11 @@ def generate(args: list[str], output: TextIO) -> None:
with printer.block(f"void\n_Py_Deepfreeze_Fini(void)"):
for p in printer.deallocs:
printer.write(p)
with printer.block(f"void\n_Py_Deepfreeze_Init(void)"):
with printer.block(f"int\n_Py_Deepfreeze_Init(void)"):
for p in printer.interns:
printer.write(p)
with printer.block(f"if ({p} < 0)"):
printer.write("return -1;")
printer.write("return 0;")
if verbose:
print(f"Cache hits: {printer.hits}, misses: {printer.misses}")