https://github.com/python/cpython/commit/1a75c0fb054db6910fc2b74c99911329b80e25b5
commit: 1a75c0fb054db6910fc2b74c99911329b80e25b5
branch: 3.13
author: Sam Gross <[email protected]>
committer: colesbury <[email protected]>
date: 2025-12-04T07:46:24Z
summary:
[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 547d8daf780646e2800bec598ed32085817c8606)
files:
A Misc/NEWS.d/next/Core and
Builtins/2025-12-03-11-03-35.gh-issue-142218.44Fq_J.rst
M Lib/test/test_dict.py
M Objects/dictobject.c
diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py
index 5c3889550953dd..c7b6f64b53b42f 100644
--- a/Lib/test/test_dict.py
+++ b/Lib/test/test_dict.py
@@ -1674,6 +1674,14 @@ def __eq__(self, other):
self.assertEqual(len(d), 1)
+ def test_split_table_update_with_str_subclass(self):
+ class MyStr(str): pass
+ class MyClass: pass
+ obj = MyClass()
+ obj.attr = 1
+ obj.__dict__[MyStr('attr')] = 2
+ self.assertEqual(obj.attr, 2)
+
class CAPITest(unittest.TestCase):
diff --git a/Misc/NEWS.d/next/Core and
Builtins/2025-12-03-11-03-35.gh-issue-142218.44Fq_J.rst b/Misc/NEWS.d/next/Core
and Builtins/2025-12-03-11-03-35.gh-issue-142218.44Fq_J.rst
new file mode 100644
index 00000000000000..a8ce0fc65267d5
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and
Builtins/2025-12-03-11-03-35.gh-issue-142218.44Fq_J.rst
@@ -0,0 +1,2 @@
+Fix crash when inserting into a split table dictionary with a non
+:class:`str` key that matches an existing key.
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index 4a88e08d1da52e..c987af31c45dd1 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -1873,10 +1873,14 @@ insertdict(PyInterpreterState *interp, PyDictObject *mp,
uint64_t new_version = _PyDict_NotifyEvent(
interp, PyDict_EVENT_MODIFIED, mp, key, value);
assert(old_value != NULL);
- assert(!_PyDict_HasSplitTable(mp));
if (DK_IS_UNICODE(mp->ma_keys)) {
- PyDictUnicodeEntry *ep = &DK_UNICODE_ENTRIES(mp->ma_keys)[ix];
- STORE_VALUE(ep, value);
+ if (_PyDict_HasSplitTable(mp)) {
+ STORE_SPLIT_VALUE(mp, ix, value);
+ }
+ else {
+ PyDictUnicodeEntry *ep = &DK_UNICODE_ENTRIES(mp->ma_keys)[ix];
+ STORE_VALUE(ep, value);
+ }
}
else {
PyDictKeyEntry *ep = &DK_ENTRIES(mp->ma_keys)[ix];
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]