8000 gh-94673: Properly Initialize and Finalize Static Builtin Types for Each Interpreter by ericsnowcurrently · Pull Request #104072 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-94673: Properly Initialize and Finalize Static Builtin Types for Each Interpreter #104072

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Pass interp to _PyStructSequence_InitBuiltin() and _PyStructSequence_…
…FiniBuiltin().
  • Loading branch information
ericsnowcurrently committed May 1, 2023
commit b3132936f341775e59a557eecc0206332653eeba
10 changes: 7 additions & 3 deletions Include/internal/pycore_structseq.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@ PyAPI_FUNC(PyTypeObject *) _PyStructSequence_NewType(
unsigned long tp_flags);

extern int _PyStructSequence_InitBuiltinWithFlags(
PyInterpreterState *interp,
PyTypeObject *type,
PyStructSequence_Desc *desc,
unsigned long tp_flags);

static inline int
_PyStructSequence_InitBuiltin(PyTypeObject *type,
_PyStructSequence_InitBuiltin(PyInterpreterState *interp,
PyTypeObject *type,
PyStructSequence_Desc *desc)
{
return _PyStructSequence_InitBuiltinWithFlags(type, desc, 0);
return _PyStructSequence_InitBuiltinWithFlags(interp, type, desc, 0);
}

extern void _PyStructSequence_FiniBuiltin(PyTypeObject *type);
extern void _PyStructSequence_FiniBuiltin(
PyInterpreterState *interp,
PyTypeObject *type);

#ifdef __cplusplus
}
Expand Down
10 changes: 6 additions & 4 deletions Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1991,8 +1991,9 @@ PyStatus
_PyFloat_InitTypes(PyInterpreterState *interp)
{
/* Init float info */
if (_PyStructSequence_InitBuiltin(&FloatInfoType,
&floatinfo_desc) < 0) {
if (_PyStructSequence_InitBuiltin(interp, &FloatInfoType,
&floatinfo_desc) < 0)
{
return _PyStatus_ERR("can't init float info type");
}

Expand Down Expand Up @@ -2028,9 +2029,10 @@ _PyFloat_Fini(PyInterpreterState *interp)
void
_PyFloat_FiniType(PyInterpreterState *interp)
{
if (_Py_IsMainInterpreter(interp)) {
_PyStructSequence_FiniBuiltin(&FloatInfoType);
if (!_Py_IsMainInterpreter(interp)) {
return;
}
_PyStructSequence_FiniBuiltin(interp, &FloatInfoType);
}

/* Print summary info about the state of the optimized allocator */
Expand Down
6 changes: 4 additions & 2 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -6352,7 +6352,9 @@ PyStatus
_PyLong_InitTypes(PyInterpreterState *interp)
{
/* initialize int_info */
if (_PyStructSequence_InitBuiltin(&Int_InfoType, &int_info_desc) < 0) {
if (_PyStructSequence_InitBuiltin(interp, &Int_InfoType,
&int_info_desc) < 0)
{
return _PyStatus_ERR("can't init int info type");
}

Expand All @@ -6367,5 +6369,5 @@ _PyLong_FiniTypes(PyInterpreterState *interp)
return;
}

_PyStructSequence_FiniBuiltin(&Int_InfoType);
_PyStructSequence_FiniBuiltin(interp, &Int_InfoType);
}
5 changes: 3 additions & 2 deletions Objects/structseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,8 @@ initialize_static_type(PyTypeObject *type, PyStructSequence_Desc *desc,
}

int
_PyStructSequence_InitBuiltinWithFlags(PyTypeObject *type,
_PyStructSequence_InitBuiltinWithFlags(PyInterpreterState *interp,
PyTypeObject *type,
PyStructSequence_Desc *desc,
unsigned long tp_flags)
{
Expand Down Expand Up @@ -606,7 +607,7 @@ PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc)
initialized via _PyStructSequence_InitBuiltinWithFlags(). */

void
_PyStructSequence_FiniBuiltin(PyTypeObject *type)
_PyStructSequence_FiniBuiltin(PyInterpreterState *interp, PyTypeObject *type)
{
// Ensure that the type is initialized
assert(type->tp_name != NULL);
Expand Down
7 changes: 4 additions & 3 deletions Python/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -1357,7 +1358,7 @@ _PyErr_FiniTypes(PyInterpreterState *interp)
return;
}

_PyStructSequence_FiniBuiltin(&UnraisableHookArgsType);
_PyStructSequence_FiniBuiltin(interp, &UnraisableHookArgsType);
}


Expand Down
30 changes: 18 additions & 12 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand All @@ -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));
Expand All @@ -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)
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -3491,18 +3495,20 @@ _PySys_Create(PyThreadState *tstate, PyObject **sysmod_p)
void
_PySys_Fini(PyInterpreterState *interp)
{
if (_Py_IsMainInterpreter(interp)) {
_PyStructSequence_FiniBuiltin(&VersionInfoType);
_PyStructSequence_FiniBuiltin(&FlagsType);
if (!_Py_IsMainInterpreter(interp)) {
return;
}

_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__
Py_CLEAR(EmscriptenInfoType);
Py_CLEAR(EmscriptenInfoType);
#endif
}
}


Expand Down
5 changes: 3 additions & 2 deletions Python/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -195,5 +196,5 @@ _PyThread_FiniType(PyInterpreterState *interp)
return;
}

_PyStructSequence_FiniBuiltin(&ThreadInfoType);
_PyStructSequence_FiniBuiltin(interp, &ThreadInfoType);
}
0