Skip to content

Commit 1a75c0f

Browse files
authored
[3.13] gh-142218: Fix split table dictionary crash (gh-142229) (gh-142245)
This fixes a regression introduced in gh-140558. The interpreter would crash if we inserted a non `str` key into a split table that matches an existing key. (cherry picked from commit 547d8da)
1 parent 03cceb1 commit 1a75c0f

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

Lib/test/test_dict.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,14 @@ def __eq__(self, other):
16741674

16751675
self.assertEqual(len(d), 1)
16761676

1677+
def test_split_table_update_with_str_subclass(self):
1678+
class MyStr(str): pass
1679+
class MyClass: pass
1680+
obj = MyClass()
1681+
obj.attr = 1
1682+
obj.__dict__[MyStr('attr')] = 2
1683+
self.assertEqual(obj.attr, 2)
1684+
16771685

16781686
class CAPITest(unittest.TestCase):
16791687

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix crash when inserting into a split table dictionary with a non
2+
:class:`str` key that matches an existing key.

Objects/dictobject.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,10 +1873,14 @@ insertdict(PyInterpreterState *interp, PyDictObject *mp,
18731873
uint64_t new_version = _PyDict_NotifyEvent(
18741874
interp, PyDict_EVENT_MODIFIED, mp, key, value);
18751875
assert(old_value != NULL);
1876-
assert(!_PyDict_HasSplitTable(mp));
18771876
if (DK_IS_UNICODE(mp->ma_keys)) {
1878-
PyDictUnicodeEntry *ep = &DK_UNICODE_ENTRIES(mp->ma_keys)[ix];
1879-
STORE_VALUE(ep, value);
1877+
if (_PyDict_HasSplitTable(mp)) {
1878+
STORE_SPLIT_VALUE(mp, ix, value);
1879+
}
1880+
else {
1881+
PyDictUnicodeEntry *ep = &DK_UNICODE_ENTRIES(mp->ma_keys)[ix];
1882+
STORE_VALUE(ep, value);
1883+
}
18801884
}
18811885
else {
18821886
PyDictKeyEntry *ep = &DK_ENTRIES(mp->ma_keys)[ix];

0 commit comments

Comments
 (0)