summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2018-02-02 14:29:02 (GMT)
committerGitHub <noreply@github.com>2018-02-02 14:29:02 (GMT)
commitb7a2c17be8411bc4c7a2babdc650074c14204aa8 (patch)
treee3816d8aa190bb23b8fa362deeb36153f05e9369
parentb60f43a0e6052b29517931eea591b0b29903f382 (diff)
downloadcpython-b7a2c17be8411bc4c7a2babdc650074c14204aa8.zip
cpython-b7a2c17be8411bc4c7a2babdc650074c14204aa8.tar.gz
cpython-b7a2c17be8411bc4c7a2babdc650074c14204aa8.tar.bz2
[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 1fb72d2ad243c965d4432b4e93884064001a2607)
-rw-r--r--Lib/test/list_tests.py9
-rw-r--r--Lib/test/mapping_tests.py9
-rw-r--r--Lib/test/test_dict.py7
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2017-11-26-14-36-30.bpo-32137.Stj5nL.rst2
-rw-r--r--Objects/listobject.c3
-rw-r--r--Objects/object.c5
-rw-r--r--Objects/tupleobject.c3
7 files changed, 28 insertions, 10 deletions
diff --git a/Lib/test/list_tests.py b/Lib/test/list_tests.py
index ed606db..54cfb97 100644
--- a/Lib/test/list_tests.py
+++ b/Lib/test/list_tests.py
@@ -45,10 +45,11 @@ class CommonTest(seq_tests.CommonTest):
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))
diff --git a/Lib/test/mapping_tests.py b/Lib/test/mapping_tests.py
index f43750b..248dee2 100644
--- a/Lib/test/mapping_tests.py
+++ b/Lib/test/mapping_tests.py
@@ -2,6 +2,7 @@
import unittest
import UserDict
import test_support
+import sys
class BasicTestMappingProtocol(unittest.TestCase):
@@ -645,6 +646,14 @@ class TestHashMappingProtocol(TestMappingProtocol):
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})))
diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py
index 6b2db34..aacd473 100644
--- a/Lib/test/test_dict.py
+++ b/Lib/test/test_dict.py
@@ -3,6 +3,7 @@ from test import test_support
import UserDict, random, string
import gc, weakref
+import sys
class DictTest(unittest.TestCase):
@@ -422,6 +423,12 @@ class DictTest(unittest.TestCase):
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})
diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-11-26-14-36-30.bpo-32137.Stj5nL.rst b/Misc/NEWS.d/next/Core and Builtins/2017-11-26-14-36-30.bpo-32137.Stj5nL.rst
new file mode 100644
index 0000000..f8f4ab9
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2017-11-26-14-36-30.bpo-32137.Stj5nL.rst
@@ -0,0 +1,2 @@
+The repr of deeply nested dict now raises a RecursionError instead of
+crashing due to a stack overflow.
diff --git a/Objects/listobject.c b/Objects/listobject.c
index bb65e98..24eff76 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -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);
diff --git a/Objects/object.c b/Objects/object.c
index 7a28218..65366b0 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -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
diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c
index a61c8aa..6f4b18c 100644
--- a/Objects/tupleobject.c
+++ b/Objects/tupleobject.c
@@ -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);