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

Serhiy Storchaka webhook-mailer at python.org
Sun Dec 3 15:12:16 EST 2017


https://github.com/python/cpython/commit/1fb72d2ad243c965d4432b4e93884064001a2607
commit: 1fb72d2ad243c965d4432b4e93884064001a2607
branch: master
author: Serhiy Storchaka <storchaka at gmail.com>
committer: GitHub <noreply at github.com>
date: 2017-12-03T22:12:11+02:00
summary:

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

instead of crashing due to a stack overflow.

This perhaps will fix similar problems in other extension types.

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 ce9db9a1b8b..ed63fda20c1 100644
--- a/Lib/test/list_tests.py
+++ b/Lib/test/list_tests.py
@@ -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))
diff --git a/Lib/test/mapping_tests.py b/Lib/test/mapping_tests.py
index ff82f4eb7d8..53f29f60538 100644
--- a/Lib/test/mapping_tests.py
+++ b/Lib/test/mapping_tests.py
@@ -1,6 +1,7 @@
 # tests common to dict and UserDict
 import unittest
 import collections
+import sys
 
 
 class BasicTestMappingProtocol(unittest.TestCase):
@@ -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}),
diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py
index 8013f37c88d..4386eda3ae4 100644
--- a/Lib/test/test_dict.py
+++ b/Lib/test/test_dict.py
@@ -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})
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 00000000000..f8f4ab93c9e
--- /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 8576b7ae683..8794e37364a 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -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;
 
diff --git a/Objects/object.c b/Objects/object.c
index 674180d7203..a0d651d0805 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -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)) {
diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c
index 964db3bb8de..3a609461251 100644
--- a/Objects/tupleobject.c
+++ b/Objects/tupleobject.c
@@ -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;
 



More information about the Python-checkins mailing list