@@ -7075,7 +7075,7 @@ type_ready_add_subclasses(PyTypeObject *type)
7075
7075
// Set tp_new and the "__new__" key in the type dictionary.
7076
7076
// Use the Py_TPFLAGS_DISALLOW_INSTANTIATION flag.
7077
7077
static int
7078
- type_ready_set_new (PyTypeObject * type )
7078
+ type_ready_set_new (PyTypeObject * type , int rerunbuiltin )
7079
7079
{
7080
7080
PyTypeObject * base = type -> tp_base ;
7081
7081
/* The condition below could use some explanation.
@@ -7097,10 +7097,12 @@ type_ready_set_new(PyTypeObject *type)
7097
7097
7098
7098
if (!(type -> tp_flags & Py_TPFLAGS_DISALLOW_INSTANTIATION )) {
7099
7099
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
+ }
7104
7106
}
7105
7107
}
7106
7108
else {
@@ -7174,7 +7176,7 @@ type_ready_post_checks(PyTypeObject *type)
7174
7176
7175
7177
7176
7178
static int
7177
- type_ready (PyTypeObject * type )
7179
+ type_ready (PyTypeObject * type , int rerunbuiltin )
7178
7180
{
7179
7181
_PyObject_ASSERT ((PyObject * )type ,
7180
7182
(type -> tp_flags & Py_TPFLAGS_READYING ) == 0 );
@@ -7203,29 +7205,33 @@ type_ready(PyTypeObject *type)
7203
7205
if (type_ready_mro (type ) < 0 ) {
7204
7206
goto error ;
7205
7207
}
7206
- if (type_ready_set_new (type ) < 0 ) {
7208
+ if (type_ready_set_new (type , rerunbuiltin ) < 0 ) {
7207
7209
goto error ;
7208
7210
}
7209
7211
if (type_ready_fill_dict (type ) < 0 ) {
7210
7212
goto error ;
7211
7213
}
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
+ }
7217
7221
}
7218
7222
if (type_ready_set_hash (type ) < 0 ) {
7219
7223
goto error ;
7220
7224
}
7221
7225
if (type_ready_add_subclasses (type ) < 0 ) {
7222
7226
goto error ;
7223
7227
}
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
+ }
7229
7235
}
7230
7236
7231
7237
/* All done -- set the ready flag */
@@ -7253,7 +7259,7 @@ PyType_Ready(PyTypeObject *type)
7253
7259
type -> tp_flags |= Py_TPFLAGS_IMMUTABLETYPE ;
7254
7260
}
7255
7261
7256
- return type_ready (type );
7262
+ return type_ready (type , 0 );
7257
7263
}
7258
7264
7259
7265
int
@@ -7264,37 +7270,26 @@ _PyStaticType_InitBuiltin(PyInterpreterState *interp, PyTypeObject *self)
7264
7270
assert (!(self -> tp_flags & Py_TPFLAGS_MANAGED_DICT ));
7265
7271
assert (!(self -> tp_flags & Py_TPFLAGS_MANAGED_WEAKREF ));
7266
7272
7267
- #ifndef NDEBUG
7268
7273
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 {
7271
7285
assert (!ismain );
7272
7286
assert (self -> tp_flags & _Py_TPFLAGS_STATIC_BUILTIN );
7273
7287
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 ;
7284
7288
}
7285
7289
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
-
7295
7290
static_builtin_state_init (interp , self );
7296
7291
7297
- int res = type_ready (self );
7292
+ int res = type_ready (self , ! ismain );
7298
7293
if (res < 0 ) {
7299
7294
static_builtin_state_clear (interp , self );
7300
7295
}
0 commit comments