[Python-checkins] [2.7] bpo-32137: The repr of deeply nested dict now raises a RuntimeError (GH-4570) (#5493)

Serhiy Storchaka webhook-mailer at python.org
Fri Feb 2 09:29:10 EST 2018


https://github.com/python/cpython/commit/b7a2c17be8411bc4c7a2babdc650074c14204aa8
commit: b7a2c17be8411bc4c7a2babdc650074c14204aa8
branch: 2.7
author: Serhiy Storchaka <storchaka at gmail.com>
committer: GitHub <noreply at github.com>
date: 2018-02-02T16:29:02+02:00
summary:

[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)

files:
A Misc/NEWS.d/next/Core and Builtins/2017-11-26-14-36-30.bpo-32137.Stj5nL.rst
M Lib/test/list_tests.py
M Lib/test/mapping_tests.py
M Lib/test/test_dict.py
M Objects/listobject.c
M Objects/object.c
M Objects/tupleobject.c

diff --git a/Lib/test/list_tests.py b/Lib/test/list_tests.py
index ed606db0d11d..54cfb97ccb8e 100644
--- a/Lib/test/list_tests.py
+++ b/Lib/test/list_tests.py
@@ -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))
diff --git a/Lib/test/mapping_tests.py b/Lib/test/mapping_tests.py
index f43750bb83f6..248dee2fed18 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 @@ 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})))
diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py
index 6b2db34ef331..aacd4739ef3b 100644
--- a/Lib/test/test_dict.py
+++ b/Lib/test/test_dict.py
@@ -3,6 +3,7 @@
 
 import UserDict, random, string
 import gc, weakref
+import sys
 
 
 class DictTest(unittest.TestCase):
@@ -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})
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 000000000000..f8f4ab93c9e2
--- /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 bb65e98714ee..24eff769c64e 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 7a2821897999..65366b0b351b 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 a61c8aa013b4..6f4b18cc5c65 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);



More information about the Python-checkins mailing list