Skip to content

Commit 65f058e

Browse files
authored
bpo-43770: Reorder type_ready() (GH-25373)
Add type_ready_create_dict() sub-function. * Start with type_ready_create_dict(). * Call type_ready_mro() earlier, before type_ready_add_attrs(). * Call type_ready_inherit_special() earlier, in type_ready_inherit().
1 parent 54db51c commit 65f058e

File tree

1 file changed

+56
-48
lines changed

1 file changed

+56
-48
lines changed

Objects/typeobject.c

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5853,7 +5853,7 @@ type_ready_checks(PyTypeObject *type)
58535853

58545854

58555855
static int
5856-
type_ready_set_base(PyTypeObject *type)
5856+
type_ready_set_bases(PyTypeObject *type)
58575857
{
58585858
/* Initialize tp_base (defaults to BaseObject unless that's us) */
58595859
PyTypeObject *base = type->tp_base;
@@ -5887,13 +5887,7 @@ type_ready_set_base(PyTypeObject *type)
58875887
if (Py_IS_TYPE(type, NULL) && base != NULL) {
58885888
Py_SET_TYPE(type, Py_TYPE(base));
58895889
}
5890-
return 0;
5891-
}
5892-
58935890

5894-
static int
5895-
type_ready_add_attrs(PyTypeObject *type)
5896-
{
58975891
/* Initialize tp_bases */
58985892
PyObject *bases = type->tp_bases;
58995893
if (bases == NULL) {
@@ -5909,17 +5903,29 @@ type_ready_add_attrs(PyTypeObject *type)
59095903
}
59105904
type->tp_bases = bases;
59115905
}
5906+
return 0;
5907+
}
59125908

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();
59155918
if (dict == NULL) {
5916-
dict = PyDict_New();
5917-
if (dict == NULL) {
5918-
return -1;
5919-
}
5920-
type->tp_dict = dict;
5919+
return -1;
59215920
}
5921+
type->tp_dict = dict;
5922+
return 0;
5923+
}
5924+
59225925

5926+
static int
5927+
type_ready_add_attrs(PyTypeObject *type)
5928+
{
59235929
/* Add type-specific descriptors to tp_dict */
59245930
if (add_operators(type) < 0) {
59255931
return -1;
@@ -5972,12 +5978,35 @@ type_ready_mro(PyTypeObject *type)
59725978
}
59735979

59745980

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+
59756003
static int
59766004
type_ready_inherit(PyTypeObject *type)
59776005
{
59786006
/* 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);
59816010
}
59826011

59836012
/* Initialize tp_dict properly */
@@ -5992,6 +6021,10 @@ type_ready_inherit(PyTypeObject *type)
59926021
}
59936022
}
59946023

6024+
if (base != NULL) {
6025+
type_ready_inherit_special(type, base);
6026+
}
6027+
59956028
/* Sanity check for tp_free. */
59966029
if (_PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&
59976030
(type->tp_free == NULL || type->tp_free == PyObject_Del))
@@ -6005,6 +6038,7 @@ type_ready_inherit(PyTypeObject *type)
60056038
type->tp_name);
60066039
return -1;
60076040
}
6041+
60086042
return 0;
60096043
}
60106044

@@ -6073,33 +6107,6 @@ type_ready_set_hash(PyTypeObject *type)
60736107
}
60746108

60756109

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-
61036110
/* Link into each base class's list of subclasses */
61046111
static int
61056112
type_ready_add_subclasses(PyTypeObject *type)
@@ -6132,15 +6139,19 @@ type_ready(PyTypeObject *type)
61326139
_Py_AddToAllObjects((PyObject *)type, 0);
61336140
#endif
61346141

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) {
61366144
return -1;
61376145
}
6138-
if (type_ready_add_attrs(type) < 0) {
6146+
if (type_ready_set_bases(type) < 0) {
61396147
return -1;
61406148
}
61416149
if (type_ready_mro(type) < 0) {
61426150
return -1;
61436151
}
6152+
if (type_ready_add_attrs(type) < 0) {
6153+
return -1;
6154+
}
61446155
if (type_ready_inherit(type) < 0) {
61456156
return -1;
61466157
}
@@ -6150,9 +6161,6 @@ type_ready(PyTypeObject *type)
61506161
if (type_ready_set_hash(type) < 0) {
61516162
return -1;
61526163
}
6153-
6154-
type_ready_inherit_special(type);
6155-
61566164
if (type_ready_add_subclasses(type) < 0) {
61576165
return -1;
61586166
}

0 commit comments

Comments
 (0)