bpo-18533: Avoid RuntimeError from repr() of recursive dictview (#4823) (#5357)
(cherry picked from commit d7773d92bd11640a8c950d6c36a9cef1cee36f96)
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index a792b2d..c544ecd 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -3005,21 +3005,29 @@
{
PyObject *seq;
PyObject *seq_str;
- PyObject *result;
+ PyObject *result = NULL;
+ Py_ssize_t rc;
+ rc = Py_ReprEnter((PyObject *)dv);
+ if (rc != 0) {
+ return rc > 0 ? PyString_FromString("...") : NULL;
+ }
seq = PySequence_List((PyObject *)dv);
- if (seq == NULL)
- return NULL;
-
+ if (seq == NULL) {
+ goto Done;
+ }
seq_str = PyObject_Repr(seq);
+ Py_DECREF(seq);
+
if (seq_str == NULL) {
- Py_DECREF(seq);
- return NULL;
+ goto Done;
}
result = PyString_FromFormat("%s(%s)", Py_TYPE(dv)->tp_name,
PyString_AS_STRING(seq_str));
Py_DECREF(seq_str);
- Py_DECREF(seq);
+
+Done:
+ Py_ReprLeave((PyObject *)dv);
return result;
}