From a9a99c75741667772741aa13538b727ae3c89358 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Tue, 24 Jun 2025 14:44:00 +0300 Subject: [PATCH 1/4] gh-135878: Fix crash in `types.SimpleNamespace.__repr__` --- .../Library/2025-06-24-14-43-24.gh-issue-135878.Db4roX.rst | 3 +++ Objects/namespaceobject.c | 5 ++--- 2 files changed, 5 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-06-24-14-43-24.gh-issue-135878.Db4roX.rst 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..df451243682f7f --- /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 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..871063b7a04b45 100644 --- a/Objects/namespaceobject.c +++ b/Objects/namespaceobject.c @@ -124,8 +124,7 @@ namespace_repr(PyObject *ns) if (PyUnicode_Check(key) && PyUnicode_GET_LENGTH(key) > 0) { PyObject *value, *item; - value = PyDict_GetItemWithError(d, key); - if (value != NULL) { + if (PyDict_GetItemRef(d, key, &value) == 1) { item = PyUnicode_FromFormat("%U=%R", key, value); if (item == NULL) { loop_error = 1; @@ -135,7 +134,7 @@ namespace_repr(PyObject *ns) Py_DECREF(item); } } - else if (PyErr_Occurred()) { + else { loop_error = 1; } } From eeb23d47e55a9fa5769faf8fcf1f386564c0de66 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Tue, 24 Jun 2025 15:00:46 +0300 Subject: [PATCH 2/4] Update Misc/NEWS.d/next/Library/2025-06-24-14-43-24.gh-issue-135878.Db4roX.rst Co-authored-by: Peter Bierma --- .../next/Library/2025-06-24-14-43-24.gh-issue-135878.Db4roX.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index df451243682f7f..969cf2dfa40878 100644 --- 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 @@ -1,3 +1,3 @@ -Fixes a crash of :class:`types.SimpleNamespace` on free-threading builds, +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. From 3e83d3a97f997ff4c69ccecff98a061d4875ff73 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Tue, 24 Jun 2025 15:38:55 +0300 Subject: [PATCH 3/4] Address review --- Objects/namespaceobject.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Objects/namespaceobject.c b/Objects/namespaceobject.c index 871063b7a04b45..36309e3e03f3b9 100644 --- a/Objects/namespaceobject.c +++ b/Objects/namespaceobject.c @@ -126,6 +126,7 @@ namespace_repr(PyObject *ns) if (PyDict_GetItemRef(d, key, &value) == 1) { item = PyUnicode_FromFormat("%U=%R", key, value); + Py_DECREF(value); if (item == NULL) { loop_error = 1; } From deb9223161f05ee2028fd21353b804f4665e954d Mon Sep 17 00:00:00 2001 From: sobolevn Date: Tue, 24 Jun 2025 16:02:29 +0300 Subject: [PATCH 4/4] Address review --- Objects/namespaceobject.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Objects/namespaceobject.c b/Objects/namespaceobject.c index 36309e3e03f3b9..0fc2bcea4cb06e 100644 --- a/Objects/namespaceobject.c +++ b/Objects/namespaceobject.c @@ -124,7 +124,8 @@ namespace_repr(PyObject *ns) if (PyUnicode_Check(key) && PyUnicode_GET_LENGTH(key) > 0) { PyObject *value, *item; - if (PyDict_GetItemRef(d, key, &value) == 1) { + 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) { @@ -135,7 +136,7 @@ namespace_repr(PyObject *ns) Py_DECREF(item); } } - else { + else if (has_key < 0) { loop_error = 1; } }