]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-42882: _PyRuntimeState_Init() leaves unicode next_index unchanged (GH-24193)
authorVictor Stinner <vstinner@python.org>
Tue, 12 Jan 2021 09:29:45 +0000 (10:29 +0100)
committerGitHub <noreply@github.com>
Tue, 12 Jan 2021 09:29:45 +0000 (10:29 +0100)
Fix the _PyUnicode_FromId() function (_Py_IDENTIFIER(var) API) when
Py_Initialize() / Py_Finalize() is called multiple times:
preserve _PyRuntime.unicode_ids.next_index value.

Use _PyRuntimeState_INIT macro instead memset(0) to reset
_PyRuntimeState members to zero.

Include/internal/pycore_runtime.h
Misc/NEWS.d/next/Core and Builtins/2021-01-11-17-58-52.bpo-42882.WfTdfg.rst [new file with mode: 0644]
Python/pystate.c

index 8c54abbb0694a3dd0a932c171408abe016ff533e..bcd710c4496bd7855f2d7ccdfc9451cf32d8f3c9 100644 (file)
@@ -51,6 +51,8 @@ typedef struct _Py_AuditHookEntry {
 
 struct _Py_unicode_runtime_ids {
     PyThread_type_lock lock;
+    // next_index value must be preserved when Py_Initialize()/Py_Finalize()
+    // is called multiple times: see _PyUnicode_FromId() implementation.
     Py_ssize_t next_index;
 };
 
@@ -107,6 +109,8 @@ typedef struct pyruntimestate {
 
     PyPreConfig preconfig;
 
+    // Audit values must be preserved when Py_Initialize()/Py_Finalize()
+    // is called multiple times.
     Py_OpenCodeHookFunction open_code_hook;
     void *open_code_userdata;
     _Py_AuditHookEntry *audit_hook_head;
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-01-11-17-58-52.bpo-42882.WfTdfg.rst b/Misc/NEWS.d/next/Core and Builtins/2021-01-11-17-58-52.bpo-42882.WfTdfg.rst
new file mode 100644 (file)
index 0000000..6cc7c92
--- /dev/null
@@ -0,0 +1,3 @@
+Fix the :c:func:`_PyUnicode_FromId` function (_Py_IDENTIFIER(var) API) when
+:c:func:`Py_Initialize` / :c:func:`Py_Finalize` is called multiple times:
+preserve ``_PyRuntime.unicode_ids.next_index`` value.
index c791b239993833bb9eb4a2152ecee64d2f56610f..ebf76a058b640220d8b36634e2c4e375a0633447 100644 (file)
@@ -54,6 +54,9 @@ _PyRuntimeState_Init_impl(_PyRuntimeState *runtime)
     void *open_code_hook = runtime->open_code_hook;
     void *open_code_userdata = runtime->open_code_userdata;
     _Py_AuditHookEntry *audit_hook_head = runtime->audit_hook_head;
+    // bpo-42882: Preserve next_index value if Py_Initialize()/Py_Finalize()
+    // is called multiple times.
+    int64_t unicode_next_index = runtime->unicode_ids.next_index;
 
     memset(runtime, 0, sizeof(*runtime));
 
@@ -90,7 +93,7 @@ _PyRuntimeState_Init_impl(_PyRuntimeState *runtime)
     if (runtime->unicode_ids.lock == NULL) {
         return _PyStatus_NO_MEMORY();
     }
-    runtime->unicode_ids.next_index = 0;
+    runtime->unicode_ids.next_index = unicode_next_index;
 
     return _PyStatus_OK();
 }