https://github.com/python/cpython/commit/b3ab94acd308591bbdf264f1722fedc7ee25d6fa
commit: b3ab94acd308591bbdf264f1722fedc7ee25d6fa
branch: main
author: sobolevn <m...@sobolevn.me>
committer: sobolevn <m...@sobolevn.me>
date: 2025-06-24T19:33:25+03:00
summary:

gh-135878: Fix crash in `types.SimpleNamespace.__repr__` (#135889)

Co-authored-by: Peter Bierma <zintensity...@gmail.com>

files:
A Misc/NEWS.d/next/Library/2025-06-24-14-43-24.gh-issue-135878.Db4roX.rst
M 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
index 00000000000000..969cf2dfa40878
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-06-24-14-43-24.gh-issue-135878.Db4roX.rst
@@ -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.
diff --git a/Objects/namespaceobject.c b/Objects/namespaceobject.c
index caebe6bf543567..0fc2bcea4cb06e 100644
--- a/Objects/namespaceobject.c
+++ b/Objects/namespaceobject.c
@@ -124,9 +124,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;
                 }
@@ -135,7 +136,7 @@ namespace_repr(PyObject *ns)
                     Py_DECREF(item);
                 }
             }
-            else if (PyErr_Occurred()) {
+            else if (has_key < 0) {
                 loop_error = 1;
             }
         }

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: arch...@mail-archive.com

Reply via email to