Skip to content

bpo-32137: The repr of deeply nested dict now raises a RecursionError #4570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
7 changes: 4 additions & 3 deletions Lib/test/list_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ def test_repr(self):
self.assertEqual(str(a2), "[0, 1, 2, [...], 3]")
self.assertEqual(repr(a2), "[0, 1, 2, [...], 3]")

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

def test_print(self):
d = self.type2test(range(200))
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/mapping_tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# tests common to dict and UserDict
import unittest
import collections
import sys


class BasicTestMappingProtocol(unittest.TestCase):
Expand Down Expand Up @@ -619,6 +620,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(RecursionError, repr, d)

def test_eq(self):
self.assertEqual(self._empty_mapping(), self._empty_mapping())
self.assertEqual(self._full_mapping({1: 2}),
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,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(RecursionError, repr, d)

def test_eq(self):
self.assertEqual({}, {})
self.assertEqual({1: 2}, {1: 2})
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 @@ -364,10 +364,7 @@ list_repr(PyListObject *v)
goto error;
}

if (Py_EnterRecursiveCall(" while getting the repr of a list"))
goto error;
s = PyObject_Repr(v->ob_item[i]);
Py_LeaveRecursiveCall();
if (s == NULL)
goto error;

Expand Down
5 changes: 5 additions & 0 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,12 @@ PyObject_Repr(PyObject *v)
assert(!PyErr_Occurred());
#endif

/* 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 = (*v->ob_type->tp_repr)(v);
Py_LeaveRecursiveCall();
if (res == NULL)
return NULL;
if (!PyUnicode_Check(res)) {
Expand Down
3 changes: 0 additions & 3 deletions Objects/tupleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,7 @@ tuplerepr(PyTupleObject *v)
goto error;
}

if (Py_EnterRecursiveCall(" while getting the repr of a tuple"))
goto error;
s = PyObject_Repr(v->ob_item[i]);
Py_LeaveRecursiveCall();
if (s == NULL)
goto error;

Expand Down