Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Lib/test/list_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ def test_repr(self):
self.assertEqual(str(a2), "[0, 1, 2, [...], 3]")
self.assertEqual(repr(a2), "[0, 1, 2, [...], 3]")

l0 = []
for i in xrange(sys.getrecursionlimit() + 100):
l0 = [l0]
self.assertRaises(RuntimeError, repr, l0)
def test_repr_deep(self):
a = self.type2test([])
for i in range(sys.getrecursionlimit() + 100):
a = self.type2test([a])
self.assertRaises(RuntimeError, repr, a)

def test_print(self):
d = self.type2test(xrange(200))
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/mapping_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest
import UserDict
import test_support
import sys


class BasicTestMappingProtocol(unittest.TestCase):
Expand Down Expand Up @@ -645,6 +646,14 @@ def __repr__(self):
d = self._full_mapping({1: BadRepr()})
self.assertRaises(Exc, repr, d)

def test_repr_deep(self):
d = self._empty_mapping()
for i in range(sys.getrecursionlimit() + 100):
d0 = d
d = self._empty_mapping()
d[1] = d0
self.assertRaises(RuntimeError, repr, d)

def test_le(self):
self.assertTrue(not (self._empty_mapping() < self._empty_mapping()))
self.assertTrue(not (self._full_mapping({1: 2}) < self._full_mapping({1L: 2L})))
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import UserDict, random, string
import gc, weakref
import sys


class DictTest(unittest.TestCase):
Expand Down Expand Up @@ -422,6 +423,12 @@ def __repr__(self):
d = {1: BadRepr()}
self.assertRaises(Exc, repr, d)

def test_repr_deep(self):
d = {}
for i in range(sys.getrecursionlimit() + 100):
d = {1: d}
self.assertRaises(RuntimeError, repr, d)

def test_le(self):
self.assertFalse({} < {})
self.assertFalse({1: 2} < {1L: 2L})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The repr of deeply nested dict now raises a RecursionError instead of
crashing due to a stack overflow.
3 changes: 0 additions & 3 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,7 @@ list_repr(PyListObject *v)
so must refetch the list size on each iteration. */
for (i = 0; i < Py_SIZE(v); ++i) {
int status;
if (Py_EnterRecursiveCall(" while getting the repr of a list"))
goto Done;
s = PyObject_Repr(v->ob_item[i]);
Py_LeaveRecursiveCall();
if (s == NULL)
goto Done;
status = PyList_Append(pieces, s);
Expand Down
5 changes: 5 additions & 0 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,12 @@ PyObject_Repr(PyObject *v)
Py_TYPE(v)->tp_name, v);
else {
PyObject *res;
/* It is possible for a type to have a tp_repr representation that
loops infinitely. */
if (Py_EnterRecursiveCall(" while getting the repr of an object"))
return NULL;
res = (*Py_TYPE(v)->tp_repr)(v);
Py_LeaveRecursiveCall();
if (res == NULL)
return NULL;
#ifdef Py_USING_UNICODE
Expand Down
3 changes: 0 additions & 3 deletions Objects/tupleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,7 @@ tuplerepr(PyTupleObject *v)

/* Do repr() on each element. */
for (i = 0; i < n; ++i) {
if (Py_EnterRecursiveCall(" while getting the repr of a tuple"))
goto Done;
s = PyObject_Repr(v->ob_item[i]);
Py_LeaveRecursiveCall();
if (s == NULL)
goto Done;
PyTuple_SET_ITEM(pieces, i, s);
Expand Down