There were cases where we do unnecessary work for builtin static types. This also simplifies some work necessary for a per-interpreter GIL.
#endif
-/* runtime lifecycle */
-
-extern PyStatus _PyBytes_InitTypes(PyInterpreterState *);
-
-
/* Substring Search.
Returns the index of the first occurrence of
/* runtime lifecycle */
extern PyStatus _PyTuple_InitGlobalObjects(PyInterpreterState *);
-extern PyStatus _PyTuple_InitTypes(PyInterpreterState *);
extern void _PyTuple_Fini(PyInterpreterState *);
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++) {
return PyFloat_Type.tp_as_number->nb_int(x);
}
- if (Py_TYPE(x)->tp_dict == NULL) {
+ if (_PyType_IsReady(Py_TYPE(x))) {
if (PyType_Ready(Py_TYPE(x)) < 0)
return NULL;
}
{NULL, NULL} /* sentinel */
};
-static int
-symtable_init_stentry_type(PyObject *m)
-{
- return PyType_Ready(&PySTEntry_Type);
-}
-
static int
symtable_init_constants(PyObject *m)
{
}
static PyModuleDef_Slot symtable_slots[] = {
- {Py_mod_exec, symtable_init_stentry_type},
{Py_mod_exec, symtable_init_constants},
{0, NULL}
};
}
-PyStatus
-_PyBytes_InitTypes(PyInterpreterState *interp)
-{
- if (!_Py_IsMainInterpreter(interp)) {
- return _PyStatus_OK();
- }
-
- if (PyType_Ready(&PyBytes_Type) < 0) {
- return _PyStatus_ERR("Can't initialize bytes type");
- }
-
- if (PyType_Ready(&PyBytesIter_Type) < 0) {
- return _PyStatus_ERR("Can't initialize bytes iterator type");
- }
-
- return _PyStatus_OK();
-}
-
-
/*********************** Bytes Iterator ****************************/
typedef struct {
PyObject *descr = NULL;
{
- if (tp->tp_dict == NULL) {
+ if (!_PyType_IsReady(tp)) {
if (PyType_Ready(tp) < 0)
return NULL;
}
PyTypeObject *tp = Py_TYPE(self);
PyObject *descr = NULL;
- if (tp->tp_dict == NULL) {
+ if (!_PyType_IsReady(tp)) {
if (PyType_Ready(tp) < 0)
return NULL;
}
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) {
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();
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();
* an explicit call to PyType_Ready, we implicitly call
* PyType_Ready here and then check the tp_hash slot again
*/
- if (tp->tp_dict == NULL) {
+ if (!_PyType_IsReady(tp)) {
if (PyType_Ready(tp) < 0)
return -1;
if (tp->tp_hash != NULL)
}
Py_INCREF(name);
- if (tp->tp_dict == NULL) {
+ if (!_PyType_IsReady(tp)) {
if (PyType_Ready(tp) < 0)
goto done;
}
return -1;
}
- if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
+ if (!_PyType_IsReady(tp) && PyType_Ready(tp) < 0) {
return -1;
+ }
Py_INCREF(name);
Py_INCREF(tp);
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;
return -1;
}
initialize_static_fields(type, desc, members, tp_flags);
+
+ Py_INCREF(type); // XXX It should be immortal.
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) {
+
+ if (initialize_structseq_dict(
+ desc, type->tp_dict, 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
}
-PyStatus
-_PyTuple_InitTypes(PyInterpreterState *interp)
-{
- if (!_Py_IsMainInterpreter(interp)) {
- return _PyStatus_OK();
- }
-
- if (PyType_Ready(&PyTuple_Type) < 0) {
- return _PyStatus_ERR("Can't initialize tuple type");
- }
-
- if (PyType_Ready(&PyTupleIter_Type) < 0) {
- return _PyStatus_ERR("Can't initialize tuple iterator type");
- }
-
- return _PyStatus_OK();
-}
-
static void maybe_freelist_clear(PyInterpreterState *, int);
void
static int
type_ready(PyTypeObject *type)
{
+ _PyObject_ASSERT((PyObject *)type,
+ (type->tp_flags & Py_TPFLAGS_READYING) == 0);
+ type->tp_flags |= Py_TPFLAGS_READYING;
+
if (type_ready_pre_checks(type) < 0) {
- return -1;
+ goto error;
}
#ifdef Py_TRACE_REFS
/* Initialize tp_dict: _PyType_IsReady() tests if tp_dict != NULL */
if (type_ready_set_dict(type) < 0) {
- return -1;
+ goto error;
}
if (type_ready_set_bases(type) < 0) {
- return -1;
+ goto error;
}
if (type_ready_mro(type) < 0) {
- return -1;
+ goto error;
}
if (type_ready_set_new(type) < 0) {
- return -1;
+ goto error;
}
if (type_ready_fill_dict(type) < 0) {
- return -1;
+ goto error;
}
if (type_ready_inherit(type) < 0) {
- return -1;
+ goto error;
}
if (type_ready_preheader(type) < 0) {
- return -1;
+ goto error;
}
if (type_ready_set_hash(type) < 0) {
- return -1;
+ goto error;
}
if (type_ready_add_subclasses(type) < 0) {
- return -1;
+ goto error;
}
if (type_ready_managed_dict(type) < 0) {
- return -1;
+ goto error;
}
if (type_ready_post_checks(type) < 0) {
- return -1;
+ goto error;
}
+
+ /* All done -- set the ready flag */
+ type->tp_flags = (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
+
+ assert(_PyType_CheckConsistency(type));
return 0;
-}
+error:
+ type->tp_flags &= ~Py_TPFLAGS_READYING;
+ return -1;
+}
int
PyType_Ready(PyTypeObject *type)
assert(_PyType_CheckConsistency(type));
return 0;
}
- _PyObject_ASSERT((PyObject *)type,
- (type->tp_flags & Py_TPFLAGS_READYING) == 0);
-
- type->tp_flags |= Py_TPFLAGS_READYING;
+ assert(!(type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN));
/* Historically, all static types were immutable. See bpo-43908 */
if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
type->tp_flags |= Py_TPFLAGS_IMMUTABLETYPE;
}
- if (type_ready(type) < 0) {
- type->tp_flags &= ~Py_TPFLAGS_READYING;
- return -1;
- }
-
- /* All done -- set the ready flag */
- type->tp_flags = (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
- assert(_PyType_CheckConsistency(type));
- return 0;
+ return type_ready(type);
}
int
_PyStaticType_InitBuiltin(PyTypeObject *self)
{
+ assert(!(self->tp_flags & Py_TPFLAGS_HEAPTYPE));
+
+ if (self->tp_flags & Py_TPFLAGS_READY) {
+ assert(self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN);
+ assert(_PyType_CheckConsistency(self));
+ return 0;
+ }
+
self->tp_flags |= _Py_TPFLAGS_STATIC_BUILTIN;
+ self->tp_flags |= Py_TPFLAGS_IMMUTABLETYPE;
assert(NEXT_GLOBAL_VERSION_TAG <= _Py_MAX_GLOBAL_TYPE_VERSION_TAG);
self->tp_version_tag = NEXT_GLOBAL_VERSION_TAG++;
static_builtin_state_init(self);
- int res = PyType_Ready(self);
+ int res = type_ready(self);
if (res < 0) {
static_builtin_state_clear(self);
}
PyStatus
_PyUnicode_InitTypes(PyInterpreterState *interp)
{
- if (!_Py_IsMainInterpreter(interp)) {
- return _PyStatus_OK();
- }
-
if (_PyStaticType_InitBuiltin(&EncodingMapType) < 0) {
goto error;
}
{
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;
}
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();
}
#include "Python.h"
#include "pycore_abstract.h" // _PyIndex_Check()
+#include "pycore_object.h" // _PyType_IsReady()
#define FLAG_SIZE_T 1
typedef double va_double;
int
PyModule_AddType(PyObject *module, PyTypeObject *type)
{
- if (PyType_Ready(type) < 0) {
+ if (!_PyType_IsReady(type) && PyType_Ready(type) < 0) {
return -1;
}
#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()
#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"
return status;
}
- status = _PyBytes_InitTypes(interp);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
-
status = _PyLong_InitTypes(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
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");
}
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));
#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)
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__
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);