@@ -3202,11 +3202,6 @@ type_new_impl(type_new_ctx *ctx)
3202
3202
// Put the proper slots in place
3203
3203
fixup_slot_dispatchers (type );
3204
3204
3205
- if (type -> tp_flags & Py_TPFLAGS_MANAGED_DICT ) {
3206
- PyHeapTypeObject * et = (PyHeapTypeObject * )type ;
3207
- et -> ht_cached_keys = _PyDict_NewKeysForClass ();
3208
- }
3209
-
3210
3205
if (type_new_set_names (type ) < 0 ) {
3211
3206
goto error ;
3212
3207
}
@@ -3588,10 +3583,6 @@ PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
3588
3583
if (PyType_Ready (type ) < 0 )
3589
3584
goto fail ;
3590
3585
3591
- if (type -> tp_flags & Py_TPFLAGS_MANAGED_DICT ) {
3592
- res -> ht_cached_keys = _PyDict_NewKeysForClass ();
3593
- }
3594
-
3595
3586
if (type -> tp_doc ) {
3596
3587
PyObject * __doc__ = PyUnicode_FromString (_PyType_DocWithoutSignature (type -> tp_name , type -> tp_doc ));
3597
3588
if (!__doc__ )
@@ -6409,6 +6400,29 @@ type_ready_set_new(PyTypeObject *type)
6409
6400
return 0 ;
6410
6401
}
6411
6402
6403
+ static int
6404
+ type_ready_managed_dict (PyTypeObject * type )
6405
+ {
6406
+ if (!(type -> tp_flags & Py_TPFLAGS_MANAGED_DICT )) {
6407
+ return 0 ;
6408
+ }
6409
+ if (!(type -> tp_flags & Py_TPFLAGS_HEAPTYPE )) {
6410
+ PyErr_Format (PyExc_SystemError ,
6411
+ "type %s has the Py_TPFLAGS_MANAGED_DICT flag "
6412
+ "but not Py_TPFLAGS_HEAPTYPE flag" ,
6413
+ type -> tp_name );
6414
+ return -1 ;
6415
+ }
6416
+ PyHeapTypeObject * et = (PyHeapTypeObject * )type ;
6417
+ if (et -> ht_cached_keys == NULL ) {
6418
+ et -> ht_cached_keys = _PyDict_NewKeysForClass ();
6419
+ if (et -> ht_cached_keys == NULL ) {
6420
+ PyErr_NoMemory ();
6421
+ return -1 ;
6422
+ }
6423
+ }
6424
+ return 0 ;
6425
+ }
6412
6426
6413
6427
static int
6414
6428
type_ready_post_checks (PyTypeObject * type )
@@ -6469,6 +6483,9 @@ type_ready(PyTypeObject *type)
6469
6483
if (type_ready_add_subclasses (type ) < 0 ) {
6470
6484
return -1 ;
6471
6485
}
6486
+ if (type_ready_managed_dict (type ) < 0 ) {
6487
+ return -1 ;
6488
+ }
6472
6489
if (type_ready_post_checks (type ) < 0 ) {
6473
6490
return -1 ;
6474
6491
}
0 commit comments