Skip to content

Commit

Permalink
Access the correct dict
Browse files Browse the repository at this point in the history
  • Loading branch information
Fidget-Spinner committed Dec 16, 2022
1 parent e209b9e commit 1aecb3c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
6 changes: 4 additions & 2 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2738,8 +2738,10 @@ dummy_func(
assert(self_cls->tp_flags & Py_TPFLAGS_MANAGED_DICT);
PyDictOrValues dorv = *_PyObject_DictOrValuesPointer(self);
DEOPT_IF(_PyDictOrValues_IsValues(dorv), LOAD_ATTR);
PyDictKeysObject *keys = ((PyHeapTypeObject *)self_cls)->ht_cached_keys;
DEOPT_IF(keys->dk_version != read_u32(cache->keys_version), LOAD_ATTR);
PyObject *dict = _PyDictOrValues_GetDict(dorv);
PyDictKeysObject *keys = (dict == NULL) ? NULL : ((PyDictObject *)dict)->ma_keys;
// Note: cache->keys_version can be 0 when dict is NULL.
DEOPT_IF(keys != NULL && keys->dk_version != read_u32(cache->keys_version), LOAD_ATTR);
STAT_INC(LOAD_ATTR, hit);
PyObject *res = read_obj(cache->descr);
assert(res != NULL);
Expand Down
6 changes: 4 additions & 2 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -1014,11 +1014,13 @@ PyObject *descr, DescriptorClassification kind)
PyDictKeysObject *keys;
if (owner_cls->tp_flags & Py_TPFLAGS_MANAGED_DICT) {
PyDictOrValues dorv = *_PyObject_DictOrValuesPointer(owner);
keys = ((PyHeapTypeObject *)owner_cls)->ht_cached_keys;
if (_PyDictOrValues_IsValues(dorv)) {
keys = ((PyHeapTypeObject *)owner_cls)->ht_cached_keys;
dictkind = MANAGED_VALUES;
}
else {
PyDictObject *dict = (PyDictObject *)_PyDictOrValues_GetDict(dorv);
keys = dict != NULL ? dict->ma_keys : NULL;
// User has directly accessed __dict__.
dictkind = MANAGED_DICT;
}
Expand Down Expand Up @@ -1046,7 +1048,7 @@ PyObject *descr, DescriptorClassification kind)
}
}
}
if (dictkind == MANAGED_VALUES || dictkind == OFFSET_DICT || dictkind == MANAGED_DICT) {
if (dictkind == MANAGED_VALUES || dictkind == OFFSET_DICT || (dictkind == MANAGED_DICT && keys != NULL)) {
Py_ssize_t index = _PyDictKeys_StringLookup(keys, name);
if (index != DKIX_EMPTY) {
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_SHADOWED);
Expand All @@ -1067,6 +1069,9 @@ PyObject *descr, DescriptorClassification kind)
_py_set_opcode(instr, LOAD_ATTR_METHOD_WITH_VALUES);
break;
case MANAGED_DICT:
if (keys == NULL) {
write_u32(cache->keys_version, 0);
}
_py_set_opcode(instr, LOAD_ATTR_METHOD_MANAGED_DICT);
break;
case OFFSET_DICT:
Expand Down

0 comments on commit 1aecb3c

Please sign in to comment.