Skip to content

Commit 5cd56b2

Browse files
[3.13] gh-133441: Fix STORE_ATTR_WITH_HINT bytecode (#133446)
Deoptimize if the dict is a dict subclass. Co-authored-by: Peter Bierma <[email protected]>
1 parent 927da99 commit 5cd56b2

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

Lib/test/test_opcache.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,24 @@ class D(dict): pass
11551155
{'a':1, 'b':2}
11561156
)
11571157

1158+
def test_store_attr_with_hint(self):
1159+
# gh-133441: Regression test for STORE_ATTR_WITH_HINT bytecode
1160+
class Node:
1161+
def __init__(self):
1162+
self.parents = {}
1163+
1164+
def __setstate__(self, data_dict):
1165+
self.__dict__ = data_dict
1166+
self.parents = {}
1167+
1168+
class Dict(dict):
1169+
pass
1170+
1171+
obj = Node()
1172+
obj.__setstate__({'parents': {}})
1173+
obj.__setstate__({'parents': {}})
1174+
obj.__setstate__(Dict({'parents': {}}))
1175+
11581176

11591177
if __name__ == "__main__":
11601178
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix crash upon setting an attribute with a :class:`dict` subclass.
2+
Patch by Victor Stinner.

Python/bytecodes.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2149,7 +2149,8 @@ dummy_func(
21492149
assert(tp->tp_flags & Py_TPFLAGS_MANAGED_DICT);
21502150
PyDictObject *dict = _PyObject_GetManagedDict(owner);
21512151
DEOPT_IF(dict == NULL);
2152-
assert(PyDict_CheckExact((PyObject *)dict));
2152+
DEOPT_IF(!PyDict_CheckExact((PyObject *)dict));
2153+
21532154
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
21542155
DEOPT_IF(hint >= (size_t)dict->ma_keys->dk_nentries);
21552156
PyObject *old_value;

Python/generated_cases.c.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)