Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
Make sure _PyStaticType_InitBuiltin() is called for all interpreters.
  • Loading branch information
ericsnowcurrently committed Apr 27, 2023
commit 0792ff35d566c4a151139ecb3efb5cc4cddaa772
10 changes: 4 additions & 6 deletions Modules/_io/_iomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -671,13 +671,11 @@ static PyTypeObject* static_types[] = {
PyStatus
_PyIO_InitTypes(PyInterpreterState *interp)
{
if (!_Py_IsMainInterpreter(interp)) {
return _PyStatus_OK();
}

// Set type base classes
#ifdef HAVE_WINDOWS_CONSOLE_IO
PyWindowsConsoleIO_Type.tp_base = &PyRawIOBase_Type;
if (_Py_IsMainInterpreter(interp)) {
// Set type base classes
PyWindowsConsoleIO_Type.tp_base = &PyRawIOBase_Type;
}
#endif

for (size_t i=0; i < Py_ARRAY_LENGTH(static_types); i++) {
Expand Down
4 changes: 0 additions & 4 deletions Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -3591,10 +3591,6 @@ static struct static_exception static_exceptions[] = {
int
_PyExc_InitTypes(PyInterpreterState *interp)
{
if (!_Py_IsMainInterpreter(interp)) {
return 0;
}

for (size_t i=0; i < Py_ARRAY_LENGTH(static_exceptions); i++) {
PyTypeObject *exc = static_exceptions[i].exc;
if (_PyStaticType_InitBuiltin(exc) < 0) {
Expand Down
12 changes: 3 additions & 9 deletions Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1990,20 +1990,14 @@ _PyFloat_InitState(PyInterpreterState *interp)
PyStatus
_PyFloat_InitTypes(PyInterpreterState *interp)
{
if (!_Py_IsMainInterpreter(interp)) {
return _PyStatus_OK();
}

if (PyType_Ready(&PyFloat_Type) < 0) {
return _PyStatus_ERR("Can't initialize float type");
}

/* Init float info */
if (FloatInfoType.tp_name == NULL) {
if (_PyStructSequence_InitBuiltin(&FloatInfoType,
&floatinfo_desc) < 0) {
return _PyStatus_ERR("can't init float info type");
}
if (_PyStructSequence_InitBuiltin(&FloatInfoType,
&floatinfo_desc) < 0) {
return _PyStatus_ERR("can't init float info type");
}

return _PyStatus_OK();
Expand Down
10 changes: 2 additions & 8 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -6351,19 +6351,13 @@ PyLong_GetInfo(void)
PyStatus
_PyLong_InitTypes(PyInterpreterState *interp)
{
if (!_Py_IsMainInterpreter(interp)) {
return _PyStatus_OK();
}

if (PyType_Ready(&PyLong_Type) < 0) {
return _PyStatus_ERR("Can't initialize int type");
}

/* initialize int_info */
if (Int_InfoType.tp_name == NULL) {
if (_PyStructSequence_InitBuiltin(&Int_InfoType, &int_info_desc) < 0) {
return _PyStatus_ERR("can't init int info type");
}
if (_PyStructSequence_InitBuiltin(&Int_InfoType, &int_info_desc) < 0) {
return _PyStatus_ERR("can't init int info type");
}

return _PyStatus_OK();
Expand Down
18 changes: 14 additions & 4 deletions Objects/structseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,13 @@ _PyStructSequence_InitBuiltinWithFlags(PyTypeObject *type,
PyStructSequence_Desc *desc,
unsigned long tp_flags)
{
if (type->tp_flags & Py_TPFLAGS_READY) {
if (_PyStaticType_InitBuiltin(type) < 0) {
goto failed_init_builtin;
}
return 0;
}

PyMemberDef *members;
Py_ssize_t n_members, n_unnamed_members;

Expand All @@ -519,16 +526,19 @@ _PyStructSequence_InitBuiltinWithFlags(PyTypeObject *type,
initialize_static_fields(type, desc, members, tp_flags);
if (_PyStaticType_InitBuiltin(type) < 0) {
PyMem_Free(members);
PyErr_Format(PyExc_RuntimeError,
"Can't initialize builtin type %s",
desc->name);
return -1;
goto failed_init_builtin;
}
if (initialize_static_type(type, desc, n_members, n_unnamed_members) < 0) {
PyMem_Free(members);
return -1;
}
return 0;

failed_init_builtin:
PyErr_Format(PyExc_RuntimeError,
"Can't initialize builtin type %s",
desc->name);
return -1;
}

int
Expand Down
4 changes: 0 additions & 4 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -14573,10 +14573,6 @@ _PyUnicode_InitGlobalObjects(PyInterpreterState *interp)
PyStatus
_PyUnicode_InitTypes(PyInterpreterState *interp)
{
if (!_Py_IsMainInterpreter(interp)) {
return _PyStatus_OK();
}

if (_PyStaticType_InitBuiltin(&EncodingMapType) < 0) {
goto error;
}
Expand Down
12 changes: 3 additions & 9 deletions Python/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -1342,15 +1342,9 @@ static PyStructSequence_Desc UnraisableHookArgs_desc = {
PyStatus
_PyErr_InitTypes(PyInterpreterState *interp)
{
if (!_Py_IsMainInterpreter(interp)) {
return _PyStatus_OK();
}

if (UnraisableHookArgsType.tp_name == NULL) {
if (_PyStructSequence_InitBuiltin(&UnraisableHookArgsType,
&UnraisableHookArgs_desc) < 0) {
return _PyStatus_ERR("failed to initialize UnraisableHookArgs type");
}
if (_PyStructSequence_InitBuiltin(&UnraisableHookArgsType,
&UnraisableHookArgs_desc) < 0) {
return _PyStatus_ERR("failed to initialize UnraisableHookArgs type");
}
return _PyStatus_OK();
}
Expand Down
22 changes: 8 additions & 14 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3166,10 +3166,8 @@ _PySys_InitCore(PyThreadState *tstate, PyObject *sysdict)
SET_SYS("float_info", PyFloat_GetInfo());
SET_SYS("int_info", PyLong_GetInfo());
/* initialize hash_info */
if (Hash_InfoType.tp_name == NULL) {
if (_PyStructSequence_InitBuiltin(&Hash_InfoType, &hash_info_desc) < 0) {
goto type_init_failed;
}
if (_PyStructSequence_InitBuiltin(&Hash_InfoType, &hash_info_desc) < 0) {
goto type_init_failed;
}
SET_SYS("hash_info", get_hash_info(tstate));
SET_SYS("maxunicode", PyLong_FromLong(0x10FFFF));
Expand All @@ -3191,11 +3189,9 @@ _PySys_InitCore(PyThreadState *tstate, PyObject *sysdict)

#define ENSURE_INFO_TYPE(TYPE, DESC) \
do { \
if (TYPE.tp_name == NULL) { \
if (_PyStructSequence_InitBuiltinWithFlags( \
&TYPE, &DESC, Py_TPFLAGS_DISALLOW_INSTANTIATION) < 0) { \
goto type_init_failed; \
} \
if (_PyStructSequence_InitBuiltinWithFlags( \
&TYPE, &DESC, Py_TPFLAGS_DISALLOW_INSTANTIATION) < 0) { \
goto type_init_failed; \
} \
} while (0)

Expand Down Expand Up @@ -3230,11 +3226,9 @@ _PySys_InitCore(PyThreadState *tstate, PyObject *sysdict)
SET_SYS("thread_info", PyThread_GetInfo());

/* initialize asyncgen_hooks */
if (AsyncGenHooksType.tp_name == NULL) {
if (_PyStructSequence_InitBuiltin(
&AsyncGenHooksType, &asyncgen_hooks_desc) < 0) {
goto type_init_failed;
}
if (_PyStructSequence_InitBuiltin(
&AsyncGenHooksType, &asyncgen_hooks_desc) < 0) {
goto type_init_failed;
}

#ifdef __EMSCRIPTEN__
Expand Down
6 changes: 2 additions & 4 deletions Python/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,8 @@ PyThread_GetInfo(void)
int len;
#endif

if (ThreadInfoType.tp_name == 0) {
if (_PyStructSequence_InitBuiltin(&ThreadInfoType,
&threadinfo_desc) < 0)
return NULL;
if (_PyStructSequence_InitBuiltin(&ThreadInfoType, &threadinfo_desc) < 0) {
return NULL;
}

threadinfo = PyStructSequence_New(&ThreadInfoType);
Expand Down