From 44bf57aca627bd11a08b12fe4e4b6a0e1d268862 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 12 Jan 2021 10:29:45 +0100 Subject: bpo-42882: _PyRuntimeState_Init() leaves unicode next_index unchanged (GH-24193) 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 | 4 ++++ .../next/Core and Builtins/2021-01-11-17-58-52.bpo-42882.WfTdfg.rst | 3 +++ Python/pystate.c | 5 ++++- 3 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2021-01-11-17-58-52.bpo-42882.WfTdfg.rst diff --git a/Include/internal/pycore_runtime.h b/Include/internal/pycore_runtime.h index 8c54abb..bcd710c 100644 --- a/Include/internal/pycore_runtime.h +++ b/Include/internal/pycore_runtime.h @@ -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 index 0000000..6cc7c92 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2021-01-11-17-58-52.bpo-42882.WfTdfg.rst @@ -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. diff --git a/Python/pystate.c b/Python/pystate.c index c791b23..ebf76a0 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -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(); } -- cgit v0.12