diff options
author | Mark Shannon <mark@hotpy.org> | 2022-03-04 11:31:29 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-04 11:31:29 (GMT) |
commit | 03c2a36b2bd2d4469160d1607619ee144175d753 (patch) | |
tree | 828a98bada6b84ea722ac104da3b7e891f44d4cd /Objects/dictobject.c | |
parent | 8f31bf46980956c735dd18f9914f3e7144e87c77 (diff) | |
download | cpython-03c2a36b2bd2d4469160d1607619ee144175d753.zip cpython-03c2a36b2bd2d4469160d1607619ee144175d753.tar.gz cpython-03c2a36b2bd2d4469160d1607619ee144175d753.tar.bz2 |
bpo-46903: Handle str-subclasses in virtual instance dictionaries. (GH-31658)
Diffstat (limited to 'Objects/dictobject.c')
-rw-r--r-- | Objects/dictobject.c | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/Objects/dictobject.c b/Objects/dictobject.c index d8bf164..635a738 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -5427,23 +5427,26 @@ int _PyObject_StoreInstanceAttribute(PyObject *obj, PyDictValues *values, PyObject *name, PyObject *value) { - assert(PyUnicode_CheckExact(name)); PyDictKeysObject *keys = CACHED_KEYS(Py_TYPE(obj)); assert(keys != NULL); assert(values != NULL); assert(Py_TYPE(obj)->tp_flags & Py_TPFLAGS_MANAGED_DICT); - Py_ssize_t ix = insert_into_dictkeys(keys, name); + Py_ssize_t ix = DKIX_EMPTY; + if (PyUnicode_CheckExact(name)) { + ix = insert_into_dictkeys(keys, name); + } if (ix == DKIX_EMPTY) { - if (value == NULL) { - PyErr_SetObject(PyExc_AttributeError, name); - return -1; - } #ifdef Py_STATS - if (shared_keys_usable_size(keys) == SHARED_KEYS_MAX_SIZE) { - OBJECT_STAT_INC(dict_materialized_too_big); + if (PyUnicode_CheckExact(name)) { + if (shared_keys_usable_size(keys) == SHARED_KEYS_MAX_SIZE) { + OBJECT_STAT_INC(dict_materialized_too_big); + } + else { + OBJECT_STAT_INC(dict_materialized_new_key); + } } else { - OBJECT_STAT_INC(dict_materialized_new_key); + OBJECT_STAT_INC(dict_materialized_str_subclass); } #endif PyObject *dict = make_dict_from_instance_attributes(keys, values); @@ -5452,7 +5455,12 @@ _PyObject_StoreInstanceAttribute(PyObject *obj, PyDictValues *values, } *_PyObject_ValuesPointer(obj) = NULL; *_PyObject_ManagedDictPointer(obj) = dict; - return PyDict_SetItem(dict, name, value); + if (value == NULL) { + return PyDict_DelItem(dict, name); + } + else { + return PyDict_SetItem(dict, name, value); + } } PyObject *old_value = values->values[ix]; Py_XINCREF(value); |