[Python-checkins] Propagate errors (however unlikely) from _Py_Deepfreeze_Init() (GH-31596)

gvanrossum webhook-mailer at python.org
Sat Feb 26 11:35:13 EST 2022


https://github.com/python/cpython/commit/0d9b565e62a5fc8c3e9b8c64cce764fe084ccb2b
commit: 0d9b565e62a5fc8c3e9b8c64cce764fe084ccb2b
branch: main
author: Kumar Aditya <59607654+kumaraditya303 at users.noreply.github.com>
committer: gvanrossum <gvanrossum at gmail.com>
date: 2022-02-26T08:35:03-08:00
summary:

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

files:
M Include/internal/pycore_code.h
M Include/internal/pycore_pylifecycle.h
M Objects/codeobject.c
M Programs/_bootstrap_python.c
M Programs/_freeze_module.c
M Python/pylifecycle.c
M Tools/scripts/deepfreeze.py

diff --git a/Include/internal/pycore_code.h b/Include/internal/pycore_code.h
index d83df5e300468..0c4850f98a318 100644
--- a/Include/internal/pycore_code.h
+++ b/Include/internal/pycore_code.h
@@ -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
 
diff --git a/Include/internal/pycore_pylifecycle.h b/Include/internal/pycore_pylifecycle.h
index 00d13b85d2c25..295505f1f3735 100644
--- a/Include/internal/pycore_pylifecycle.h
+++ b/Include/internal/pycore_pylifecycle.h
@@ -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 */
 
diff --git a/Objects/codeobject.c b/Objects/codeobject.c
index f947595803aed..5a87e6c4ff877 100644
--- a/Objects/codeobject.c
+++ b/Objects/codeobject.c
@@ -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;
 }
diff --git a/Programs/_bootstrap_python.c b/Programs/_bootstrap_python.c
index 75d455ca17983..f6b49c8c806a1 100644
--- a/Programs/_bootstrap_python.c
+++ b/Programs/_bootstrap_python.c
@@ -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
diff --git a/Programs/_freeze_module.c b/Programs/_freeze_module.c
index d5a236a0c635c..3d27b79c237c3 100644
--- a/Programs/_freeze_module.c
+++ b/Programs/_freeze_module.c
@@ -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
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index a671bcaf42c66..61534742005a3 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -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)) {
diff --git a/Tools/scripts/deepfreeze.py b/Tools/scripts/deepfreeze.py
index 8ea232fc46690..cdd34ddb8b1e8 100644
--- a/Tools/scripts/deepfreeze.py
+++ b/Tools/scripts/deepfreeze.py
@@ -283,7 +283,7 @@ def generate_code(self, name: str, code: types.CodeType) -> str:
             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}")
 



More information about the Python-checkins mailing list