@@ -3287,11 +3287,6 @@ type_new_impl(type_new_ctx *ctx)
3287
3287
// Put the proper slots in place
3288
3288
fixup_slot_dispatchers (type );
3289
3289
3290
- if (type -> tp_flags & Py_TPFLAGS_MANAGED_DICT ) {
3291
- PyHeapTypeObject * et = (PyHeapTypeObject * )type ;
3292
- et -> ht_cached_keys = _PyDict_NewKeysForClass ();
3293
- }
3294
-
3295
3290
if (type_new_set_names (type ) < 0 ) {
3296
3291
goto error ;
3297
3292
}
@@ -3836,10 +3831,6 @@ PyType_FromMetaclass(PyTypeObject *metaclass, PyObject *module,
3836
3831
goto finally ;
3837
3832
}
3838
3833
3839
- if (type -> tp_flags & Py_TPFLAGS_MANAGED_DICT ) {
3840
- res -> ht_cached_keys = _PyDict_NewKeysForClass ();
3841
- }
3842
-
3843
3834
if (type -> tp_doc ) {
3844
3835
PyObject * __doc__ = PyUnicode_FromString (_PyType_DocWithoutSignature (type -> tp_name , type -> tp_doc ));
3845
3836
if (!__doc__ ) {
@@ -6774,6 +6765,29 @@ type_ready_set_new(PyTypeObject *type)
6774
6765
return 0 ;
6775
6766
}
6776
6767
6768
+ static int
6769
+ type_ready_managed_dict (PyTypeObject * type )
6770
+ {
6771
+ if (!(type -> tp_flags & Py_TPFLAGS_MANAGED_DICT )) {
6772
+ return 0 ;
6773
+ }
6774
+ if (!(type -> tp_flags & Py_TPFLAGS_HEAPTYPE )) {
6775
+ PyErr_Format (PyExc_SystemError ,
6776
+ "type %s has the Py_TPFLAGS_MANAGED_DICT flag "
6777
+ "but not Py_TPFLAGS_HEAPTYPE flag" ,
6778
+ type -> tp_name );
6779
+ return -1 ;
6780
+ }
6781
+ PyHeapTypeObject * et = (PyHeapTypeObject * )type ;
6782
+ if (et -> ht_cached_keys == NULL ) {
6783
+ et -> ht_cached_keys = _PyDict_NewKeysForClass ();
6784
+ if (et -> ht_cached_keys == NULL ) {
6785
+ PyErr_NoMemory ();
6786
+ return -1 ;
6787
+ }
6788
+ }
6789
+ return 0 ;
6790
+ }
6777
6791
6778
6792
static int
6779
6793
type_ready_post_checks (PyTypeObject * type )
@@ -6852,6 +6866,9 @@ type_ready(PyTypeObject *type)
6852
6866
if (type_ready_add_subclasses (type ) < 0 ) {
6853
6867
return -1 ;
6854
6868
}
6869
+ if (type_ready_managed_dict (type ) < 0 ) {
6870
+ return -1 ;
6871
+ }
6855
6872
if (type_ready_post_checks (type ) < 0 ) {
6856
6873
return -1 ;
6857
6874
}
0 commit comments