]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-135878: Fix crash in `types.SimpleNamespace.__repr__` (GH-135889) (#135895)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 24 Jun 2025 16:59:29 +0000 (18:59 +0200)
committerGitHub <noreply@github.com>
Tue, 24 Jun 2025 16:59:29 +0000 (16:59 +0000)
gh-135878: Fix crash in `types.SimpleNamespace.__repr__` (GH-135889)
(cherry picked from commit b3ab94acd308591bbdf264f1722fedc7ee25d6fa)

Co-authored-by: sobolevn <mail@sobolevn.me>
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Misc/NEWS.d/next/Library/2025-06-24-14-43-24.gh-issue-135878.Db4roX.rst [new file with mode: 0644]
Objects/namespaceobject.c

diff --git a/Misc/NEWS.d/next/Library/2025-06-24-14-43-24.gh-issue-135878.Db4roX.rst b/Misc/NEWS.d/next/Library/2025-06-24-14-43-24.gh-issue-135878.Db4roX.rst
new file mode 100644 (file)
index 0000000..969cf2d
--- /dev/null
@@ -0,0 +1,3 @@
+Fixes a crash of :class:`types.SimpleNamespace` on :term:`free threading` builds,
+when several threads were calling its :meth:`~object.__repr__` method at the
+same time.
index 4ef3bd92f5a56968cf490817f1a60e54c2756637..7eeac11b5538106a38256763682ea1088f7366fe 100644 (file)
@@ -120,9 +120,10 @@ namespace_repr(PyObject *ns)
         if (PyUnicode_Check(key) && PyUnicode_GET_LENGTH(key) > 0) {
             PyObject *value, *item;
 
-            value = PyDict_GetItemWithError(d, key);
-            if (value != NULL) {
+            int has_key = PyDict_GetItemRef(d, key, &value);
+            if (has_key == 1) {
                 item = PyUnicode_FromFormat("%U=%R", key, value);
+                Py_DECREF(value);
                 if (item == NULL) {
                     loop_error = 1;
                 }
@@ -131,7 +132,7 @@ namespace_repr(PyObject *ns)
                     Py_DECREF(item);
                 }
             }
-            else if (PyErr_Occurred()) {
+            else if (has_key < 0) {
                 loop_error = 1;
             }
         }