mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
[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 b3ab94acd3)
Co-authored-by: sobolevn <mail@sobolevn.me>
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
This commit is contained in:
parent
3cdb659a0c
commit
88c55528f5
2 changed files with 7 additions and 3 deletions
|
|
@ -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.
|
||||||
|
|
@ -124,9 +124,10 @@ namespace_repr(PyObject *ns)
|
||||||
if (PyUnicode_Check(key) && PyUnicode_GET_LENGTH(key) > 0) {
|
if (PyUnicode_Check(key) && PyUnicode_GET_LENGTH(key) > 0) {
|
||||||
PyObject *value, *item;
|
PyObject *value, *item;
|
||||||
|
|
||||||
value = PyDict_GetItemWithError(d, key);
|
int has_key = PyDict_GetItemRef(d, key, &value);
|
||||||
if (value != NULL) {
|
if (has_key == 1) {
|
||||||
item = PyUnicode_FromFormat("%U=%R", key, value);
|
item = PyUnicode_FromFormat("%U=%R", key, value);
|
||||||
|
Py_DECREF(value);
|
||||||
if (item == NULL) {
|
if (item == NULL) {
|
||||||
loop_error = 1;
|
loop_error = 1;
|
||||||
}
|
}
|
||||||
|
|
@ -135,7 +136,7 @@ namespace_repr(PyObject *ns)
|
||||||
Py_DECREF(item);
|
Py_DECREF(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (PyErr_Occurred()) {
|
else if (has_key < 0) {
|
||||||
loop_error = 1;
|
loop_error = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue