gh-94673: Ensure Builtin Static Types are Readied Properly (gh-103940)

There were cases where we do unnecessary work for builtin static types. This also simplifies some work necessary for a per-interpreter GIL.
This commit is contained in:
Eric Snow 2023-04-27 16:19:43 -06:00 committed by GitHub
parent 56c7176d1d
commit d2e2e53f73
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 89 additions and 169 deletions

View file

@ -2316,7 +2316,7 @@ builtin_round_impl(PyObject *module, PyObject *number, PyObject *ndigits)
{
PyObject *round, *result;
if (Py_TYPE(number)->tp_dict == NULL) {
if (!_PyType_IsReady(Py_TYPE(number))) {
if (PyType_Ready(Py_TYPE(number)) < 0)
return NULL;
}

View file

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

View file

@ -3,6 +3,7 @@
#include "Python.h"
#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_object.h" // _PyType_IsReady()
#define FLAG_SIZE_T 1
typedef double va_double;
@ -693,7 +694,7 @@ PyModule_AddStringConstant(PyObject *m, const char *name, const char *value)
int
PyModule_AddType(PyObject *module, PyTypeObject *type)
{
if (PyType_Ready(type) < 0) {
if (!_PyType_IsReady(type) && PyType_Ready(type) < 0) {
return -1;
}

View file

@ -2,7 +2,6 @@
#include "Python.h"
#include "pycore_bytesobject.h" // _PyBytes_InitTypes()
#include "pycore_ceval.h" // _PyEval_FiniGIL()
#include "pycore_context.h" // _PyContext_Init()
#include "pycore_exceptions.h" // _PyExc_InitTypes()
@ -26,7 +25,6 @@
#include "pycore_sliceobject.h" // _PySlice_Fini()
#include "pycore_sysmodule.h" // _PySys_ClearAuditHooks()
#include "pycore_traceback.h" // _Py_DumpTracebackThreads()
#include "pycore_tuple.h" // _PyTuple_InitTypes()
#include "pycore_typeobject.h" // _PyTypes_InitTypes()
#include "pycore_unicodeobject.h" // _PyUnicode_InitTypes()
#include "opcode.h"
@ -684,11 +682,6 @@ pycore_init_types(PyInterpreterState *interp)
return status;
}
status = _PyBytes_InitTypes(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
status = _PyLong_InitTypes(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
@ -704,11 +697,6 @@ pycore_init_types(PyInterpreterState *interp)
return status;
}
status = _PyTuple_InitTypes(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
if (_PyExc_InitTypes(interp) < 0) {
return _PyStatus_ERR("failed to initialize an exception type");
}

View file

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

View file

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