@@ -7075,7 +7075,7 @@ type_ready_add_subclasses(PyTypeObject *type)
70757075// Set tp_new and the "__new__" key in the type dictionary.
70767076// Use the Py_TPFLAGS_DISALLOW_INSTANTIATION flag.
70777077static int
7078- type_ready_set_new (PyTypeObject * type )
7078+ type_ready_set_new (PyTypeObject * type , int rerunbuiltin )
70797079{
70807080 PyTypeObject * base = type -> tp_base ;
70817081 /* The condition below could use some explanation.
@@ -7097,10 +7097,12 @@ type_ready_set_new(PyTypeObject *type)
70977097
70987098 if (!(type -> tp_flags & Py_TPFLAGS_DISALLOW_INSTANTIATION )) {
70997099 if (type -> tp_new != NULL ) {
7100- // If "__new__" key does not exists in the type dictionary,
7101- // set it to tp_new_wrapper().
7102- if (add_tp_new_wrapper (type ) < 0 ) {
7103- return -1 ;
7100+ if (!rerunbuiltin || base == NULL || type -> tp_new != base -> tp_new ) {
7101+ // If "__new__" key does not exists in the type dictionary,
7102+ // set it to tp_new_wrapper().
7103+ if (add_tp_new_wrapper (type ) < 0 ) {
7104+ return -1 ;
7105+ }
71047106 }
71057107 }
71067108 else {
@@ -7174,7 +7176,7 @@ type_ready_post_checks(PyTypeObject *type)
71747176
71757177
71767178static int
7177- type_ready (PyTypeObject * type )
7179+ type_ready (PyTypeObject * type , int rerunbuiltin )
71787180{
71797181 _PyObject_ASSERT ((PyObject * )type ,
71807182 (type -> tp_flags & Py_TPFLAGS_READYING ) == 0 );
@@ -7203,29 +7205,33 @@ type_ready(PyTypeObject *type)
72037205 if (type_ready_mro (type ) < 0 ) {
72047206 goto error ;
72057207 }
7206- if (type_ready_set_new (type ) < 0 ) {
7208+ if (type_ready_set_new (type , rerunbuiltin ) < 0 ) {
72077209 goto error ;
72087210 }
72097211 if (type_ready_fill_dict (type ) < 0 ) {
72107212 goto error ;
72117213 }
7212- if (type_ready_inherit (type ) < 0 ) {
7213- goto error ;
7214- }
7215- if (type_ready_preheader (type ) < 0 ) {
7216- goto error ;
7214+ if (!rerunbuiltin ) {
7215+ if (type_ready_inherit (type ) < 0 ) {
7216+ goto error ;
7217+ }
7218+ if (type_ready_preheader (type ) < 0 ) {
7219+ goto error ;
7220+ }
72177221 }
72187222 if (type_ready_set_hash (type ) < 0 ) {
72197223 goto error ;
72207224 }
72217225 if (type_ready_add_subclasses (type ) < 0 ) {
72227226 goto error ;
72237227 }
7224- if (type_ready_managed_dict (type ) < 0 ) {
7225- goto error ;
7226- }
7227- if (type_ready_post_checks (type ) < 0 ) {
7228- goto error ;
7228+ if (!rerunbuiltin ) {
7229+ if (type_ready_managed_dict (type ) < 0 ) {
7230+ goto error ;
7231+ }
7232+ if (type_ready_post_checks (type ) < 0 ) {
7233+ goto error ;
7234+ }
72297235 }
72307236
72317237 /* All done -- set the ready flag */
@@ -7253,7 +7259,7 @@ PyType_Ready(PyTypeObject *type)
72537259 type -> tp_flags |= Py_TPFLAGS_IMMUTABLETYPE ;
72547260 }
72557261
7256- return type_ready (type );
7262+ return type_ready (type , 0 );
72577263}
72587264
72597265int
@@ -7271,16 +7277,7 @@ _PyStaticType_InitBuiltin(PyInterpreterState *interp, PyTypeObject *self)
72717277 assert (!ismain );
72727278 assert (self -> tp_flags & _Py_TPFLAGS_STATIC_BUILTIN );
72737279 assert (self -> tp_flags & Py_TPFLAGS_VALID_VERSION_TAG );
7274-
7275- static_builtin_state_init (interp , self );
7276-
7277- /* We must explicitly set these for subinterpreters.
7278- tp_subclasses is set lazily. */
7279- type_ready_set_dict (self );
7280- type_ready_set_bases (self );
7281- type_ready_mro (self );
7282- assert (_PyType_CheckConsistency (self ));
7283- return 0 ;
7280+ return type_ready (self , 1 );
72847281 }
72857282
72867283 assert (ismain );
@@ -7294,7 +7291,7 @@ _PyStaticType_InitBuiltin(PyInterpreterState *interp, PyTypeObject *self)
72947291
72957292 static_builtin_state_init (interp , self );
72967293
7297- int res = type_ready (self );
7294+ int res = type_ready (self , 0 );
72987295 if (res < 0 ) {
72997296 static_builtin_state_clear (interp , self );
73007297 }
0 commit comments