]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Propagate errors (however unlikely) from _Py_Deepfreeze_Init() (GH-31596)
authorKumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Sat, 26 Feb 2022 16:35:03 +0000 (22:05 +0530)
committerGitHub <noreply@github.com>
Sat, 26 Feb 2022 16:35:03 +0000 (08:35 -0800)
Include/internal/pycore_code.h
Include/internal/pycore_pylifecycle.h
Objects/codeobject.c
Programs/_bootstrap_python.c
Programs/_freeze_module.c
Python/pylifecycle.c
Tools/scripts/deepfreeze.py

index d83df5e300468484b6091e2e316483b3fcdce7cc..0c4850f98a318f06e1f775af358ca786f3be0ff8 100644 (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
 
index 00d13b85d2c25b9884beeffbfb5a112347f95a03..295505f1f37359d0c50f669fb0349bc4c5e12aad 100644 (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 */
 
index f947595803aed569548a64f9c8c5689c042a75be..5a87e6c4ff877702d078fed32a5a255dafdf9788 100644 (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;
 }
index 75d455ca179835f83ced328044ce93b29ec0053f..f6b49c8c806a1cbfce963ed01859e85c998db808 100644 (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
index d5a236a0c635c6b88dd7c4edbf9905eee90f520b..3d27b79c237c364b8647bdf3c9611b57c8bcaaa8 100644 (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
index a671bcaf42c6601e6becfa23c0e01af25119d810..61534742005a3af80512345c783754a3865160e9 100644 (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)) {
index 8ea232fc466908c145b2d5605bb42ab8ef5b310d..cdd34ddb8b1e8573919c24c927f4647d5f5a740f 100644 (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}")