summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2023-05-02 01:36:00 (GMT)
committerGitHub <noreply@github.com>2023-05-02 01:36:00 (GMT)
commitfdd878650d325297cd801305bc2d1b0e903e42b4 (patch)
tree9814f09627ef014852dcc3fa462dfec30e5e591d /Python
parentb1ca34d4d5e463b8108eea20090f12292390f0cf (diff)
downloadcpython-fdd878650d325297cd801305bc2d1b0e903e42b4.zip
cpython-fdd878650d325297cd801305bc2d1b0e903e42b4.tar.gz
cpython-fdd878650d325297cd801305bc2d1b0e903e42b4.tar.bz2
gh-94673: Properly Initialize and Finalize Static Builtin Types for Each Interpreter (gh-104072)
Until now, we haven't been initializing nor finalizing the per-interpreter state properly.
Diffstat (limited to 'Python')
-rw-r--r--Python/errors.c11
-rw-r--r--Python/pylifecycle.c6
-rw-r--r--Python/sysmodule.c28
-rw-r--r--Python/thread.c9
4 files changed, 26 insertions, 28 deletions
diff --git a/Python/errors.c b/Python/errors.c
index ce72049..a8000ac 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -1342,8 +1342,9 @@ static PyStructSequence_Desc UnraisableHookArgs_desc = {
PyStatus
_PyErr_InitTypes(PyInterpreterState *interp)
{
- if (_PyStructSequence_InitBuiltin(&UnraisableHookArgsType,
- &UnraisableHookArgs_desc) < 0) {
+ if (_PyStructSequence_InitBuiltin(interp, &UnraisableHookArgsType,
+ &UnraisableHookArgs_desc) < 0)
+ {
return _PyStatus_ERR("failed to initialize UnraisableHookArgs type");
}
return _PyStatus_OK();
@@ -1353,11 +1354,7 @@ _PyErr_InitTypes(PyInterpreterState *interp)
void
_PyErr_FiniTypes(PyInterpreterState *interp)
{
- if (!_Py_IsMainInterpreter(interp)) {
- return;
- }
-
- _PyStructSequence_FiniBuiltin(&UnraisableHookArgsType);
+ _PyStructSequence_FiniBuiltin(interp, &UnraisableHookArgsType);
}
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index ba248d2..b8a1152 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -1663,8 +1663,10 @@ flush_std_files(void)
static void
finalize_interp_types(PyInterpreterState *interp)
{
+ _PyIO_FiniTypes(interp);
+
_PyUnicode_FiniTypes(interp);
- _PySys_Fini(interp);
+ _PySys_FiniTypes(interp);
_PyExc_Fini(interp);
_PyAsyncGen_Fini(interp);
_PyContext_Fini(interp);
@@ -1706,8 +1708,6 @@ finalize_interp_clear(PyThreadState *tstate)
/* Clear interpreter state and all thread states */
_PyInterpreterState_Clear(tstate);
- _PyIO_FiniTypes(tstate->interp);
-
/* Clear all loghooks */
/* Both _PySys_Audit function and users still need PyObject, such as tuple.
Call _PySys_ClearAuditHooks when PyObject available. */
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 81dabe6..781588b 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -3141,6 +3141,7 @@ _PySys_InitCore(PyThreadState *tstate, PyObject *sysdict)
{
PyObject *version_info;
int res;
+ PyInterpreterState *interp = tstate->interp;
/* stdin/stdout/stderr are set in pylifecycle.c */
@@ -3166,7 +3167,9 @@ _PySys_InitCore(PyThreadState *tstate, PyObject *sysdict)
SET_SYS("float_info", PyFloat_GetInfo());
SET_SYS("int_info", PyLong_GetInfo());
/* initialize hash_info */
- if (_PyStructSequence_InitBuiltin(&Hash_InfoType, &hash_info_desc) < 0) {
+ if (_PyStructSequence_InitBuiltin(interp, &Hash_InfoType,
+ &hash_info_desc) < 0)
+ {
goto type_init_failed;
}
SET_SYS("hash_info", get_hash_info(tstate));
@@ -3190,7 +3193,7 @@ _PySys_InitCore(PyThreadState *tstate, PyObject *sysdict)
#define ENSURE_INFO_TYPE(TYPE, DESC) \
do { \
if (_PyStructSequence_InitBuiltinWithFlags( \
- &TYPE, &DESC, Py_TPFLAGS_DISALLOW_INSTANTIATION) < 0) { \
+ interp, &TYPE, &DESC, Py_TPFLAGS_DISALLOW_INSTANTIATION) < 0) { \
goto type_init_failed; \
} \
} while (0)
@@ -3226,8 +3229,9 @@ _PySys_InitCore(PyThreadState *tstate, PyObject *sysdict)
SET_SYS("thread_info", PyThread_GetInfo());
/* initialize asyncgen_hooks */
- if (_PyStructSequence_InitBuiltin(
- &AsyncGenHooksType, &asyncgen_hooks_desc) < 0) {
+ if (_PyStructSequence_InitBuiltin(interp, &AsyncGenHooksType,
+ &asyncgen_hooks_desc) < 0)
+ {
goto type_init_failed;
}
@@ -3489,20 +3493,20 @@ error:
void
-_PySys_Fini(PyInterpreterState *interp)
+_PySys_FiniTypes(PyInterpreterState *interp)
{
- if (_Py_IsMainInterpreter(interp)) {
- _PyStructSequence_FiniBuiltin(&VersionInfoType);
- _PyStructSequence_FiniBuiltin(&FlagsType);
+ _PyStructSequence_FiniBuiltin(interp, &VersionInfoType);
+ _PyStructSequence_FiniBuiltin(interp, &FlagsType);
#if defined(MS_WINDOWS)
- _PyStructSequence_FiniBuiltin(&WindowsVersionType);
+ _PyStructSequence_FiniBuiltin(interp, &WindowsVersionType);
#endif
- _PyStructSequence_FiniBuiltin(&Hash_InfoType);
- _PyStructSequence_FiniBuiltin(&AsyncGenHooksType);
+ _PyStructSequence_FiniBuiltin(interp, &Hash_InfoType);
+ _PyStructSequence_FiniBuiltin(interp, &AsyncGenHooksType);
#ifdef __EMSCRIPTEN__
+ if (_Py_IsMainInterpreter(interp)) {
Py_CLEAR(EmscriptenInfoType);
-#endif
}
+#endif
}
diff --git a/Python/thread.c b/Python/thread.c
index f90cd34..7fc53f9 100644
--- a/Python/thread.c
+++ b/Python/thread.c
@@ -137,7 +137,8 @@ PyThread_GetInfo(void)
int len;
#endif
- if (_PyStructSequence_InitBuiltin(&ThreadInfoType, &threadinfo_desc) < 0) {
+ PyInterpreterState *interp = _PyInterpreterState_GET();
+ if (_PyStructSequence_InitBuiltin(interp, &ThreadInfoType, &threadinfo_desc) < 0) {
return NULL;
}
@@ -191,9 +192,5 @@ PyThread_GetInfo(void)
void
_PyThread_FiniType(PyInterpreterState *interp)
{
- if (!_Py_IsMainInterpreter(interp)) {
- return;
- }
-
- _PyStructSequence_FiniBuiltin(&ThreadInfoType);
+ _PyStructSequence_FiniBuiltin(interp, &ThreadInfoType);
}