Index: Objects/object.c =================================================================== --- Objects/object.c (revision 57711) +++ Objects/object.c (working copy) @@ -359,7 +359,11 @@ Py_Type(v)->tp_name, v); else { PyObject *res; + if (Py_EnterRecursiveCall(" in repr")) { + return NULL; + } res = (*Py_Type(v)->tp_repr)(v); + Py_LeaveRecursiveCall(); if (res == NULL) return NULL; #ifdef Py_USING_UNICODE @@ -424,7 +428,11 @@ PyObject * PyObject_Str(PyObject *v) { + if (Py_EnterRecursiveCall(" in str")) { + return NULL; + } PyObject *res = _PyObject_Str(v); + Py_LeaveRecursiveCall(); if (res == NULL) return NULL; #ifdef Py_USING_UNICODE Index: Lib/test/test_exceptions.py =================================================================== --- Lib/test/test_exceptions.py (revision 57711) +++ Lib/test/test_exceptions.py (working copy) @@ -343,6 +343,13 @@ self.failUnless(str(Exception('a'))) self.failUnless(unicode(Exception(u'a'))) + def testStrReprInfiniteRecursion(self): + class A(Exception): + def __init__(self): + Exception.__init__(self, self) + a = A() + self.assertRaises(RuntimeError, str, a) + self.assertRaises(RuntimeError, repr, a) def test_main(): run_unittest(ExceptionTests)