Skip to content

Commit 756774e

Browse files
committed
gh-133441: Fix STORE_ATTR_WITH_HINT bytecode
Deoptimize if the dict is a dict subclass.
1 parent df1669e commit 756774e

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

Lib/test/test_dict.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,24 @@ def make_pairs():
15941594
self.assertEqual(d.get(key3_3), 44)
15951595
self.assertGreaterEqual(eq_count, 1)
15961596

1597+
def test_store_attr_with_hint(self):
1598+
# gh-133441: Regression test for STORE_ATTR_WITH_HINT bytecode
1599+
class Node:
1600+
def __init__(self):
1601+
self.parents = {}
1602+
1603+
def __setstate__(self, data_dict):
1604+
self.__dict__ = data_dict
1605+
self.parents = {}
1606+
1607+
class Dict(dict):
1608+
pass
1609+
1610+
obj = Node()
1611+
obj.__setstate__({'parents': {}})
1612+
obj.__setstate__({'parents': {}})
1613+
obj.__setstate__(Dict({'parents': {}}))
1614+
15971615

15981616
class CAPITest(unittest.TestCase):
15991617

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix ``STORE_ATTR_WITH_HINT`` bytecode: deoptimize if the dictionary is a
2+
dict subclass. 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)