@@ -306,11 +306,14 @@ clear_tp_bases(PyTypeObject *self)
306306{
307307 if (self -> tp_flags & _Py_TPFLAGS_STATIC_BUILTIN ) {
308308 if (_Py_IsMainInterpreter (_PyInterpreterState_GET ())) {
309- if (self -> tp_bases != NULL
310- && PyTuple_GET_SIZE (self -> tp_bases ) > 0 )
311- {
312- assert (_Py_IsImmortal (self -> tp_bases ));
313- _Py_ClearImmortal (self -> tp_bases );
309+ if (self -> tp_bases != NULL ) {
310+ if (PyTuple_GET_SIZE (self -> tp_bases ) == 0 ) {
311+ Py_CLEAR (self -> tp_bases );
312+ }
313+ else {
314+ assert (_Py_IsImmortal (self -> tp_bases ));
315+ _Py_ClearImmortal (self -> tp_bases );
316+ }
314317 }
315318 }
316319 return ;
@@ -352,11 +355,14 @@ clear_tp_mro(PyTypeObject *self)
352355{
353356 if (self -> tp_flags & _Py_TPFLAGS_STATIC_BUILTIN ) {
354357 if (_Py_IsMainInterpreter (_PyInterpreterState_GET ())) {
355- if (self -> tp_mro != NULL
356- && PyTuple_GET_SIZE (self -> tp_mro ) > 0 )
357- {
358- assert (_Py_IsImmortal (self -> tp_mro ));
359- _Py_ClearImmortal (self -> tp_mro );
358+ if (self -> tp_mro != NULL ) {
359+ if (PyTuple_GET_SIZE (self -> tp_mro ) == 0 ) {
360+ Py_CLEAR (self -> tp_mro );
361+ }
362+ else {
363+ assert (_Py_IsImmortal (self -> tp_mro ));
364+ _Py_ClearImmortal (self -> tp_mro );
365+ }
360366 }
361367 }
362368 return ;
@@ -6996,12 +7002,8 @@ type_ready_pre_checks(PyTypeObject *type)
69967002
69977003
69987004static int
6999- type_ready_set_bases (PyTypeObject * type )
7005+ type_ready_set_base (PyTypeObject * type )
70007006{
7001- if (lookup_tp_bases (type ) != NULL ) {
7002- return 0 ;
7003- }
7004-
70057007 /* Initialize tp_base (defaults to BaseObject unless that's us) */
70067008 PyTypeObject * base = type -> tp_base ;
70077009 if (base == NULL && type != & PyBaseObject_Type ) {
@@ -7025,18 +7027,38 @@ type_ready_set_bases(PyTypeObject *type)
70257027 }
70267028 }
70277029
7030+ return 0 ;
7031+ }
7032+
7033+ static int
7034+ type_ready_set_type (PyTypeObject * type )
7035+ {
70287036 /* Initialize ob_type if NULL. This means extensions that want to be
70297037 compilable separately on Windows can call PyType_Ready() instead of
70307038 initializing the ob_type field of their type objects. */
70317039 /* The test for base != NULL is really unnecessary, since base is only
70327040 NULL when type is &PyBaseObject_Type, and we know its ob_type is
70337041 not NULL (it's initialized to &PyType_Type). But coverity doesn't
70347042 know that. */
7043+ PyTypeObject * base = type -> tp_base ;
70357044 if (Py_IS_TYPE (type , NULL ) && base != NULL ) {
70367045 Py_SET_TYPE (type , Py_TYPE (base ));
70377046 }
70387047
7039- /* Initialize tp_bases */
7048+ return 0 ;
7049+ }
7050+
7051+ static int
7052+ type_ready_set_bases (PyTypeObject * type )
7053+ {
7054+ if (type -> tp_flags & _Py_TPFLAGS_STATIC_BUILTIN ) {
7055+ if (!_Py_IsMainInterpreter (_PyInterpreterState_GET ())) {
7056+ assert (lookup_tp_bases (type ) != NULL );
7057+ return 0 ;
7058+ }
7059+ assert (lookup_tp_bases (type ) == NULL );
7060+ }
7061+
70407062 PyObject * bases = lookup_tp_bases (type );
70417063 if (bases == NULL ) {
70427064 PyTypeObject * base = type -> tp_base ;
@@ -7446,6 +7468,12 @@ type_ready(PyTypeObject *type, int rerunbuiltin)
74467468 if (type_ready_set_dict (type ) < 0 ) {
74477469 goto error ;
74487470 }
7471+ if (type_ready_set_base (type ) < 0 ) {
7472+ goto error ;
7473+ }
7474+ if (type_ready_set_type (type ) < 0 ) {
7475+ goto error ;
7476+ }
74497477 if (type_ready_set_bases (type ) < 0 ) {
74507478 goto error ;
74517479 }
0 commit comments