diff -r 2c81a883d8ca Lib/test/test_fileio.py --- a/Lib/test/test_fileio.py Sat Nov 07 11:17:15 2015 +0200 +++ b/Lib/test/test_fileio.py Sat Nov 07 14:26:38 2015 +0200 @@ -9,7 +9,8 @@ from array import array from weakref import proxy from functools import wraps -from test.support import TESTFN, check_warnings, run_unittest, make_bad_fd, cpython_only +from test.support import (TESTFN, check_warnings, run_unittest, make_bad_fd, + cpython_only, swap_attr) from collections import UserList import _io # C implementation of io @@ -175,6 +176,12 @@ class AutoFileTests: finally: os.close(fd) + def testRecursiveRepr(self): + # Issue #25455 + with swap_attr(self.f, 'name', self.f): + with self.assertRaises(RuntimeError): + repr(self.f) # Should not crash + def testErrors(self): f = self.f self.assertFalse(f.isatty()) diff -r 2c81a883d8ca Lib/test/test_io.py --- a/Lib/test/test_io.py Sat Nov 07 11:17:15 2015 +0200 +++ b/Lib/test/test_io.py Sat Nov 07 14:26:38 2015 +0200 @@ -829,6 +829,16 @@ class CommonBufferedTests: raw.name = b"dummy" self.assertEqual(repr(b), "<%s name=b'dummy'>" % clsname) + def test_recursive_repr(self): + # Issue #25455 + raw = self.MockRawIO() + b = self.tp(raw) + with support.swap_attr(raw, 'name', b): + try: + repr(b) # Should not crash + except RuntimeError: + pass + def test_flush_error_on_close(self): # Test that buffered file is closed despite failed flush # and that flush() is called before file closed. @@ -2230,6 +2240,16 @@ class TextIOWrapperTest(unittest.TestCas t.buffer.detach() repr(t) # Should not raise an exception + def test_recursive_repr(self): + # Issue #25455 + raw = self.BytesIO() + t = self.TextIOWrapper(raw) + with support.swap_attr(raw, 'name', t): + try: + repr(t) # Should not crash + except RuntimeError: + pass + def test_line_buffering(self): r = self.BytesIO() b = self.BufferedWriter(r, 1000) diff -r 2c81a883d8ca Modules/_io/bufferedio.c --- a/Modules/_io/bufferedio.c Sat Nov 07 11:17:15 2015 +0200 +++ b/Modules/_io/bufferedio.c Sat Nov 07 14:26:38 2015 +0200 @@ -1417,8 +1417,17 @@ buffered_repr(buffered *self) res = PyUnicode_FromFormat("<%s>", Py_TYPE(self)->tp_name); } else { - res = PyUnicode_FromFormat("<%s name=%R>", - Py_TYPE(self)->tp_name, nameobj); + int status = Py_ReprEnter((PyObject *)self); + res = NULL; + if (status == 0) { + res = PyUnicode_FromFormat("<%s name=%R>", + Py_TYPE(self)->tp_name, nameobj); + Py_ReprLeave((PyObject *)self); + } + else if (status > 0) + PyErr_Format(PyExc_RuntimeError, + "reentrant call inside %s.__repr__", + Py_TYPE(self)->tp_name); Py_DECREF(nameobj); } return res; diff -r 2c81a883d8ca Modules/_io/fileio.c --- a/Modules/_io/fileio.c Sat Nov 07 11:17:15 2015 +0200 +++ b/Modules/_io/fileio.c Sat Nov 07 14:26:38 2015 +0200 @@ -1069,9 +1069,18 @@ fileio_repr(fileio *self) self->fd, mode_string(self), self->closefd ? "True" : "False"); } else { - res = PyUnicode_FromFormat( - "<_io.FileIO name=%R mode='%s' closefd=%s>", - nameobj, mode_string(self), self->closefd ? "True" : "False"); + int status = Py_ReprEnter((PyObject *)self); + res = NULL; + if (status == 0) { + res = PyUnicode_FromFormat( + "<_io.FileIO name=%R mode='%s' closefd=%s>", + nameobj, mode_string(self), self->closefd ? "True" : "False"); + Py_ReprLeave((PyObject *)self); + } + else if (status > 0) + PyErr_Format(PyExc_RuntimeError, + "reentrant call inside %s.__repr__", + Py_TYPE(self)->tp_name); Py_DECREF(nameobj); } return res; diff -r 2c81a883d8ca Modules/_io/textio.c --- a/Modules/_io/textio.c Sat Nov 07 11:17:15 2015 +0200 +++ b/Modules/_io/textio.c Sat Nov 07 14:26:38 2015 +0200 @@ -2494,6 +2494,7 @@ static PyObject * textiowrapper_repr(textio *self) { PyObject *nameobj, *modeobj, *res, *s; + int status; CHECK_INITIALIZED(self); @@ -2501,6 +2502,14 @@ textiowrapper_repr(textio *self) if (res == NULL) return NULL; + status = Py_ReprEnter((PyObject *)self); + if (status != 0) { + if (status > 0) + PyErr_Format(PyExc_RuntimeError, + "reentrant call inside %s.__repr__", + Py_TYPE(self)->tp_name); + goto error; + } nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name); if (nameobj == NULL) { if (PyErr_ExceptionMatches(PyExc_Exception)) @@ -2539,6 +2548,8 @@ textiowrapper_repr(textio *self) return s; error: Py_XDECREF(res); + if (status == 0) + Py_ReprLeave((PyObject *)self); return NULL; }