@@ -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
@@ -7264,37 +7270,26 @@ _PyStaticType_InitBuiltin(PyInterpreterState *interp, PyTypeObject *self)
72647270 assert (!(self -> tp_flags & Py_TPFLAGS_MANAGED_DICT ));
72657271 assert (!(self -> tp_flags & Py_TPFLAGS_MANAGED_WEAKREF ));
72667272
7267- #ifndef NDEBUG
72687273 int ismain = _Py_IsMainInterpreter (interp );
7269- #endif
7270- if (self -> tp_flags & Py_TPFLAGS_READY ) {
7274+ if ((self -> tp_flags & Py_TPFLAGS_READY ) == 0 ) {
7275+ assert (ismain );
7276+
7277+ self -> tp_flags |= _Py_TPFLAGS_STATIC_BUILTIN ;
7278+ self -> tp_flags |= Py_TPFLAGS_IMMUTABLETYPE ;
7279+
7280+ assert (NEXT_GLOBAL_VERSION_TAG <= _Py_MAX_GLOBAL_TYPE_VERSION_TAG );
7281+ self -> tp_version_tag = NEXT_GLOBAL_VERSION_TAG ++ ;
7282+ self -> tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG ;
7283+ }
7284+ else {
72717285 assert (!ismain );
72727286 assert (self -> tp_flags & _Py_TPFLAGS_STATIC_BUILTIN );
72737287 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 ;
72847288 }
72857289
7286- assert (ismain );
7287-
7288- self -> tp_flags |= _Py_TPFLAGS_STATIC_BUILTIN ;
7289- self -> tp_flags |= Py_TPFLAGS_IMMUTABLETYPE ;
7290-
7291- assert (NEXT_GLOBAL_VERSION_TAG <= _Py_MAX_GLOBAL_TYPE_VERSION_TAG );
7292- self -> tp_version_tag = NEXT_GLOBAL_VERSION_TAG ++ ;
7293- self -> tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG ;
7294-
72957290 static_builtin_state_init (interp , self );
72967291
7297- int res = type_ready (self );
7292+ int res = type_ready (self , ! ismain );
72987293 if (res < 0 ) {
72997294 static_builtin_state_clear (interp , self );
73007295 }
0 commit comments