diff --git a/Objects/typeobject.c b/Objects/typeobject.c index c8b1c3bf07721c6070e049e44be1ba3868cdf310_T2JqZWN0cy90eXBlb2JqZWN0LmM=..807f355df83e7bc56b93d828f6e299b9cd389da0_T2JqZWN0cy90eXBlb2JqZWN0LmM= 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -5853,7 +5853,7 @@ static int -type_ready_set_base(PyTypeObject *type) +type_ready_set_bases(PyTypeObject *type) { /* Initialize tp_base (defaults to BaseObject unless that's us) */ PyTypeObject *base = type->tp_base; @@ -5887,13 +5887,7 @@ if (Py_IS_TYPE(type, NULL) && base != NULL) { Py_SET_TYPE(type, Py_TYPE(base)); } - return 0; -} - - -static int -type_ready_add_attrs(PyTypeObject *type) -{ + /* Initialize tp_bases */ PyObject *bases = type->tp_bases; if (bases == NULL) { @@ -5909,7 +5903,16 @@ } type->tp_bases = bases; } - - /* Initialize tp_dict */ - PyObject *dict = type->tp_dict; + return 0; +} + + +static int +type_ready_set_dict(PyTypeObject *type) +{ + if (type->tp_dict != NULL) { + return 0; + } + + PyObject *dict = PyDict_New(); if (dict == NULL) { @@ -5915,11 +5918,14 @@ if (dict == NULL) { - dict = PyDict_New(); - if (dict == NULL) { - return -1; - } - type->tp_dict = dict; - } - + return -1; + } + type->tp_dict = dict; + return 0; +} + + +static int +type_ready_add_attrs(PyTypeObject *type) +{ /* Add type-specific descriptors to tp_dict */ if (add_operators(type) < 0) { return -1; @@ -5972,7 +5978,29 @@ } +/* Some more special stuff */ +static void +type_ready_inherit_special(PyTypeObject *type, PyTypeObject *base) +{ + if (type->tp_as_async == NULL) { + type->tp_as_async = base->tp_as_async; + } + if (type->tp_as_number == NULL) { + type->tp_as_number = base->tp_as_number; + } + if (type->tp_as_sequence == NULL) { + type->tp_as_sequence = base->tp_as_sequence; + } + if (type->tp_as_mapping == NULL) { + type->tp_as_mapping = base->tp_as_mapping; + } + if (type->tp_as_buffer == NULL) { + type->tp_as_buffer = base->tp_as_buffer; + } +} + + static int type_ready_inherit(PyTypeObject *type) { /* Inherit special flags from dominant base */ @@ -5975,9 +6003,10 @@ static int type_ready_inherit(PyTypeObject *type) { /* Inherit special flags from dominant base */ - if (type->tp_base != NULL) { - inherit_special(type, type->tp_base); + PyTypeObject *base = type->tp_base; + if (base != NULL) { + inherit_special(type, base); } /* Initialize tp_dict properly */ @@ -5992,6 +6021,10 @@ } } + if (base != NULL) { + type_ready_inherit_special(type, base); + } + /* Sanity check for tp_free. */ if (_PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) && (type->tp_free == NULL || type->tp_free == PyObject_Del)) @@ -6005,6 +6038,7 @@ type->tp_name); return -1; } + return 0; } @@ -6073,33 +6107,6 @@ } -/* Some more special stuff */ -static void -type_ready_inherit_special(PyTypeObject *type) -{ - PyTypeObject *base = type->tp_base; - if (base == NULL) { - return; - } - - if (type->tp_as_async == NULL) { - type->tp_as_async = base->tp_as_async; - } - if (type->tp_as_number == NULL) { - type->tp_as_number = base->tp_as_number; - } - if (type->tp_as_sequence == NULL) { - type->tp_as_sequence = base->tp_as_sequence; - } - if (type->tp_as_mapping == NULL) { - type->tp_as_mapping = base->tp_as_mapping; - } - if (type->tp_as_buffer == NULL) { - type->tp_as_buffer = base->tp_as_buffer; - } -} - - /* Link into each base class's list of subclasses */ static int type_ready_add_subclasses(PyTypeObject *type) @@ -6132,9 +6139,16 @@ _Py_AddToAllObjects((PyObject *)type, 0); #endif - if (type_ready_set_base(type) < 0) { + /* Initialize tp_dict: _PyType_IsReady() tests if tp_dict != NULL */ + if (type_ready_set_dict(type) < 0) { + return -1; + } + if (type_ready_set_bases(type) < 0) { + return -1; + } + if (type_ready_mro(type) < 0) { return -1; } if (type_ready_add_attrs(type) < 0) { return -1; } @@ -6136,11 +6150,8 @@ return -1; } if (type_ready_add_attrs(type) < 0) { return -1; } - if (type_ready_mro(type) < 0) { - return -1; - } if (type_ready_inherit(type) < 0) { return -1; } @@ -6150,9 +6161,6 @@ if (type_ready_set_hash(type) < 0) { return -1; } - - type_ready_inherit_special(type); - if (type_ready_add_subclasses(type) < 0) { return -1; }