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.
This commit is contained in:
Eric Snow 2023-05-01 19:36:00 -06:00 committed by GitHub
parent b1ca34d4d5
commit fdd878650d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 146 additions and 135 deletions

View file

@ -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);
}

View file

@ -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. */

View file

@ -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
}

View file

@ -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);
}