]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-142218: Fix split table dictionary crash (gh-142229) (gh-142245)
authorSam Gross <colesbury@gmail.com>
Thu, 4 Dec 2025 07:46:24 +0000 (02:46 -0500)
committerGitHub <noreply@github.com>
Thu, 4 Dec 2025 07:46:24 +0000 (07:46 +0000)
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)

Lib/test/test_dict.py
Misc/NEWS.d/next/Core and Builtins/2025-12-03-11-03-35.gh-issue-142218.44Fq_J.rst [new file with mode: 0644]
Objects/dictobject.c

index 5c3889550953ddd6e312b10a4b80626ee3309ac8..c7b6f64b53b42f19305e5e1efc1c7510c126af4a 100644 (file)
@@ -1674,6 +1674,14 @@ class DictTest(unittest.TestCase):
 
         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 (file)
index 0000000..a8ce0fc
--- /dev/null
@@ -0,0 +1,2 @@
+Fix crash when inserting into a split table dictionary with a non
+:class:`str` key that matches an existing key.
index 4a88e08d1da52ed07cae0b267bd30e606b999975..c987af31c45dd1c10d8e7f5bb5b4bf6be04b8568 100644 (file)
@@ -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];