tuple.__repr__ did not consider a reference loop as it is not possible from
Python code; but it is possible from C. object.__str__ had the issue of not
expecting a type to doing something within it's tp_str implementation that
could trigger an infinite recursion, but it could in C code.. Both found
thanks to BaseException and how it handles its repr.
Closes issue #1686386. Thanks to Thomas Herve for taking an initial stab at
coming up with a solution.
diff --git a/Objects/object.c b/Objects/object.c
index 0651d6b6..e75a03d 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -408,7 +408,12 @@
if (Py_Type(v)->tp_str == NULL)
return PyObject_Repr(v);
+ /* It is possible for a type to have a tp_str representation that loops
+ infinitely. */
+ if (Py_EnterRecursiveCall(" while getting the str of an object"))
+ return NULL;
res = (*Py_Type(v)->tp_str)(v);
+ Py_LeaveRecursiveCall();
if (res == NULL)
return NULL;
type_ok = PyString_Check(res);