[2.7] bpo-32137: The repr of deeply nested dict now raises a RuntimeError (GH-4570) (#5493)
instead of crashing due to a stack overflow.
This perhaps will fix similar problems in other extension types.
(cherry picked from commit 1fb72d2ad2
)
This commit is contained in:
parent
b60f43a0e6
commit
b7a2c17be8
|
@ -45,10 +45,11 @@ class CommonTest(seq_tests.CommonTest):
|
||||||
self.assertEqual(str(a2), "[0, 1, 2, [...], 3]")
|
self.assertEqual(str(a2), "[0, 1, 2, [...], 3]")
|
||||||
self.assertEqual(repr(a2), "[0, 1, 2, [...], 3]")
|
self.assertEqual(repr(a2), "[0, 1, 2, [...], 3]")
|
||||||
|
|
||||||
l0 = []
|
def test_repr_deep(self):
|
||||||
for i in xrange(sys.getrecursionlimit() + 100):
|
a = self.type2test([])
|
||||||
l0 = [l0]
|
for i in range(sys.getrecursionlimit() + 100):
|
||||||
self.assertRaises(RuntimeError, repr, l0)
|
a = self.type2test([a])
|
||||||
|
self.assertRaises(RuntimeError, repr, a)
|
||||||
|
|
||||||
def test_print(self):
|
def test_print(self):
|
||||||
d = self.type2test(xrange(200))
|
d = self.type2test(xrange(200))
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
import unittest
|
import unittest
|
||||||
import UserDict
|
import UserDict
|
||||||
import test_support
|
import test_support
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
class BasicTestMappingProtocol(unittest.TestCase):
|
class BasicTestMappingProtocol(unittest.TestCase):
|
||||||
|
@ -645,6 +646,14 @@ class TestHashMappingProtocol(TestMappingProtocol):
|
||||||
d = self._full_mapping({1: BadRepr()})
|
d = self._full_mapping({1: BadRepr()})
|
||||||
self.assertRaises(Exc, repr, d)
|
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):
|
def test_le(self):
|
||||||
self.assertTrue(not (self._empty_mapping() < self._empty_mapping()))
|
self.assertTrue(not (self._empty_mapping() < self._empty_mapping()))
|
||||||
self.assertTrue(not (self._full_mapping({1: 2}) < self._full_mapping({1L: 2L})))
|
self.assertTrue(not (self._full_mapping({1: 2}) < self._full_mapping({1L: 2L})))
|
||||||
|
|
|
@ -3,6 +3,7 @@ from test import test_support
|
||||||
|
|
||||||
import UserDict, random, string
|
import UserDict, random, string
|
||||||
import gc, weakref
|
import gc, weakref
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
class DictTest(unittest.TestCase):
|
class DictTest(unittest.TestCase):
|
||||||
|
@ -422,6 +423,12 @@ class DictTest(unittest.TestCase):
|
||||||
d = {1: BadRepr()}
|
d = {1: BadRepr()}
|
||||||
self.assertRaises(Exc, repr, d)
|
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):
|
def test_le(self):
|
||||||
self.assertFalse({} < {})
|
self.assertFalse({} < {})
|
||||||
self.assertFalse({1: 2} < {1L: 2L})
|
self.assertFalse({1: 2} < {1L: 2L})
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
The repr of deeply nested dict now raises a RecursionError instead of
|
||||||
|
crashing due to a stack overflow.
|
|
@ -383,10 +383,7 @@ list_repr(PyListObject *v)
|
||||||
so must refetch the list size on each iteration. */
|
so must refetch the list size on each iteration. */
|
||||||
for (i = 0; i < Py_SIZE(v); ++i) {
|
for (i = 0; i < Py_SIZE(v); ++i) {
|
||||||
int status;
|
int status;
|
||||||
if (Py_EnterRecursiveCall(" while getting the repr of a list"))
|
|
||||||
goto Done;
|
|
||||||
s = PyObject_Repr(v->ob_item[i]);
|
s = PyObject_Repr(v->ob_item[i]);
|
||||||
Py_LeaveRecursiveCall();
|
|
||||||
if (s == NULL)
|
if (s == NULL)
|
||||||
goto Done;
|
goto Done;
|
||||||
status = PyList_Append(pieces, s);
|
status = PyList_Append(pieces, s);
|
||||||
|
|
|
@ -378,7 +378,12 @@ PyObject_Repr(PyObject *v)
|
||||||
Py_TYPE(v)->tp_name, v);
|
Py_TYPE(v)->tp_name, v);
|
||||||
else {
|
else {
|
||||||
PyObject *res;
|
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);
|
res = (*Py_TYPE(v)->tp_repr)(v);
|
||||||
|
Py_LeaveRecursiveCall();
|
||||||
if (res == NULL)
|
if (res == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
#ifdef Py_USING_UNICODE
|
#ifdef Py_USING_UNICODE
|
||||||
|
|
|
@ -288,10 +288,7 @@ tuplerepr(PyTupleObject *v)
|
||||||
|
|
||||||
/* Do repr() on each element. */
|
/* Do repr() on each element. */
|
||||||
for (i = 0; i < n; ++i) {
|
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]);
|
s = PyObject_Repr(v->ob_item[i]);
|
||||||
Py_LeaveRecursiveCall();
|
|
||||||
if (s == NULL)
|
if (s == NULL)
|
||||||
goto Done;
|
goto Done;
|
||||||
PyTuple_SET_ITEM(pieces, i, s);
|
PyTuple_SET_ITEM(pieces, i, s);
|
||||||
|
|
Loading…
Reference in New Issue