Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-96046: Initialize ht_cached_keys in PyType_Ready() #96047

Merged
merged 10 commits into from
Aug 22, 2022
Prev Previous commit
Next Next commit
Move ht_cached_keys initialization into PyType_Ready()
  • Loading branch information
tiran committed Aug 17, 2022
commit 1fa05b37f1df034dc2377441742ce0e2ab3ed99d
43 changes: 27 additions & 16 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3285,11 +3285,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;
}
Expand Down Expand Up @@ -3834,10 +3829,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__) {
Expand Down Expand Up @@ -6772,6 +6763,30 @@ type_ready_set_new(PyTypeObject *type)
return 0;
}

static int
type_ready_managed_dict(PyTypeObject *type)
{
if (!(type->tp_flags & Py_TPFLAGS_MANAGED_DICT)) {
tiran marked this conversation as resolved.
Show resolved Hide resolved
tiran marked this conversation as resolved.
Show resolved Hide resolved
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.",
tiran marked this conversation as resolved.
Show resolved Hide resolved
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_Format(PyExc_SystemError,
tiran marked this conversation as resolved.
Show resolved Hide resolved
"failed to initialize ht_cached_keys of type %s.",
tiran marked this conversation as resolved.
Show resolved Hide resolved
type->tp_name);
return -1;
}
}
}

static int
type_ready_post_checks(PyTypeObject *type)
Expand All @@ -6795,13 +6810,6 @@ type_ready_post_checks(PyTypeObject *type)
type->tp_name);
return -1;
}
if (((PyHeapTypeObject*)type)->ht_cached_keys == NULL) {
PyErr_Format(PyExc_SystemError,
"type %s has the Py_TPFLAGS_MANAGED_DICT flag "
"but ht_cached_keys is not initialized.",
type->tp_name);
return -1;
}
}
else if (type->tp_dictoffset < (Py_ssize_t)sizeof(PyObject)) {
if (type->tp_dictoffset + type->tp_basicsize <= 0) {
Expand Down Expand Up @@ -6857,6 +6865,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;
}
Expand Down