Skip to content

Commit 1aecb3c

Browse files
Access the correct dict
1 parent e209b9e commit 1aecb3c

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

Python/bytecodes.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2738,8 +2738,10 @@ dummy_func(
27382738
assert(self_cls->tp_flags & Py_TPFLAGS_MANAGED_DICT);
27392739
PyDictOrValues dorv = *_PyObject_DictOrValuesPointer(self);
27402740
DEOPT_IF(_PyDictOrValues_IsValues(dorv), LOAD_ATTR);
2741-
PyDictKeysObject *keys = ((PyHeapTypeObject *)self_cls)->ht_cached_keys;
2742-
DEOPT_IF(keys->dk_version != read_u32(cache->keys_version), LOAD_ATTR);
2741+
PyObject *dict = _PyDictOrValues_GetDict(dorv);
2742+
PyDictKeysObject *keys = (dict == NULL) ? NULL : ((PyDictObject *)dict)->ma_keys;
2743+
// Note: cache->keys_version can be 0 when dict is NULL.
2744+
DEOPT_IF(keys != NULL && keys->dk_version != read_u32(cache->keys_version), LOAD_ATTR);
27432745
STAT_INC(LOAD_ATTR, hit);
27442746
PyObject *res = read_obj(cache->descr);
27452747
assert(res != NULL);

Python/generated_cases.c.h

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/specialize.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,11 +1014,13 @@ PyObject *descr, DescriptorClassification kind)
10141014
PyDictKeysObject *keys;
10151015
if (owner_cls->tp_flags & Py_TPFLAGS_MANAGED_DICT) {
10161016
PyDictOrValues dorv = *_PyObject_DictOrValuesPointer(owner);
1017-
keys = ((PyHeapTypeObject *)owner_cls)->ht_cached_keys;
10181017
if (_PyDictOrValues_IsValues(dorv)) {
1018+
keys = ((PyHeapTypeObject *)owner_cls)->ht_cached_keys;
10191019
dictkind = MANAGED_VALUES;
10201020
}
10211021
else {
1022+
PyDictObject *dict = (PyDictObject *)_PyDictOrValues_GetDict(dorv);
1023+
keys = dict != NULL ? dict->ma_keys : NULL;
10221024
// User has directly accessed __dict__.
10231025
dictkind = MANAGED_DICT;
10241026
}
@@ -1046,7 +1048,7 @@ PyObject *descr, DescriptorClassification kind)
10461048
}
10471049
}
10481050
}
1049-
if (dictkind == MANAGED_VALUES || dictkind == OFFSET_DICT || dictkind == MANAGED_DICT) {
1051+
if (dictkind == MANAGED_VALUES || dictkind == OFFSET_DICT || (dictkind == MANAGED_DICT && keys != NULL)) {
10501052
Py_ssize_t index = _PyDictKeys_StringLookup(keys, name);
10511053
if (index != DKIX_EMPTY) {
10521054
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_SHADOWED);
@@ -1067,6 +1069,9 @@ PyObject *descr, DescriptorClassification kind)
10671069
_py_set_opcode(instr, LOAD_ATTR_METHOD_WITH_VALUES);
10681070
break;
10691071
case MANAGED_DICT:
1072+
if (keys == NULL) {
1073+
write_u32(cache->keys_version, 0);
1074+
}
10701075
_py_set_opcode(instr, LOAD_ATTR_METHOD_MANAGED_DICT);
10711076
break;
10721077
case OFFSET_DICT:

0 commit comments

Comments
 (0)