@@ -5853,7 +5853,7 @@ type_ready_checks(PyTypeObject *type)
5853
5853
5854
5854
5855
5855
static int
5856
- type_ready_set_base (PyTypeObject * type )
5856
+ type_ready_set_bases (PyTypeObject * type )
5857
5857
{
5858
5858
/* Initialize tp_base (defaults to BaseObject unless that's us) */
5859
5859
PyTypeObject * base = type -> tp_base ;
@@ -5887,13 +5887,7 @@ type_ready_set_base(PyTypeObject *type)
5887
5887
if (Py_IS_TYPE (type , NULL ) && base != NULL ) {
5888
5888
Py_SET_TYPE (type , Py_TYPE (base ));
5889
5889
}
5890
- return 0 ;
5891
- }
5892
-
5893
5890
5894
- static int
5895
- type_ready_add_attrs (PyTypeObject * type )
5896
- {
5897
5891
/* Initialize tp_bases */
5898
5892
PyObject * bases = type -> tp_bases ;
5899
5893
if (bases == NULL ) {
@@ -5909,17 +5903,29 @@ type_ready_add_attrs(PyTypeObject *type)
5909
5903
}
5910
5904
type -> tp_bases = bases ;
5911
5905
}
5906
+ return 0 ;
5907
+ }
5912
5908
5913
- /* Initialize tp_dict */
5914
- PyObject * dict = type -> tp_dict ;
5909
+
5910
+ static int
5911
+ type_ready_set_dict (PyTypeObject * type )
5912
+ {
5913
+ if (type -> tp_dict != NULL ) {
5914
+ return 0 ;
5915
+ }
5916
+
5917
+ PyObject * dict = PyDict_New ();
5915
5918
if (dict == NULL ) {
5916
- dict = PyDict_New ();
5917
- if (dict == NULL ) {
5918
- return -1 ;
5919
- }
5920
- type -> tp_dict = dict ;
5919
+ return -1 ;
5921
5920
}
5921
+ type -> tp_dict = dict ;
5922
+ return 0 ;
5923
+ }
5924
+
5922
5925
5926
+ static int
5927
+ type_ready_add_attrs (PyTypeObject * type )
5928
+ {
5923
5929
/* Add type-specific descriptors to tp_dict */
5924
5930
if (add_operators (type ) < 0 ) {
5925
5931
return -1 ;
@@ -5972,12 +5978,35 @@ type_ready_mro(PyTypeObject *type)
5972
5978
}
5973
5979
5974
5980
5981
+ /* Some more special stuff */
5982
+ static void
5983
+ type_ready_inherit_special (PyTypeObject * type , PyTypeObject * base )
5984
+ {
5985
+ if (type -> tp_as_async == NULL ) {
5986
+ type -> tp_as_async = base -> tp_as_async ;
5987
+ }
5988
+ if (type -> tp_as_number == NULL ) {
5989
+ type -> tp_as_number = base -> tp_as_number ;
5990
+ }
5991
+ if (type -> tp_as_sequence == NULL ) {
5992
+ type -> tp_as_sequence = base -> tp_as_sequence ;
5993
+ }
5994
+ if (type -> tp_as_mapping == NULL ) {
5995
+ type -> tp_as_mapping = base -> tp_as_mapping ;
5996
+ }
5997
+ if (type -> tp_as_buffer == NULL ) {
5998
+ type -> tp_as_buffer = base -> tp_as_buffer ;
5999
+ }
6000
+ }
6001
+
6002
+
5975
6003
static int
5976
6004
type_ready_inherit (PyTypeObject * type )
5977
6005
{
5978
6006
/* Inherit special flags from dominant base */
5979
- if (type -> tp_base != NULL ) {
5980
- inherit_special (type , type -> tp_base );
6007
+ PyTypeObject * base = type -> tp_base ;
6008
+ if (base != NULL ) {
6009
+ inherit_special (type , base );
5981
6010
}
5982
6011
5983
6012
/* Initialize tp_dict properly */
@@ -5992,6 +6021,10 @@ type_ready_inherit(PyTypeObject *type)
5992
6021
}
5993
6022
}
5994
6023
6024
+ if (base != NULL ) {
6025
+ type_ready_inherit_special (type , base );
6026
+ }
6027
+
5995
6028
/* Sanity check for tp_free. */
5996
6029
if (_PyType_IS_GC (type ) && (type -> tp_flags & Py_TPFLAGS_BASETYPE ) &&
5997
6030
(type -> tp_free == NULL || type -> tp_free == PyObject_Del ))
@@ -6005,6 +6038,7 @@ type_ready_inherit(PyTypeObject *type)
6005
6038
type -> tp_name );
6006
6039
return -1 ;
6007
6040
}
6041
+
6008
6042
return 0 ;
6009
6043
}
6010
6044
@@ -6073,33 +6107,6 @@ type_ready_set_hash(PyTypeObject *type)
6073
6107
}
6074
6108
6075
6109
6076
- /* Some more special stuff */
6077
- static void
6078
- type_ready_inherit_special (PyTypeObject * type )
6079
- {
6080
- PyTypeObject * base = type -> tp_base ;
6081
- if (base == NULL ) {
6082
- return ;
6083
- }
6084
-
6085
- if (type -> tp_as_async == NULL ) {
6086
- type -> tp_as_async = base -> tp_as_async ;
6087
- }
6088
- if (type -> tp_as_number == NULL ) {
6089
- type -> tp_as_number = base -> tp_as_number ;
6090
- }
6091
- if (type -> tp_as_sequence == NULL ) {
6092
- type -> tp_as_sequence = base -> tp_as_sequence ;
6093
- }
6094
- if (type -> tp_as_mapping == NULL ) {
6095
- type -> tp_as_mapping = base -> tp_as_mapping ;
6096
- }
6097
- if (type -> tp_as_buffer == NULL ) {
6098
- type -> tp_as_buffer = base -> tp_as_buffer ;
6099
- }
6100
- }
6101
-
6102
-
6103
6110
/* Link into each base class's list of subclasses */
6104
6111
static int
6105
6112
type_ready_add_subclasses (PyTypeObject * type )
@@ -6132,15 +6139,19 @@ type_ready(PyTypeObject *type)
6132
6139
_Py_AddToAllObjects ((PyObject * )type , 0 );
6133
6140
#endif
6134
6141
6135
- if (type_ready_set_base (type ) < 0 ) {
6142
+ /* Initialize tp_dict: _PyType_IsReady() tests if tp_dict != NULL */
6143
+ if (type_ready_set_dict (type ) < 0 ) {
6136
6144
return -1 ;
6137
6145
}
6138
- if (type_ready_add_attrs (type ) < 0 ) {
6146
+ if (type_ready_set_bases (type ) < 0 ) {
6139
6147
return -1 ;
6140
6148
}
6141
6149
if (type_ready_mro (type ) < 0 ) {
6142
6150
return -1 ;
6143
6151
}
6152
+ if (type_ready_add_attrs (type ) < 0 ) {
6153
+ return -1 ;
6154
+ }
6144
6155
if (type_ready_inherit (type ) < 0 ) {
6145
6156
return -1 ;
6146
6157
}
@@ -6150,9 +6161,6 @@ type_ready(PyTypeObject *type)
6150
6161
if (type_ready_set_hash (type ) < 0 ) {
6151
6162
return -1 ;
6152
6163
}
6153
-
6154
- type_ready_inherit_special (type );
6155
-
6156
6164
if (type_ready_add_subclasses (type ) < 0 ) {
6157
6165
return -1 ;
6158
6166
}
0 commit comments