diff options
author | Christian Heimes <christian@python.org> | 2022-08-22 05:24:03 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-22 05:24:03 (GMT) |
commit | 53e6a9a7254bdcd0538580ba7d799cd453e2dca5 (patch) | |
tree | 4f0d59c00859637c6b16159559545f38f7f342a5 /Objects | |
parent | d8c7a1174cc182668085b10aab4049f6a2794c2f (diff) | |
download | cpython-53e6a9a7254bdcd0538580ba7d799cd453e2dca5.zip cpython-53e6a9a7254bdcd0538580ba7d799cd453e2dca5.tar.gz cpython-53e6a9a7254bdcd0538580ba7d799cd453e2dca5.tar.bz2 |
gh-96046: Initialize ht_cached_keys in PyType_Ready() (GH-96047)
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/typeobject.c | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/Objects/typeobject.c b/Objects/typeobject.c index e61acd2..40d4294 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -3287,11 +3287,6 @@ type_new_impl(type_new_ctx *ctx) // Put the proper slots in place fixup_slot_dispatchers(type); - if (type->tp_flags & Py_TPFLAGS_MANAGED_DICT) { - PyHeapTypeObject *et = (PyHeapTypeObject*)type; - et->ht_cached_keys = _PyDict_NewKeysForClass(); - } - if (type_new_set_names(type) < 0) { goto error; } @@ -3836,10 +3831,6 @@ PyType_FromMetaclass(PyTypeObject *metaclass, PyObject *module, goto finally; } - if (type->tp_flags & Py_TPFLAGS_MANAGED_DICT) { - res->ht_cached_keys = _PyDict_NewKeysForClass(); - } - if (type->tp_doc) { PyObject *__doc__ = PyUnicode_FromString(_PyType_DocWithoutSignature(type->tp_name, type->tp_doc)); if (!__doc__) { @@ -6774,6 +6765,29 @@ type_ready_set_new(PyTypeObject *type) return 0; } +static int +type_ready_managed_dict(PyTypeObject *type) +{ + if (!(type->tp_flags & Py_TPFLAGS_MANAGED_DICT)) { + return 0; + } + if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { + PyErr_Format(PyExc_SystemError, + "type %s has the Py_TPFLAGS_MANAGED_DICT flag " + "but not Py_TPFLAGS_HEAPTYPE flag", + type->tp_name); + return -1; + } + PyHeapTypeObject* et = (PyHeapTypeObject*)type; + if (et->ht_cached_keys == NULL) { + et->ht_cached_keys = _PyDict_NewKeysForClass(); + if (et->ht_cached_keys == NULL) { + PyErr_NoMemory(); + return -1; + } + } + return 0; +} static int type_ready_post_checks(PyTypeObject *type) @@ -6852,6 +6866,9 @@ type_ready(PyTypeObject *type) if (type_ready_add_subclasses(type) < 0) { return -1; } + if (type_ready_managed_dict(type) < 0) { + return -1; + } if (type_ready_post_checks(type) < 0) { return -1; } |