8000 [3.14] gh-135878: Fix crash in `types.SimpleNamespace.__repr__` (GH-1… · python/cpython@88c5552 · GitHub
[go: up one dir, main page]

Skip to content

Commit 88c5552

Browse files
miss-islingtonsobolevnZeroIntensity
authored
[3.14] gh-135878: Fix crash in types.SimpleNamespace.__repr__ (GH-135889) (#135896)
gh-135878: Fix crash in `types.SimpleNamespace.__repr__` (GH-135889) (cherry picked from commit b3ab94a) Co-authored-by: sobolevn <mail@sobolevn.me> Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
1 parent 3cdb659 commit 88c5552

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixes a crash of :class:`types.SimpleNamespace` on :term:`free threading` builds,
2+
when several threads were calling its :meth:`~object.__repr__` method at the
3+
same time.

Objects/namespaceobject.c

Lines changed: 4 additions & 3 deletions
7A60
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,10 @@ namespace_repr(PyObject *ns)
124124
if (PyUnicode_Check(key) && PyUnicode_GET_LENGTH(key) > 0) {
125125
PyObject *value, *item;
126126

127-
value = PyDict_GetItemWithError(d, key);
128-
if (value != NULL) {
127+
int has_key = PyDict_GetItemRef(d, key, &value);
128+
if (has_key == 1) {
129129
item = PyUnicode_FromFormat("%U=%R", key, value);
130+
Py_DECREF(value);
130131
if (item == NULL) {
131132
loop_error = 1;
132133
}
@@ -135,7 +136,7 @@ namespace_repr(PyObject *ns)
135136
Py_DECREF(item);
136137
}
137138
}
138-
else if (PyErr_Occurred()) {
139+
else if (has_key < 0) {
139140
loop_error = 1;
140141
}
141142
}

0 commit comments

Comments
 (0)
0