Skip to content

Commit b3ab94a

Browse files
gh-135878: Fix crash in types.SimpleNamespace.__repr__ (#135889)
Co-authored-by: Peter Bierma <[email protected]>
1 parent e5f03b9 commit b3ab94a

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
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)