aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Include/internal/pycore_runtime.h4
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2021-01-11-17-58-52.bpo-42882.WfTdfg.rst3
-rw-r--r--Python/pystate.c5
3 files changed, 11 insertions, 1 deletions
diff --git a/Include/internal/pycore_runtime.h b/Include/internal/pycore_runtime.h
index 8c54abbb06..bcd710c449 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 0000000000..6cc7c92194
--- /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 c791b23999..ebf76a058b 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();
}