diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2018-02-01 11:57:28 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2018-02-01 11:57:28 (GMT) |
commit | 688b6dec4e8847a154ef27257069291175764794 (patch) | |
tree | dc9b066bff693558e1c406b531dc6c024dff4394 /Lib | |
parent | 581ce25e1ffa374e62547ef266b6326bee0c54e5 (diff) | |
download | cpython-688b6dec4e8847a154ef27257069291175764794.zip cpython-688b6dec4e8847a154ef27257069291175764794.tar.gz cpython-688b6dec4e8847a154ef27257069291175764794.tar.bz2 |
bpo-32137: The repr of deeply nested dict now raises a RecursionError (GH-4570) (GH-4689)
instead of crashing due to a stack overflow.
This perhaps will fix similar problems in other extension types.
(cherry picked from commit 1fb72d2ad243c965d4432b4e93884064001a2607)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/list_tests.py | 7 | ||||
-rw-r--r-- | Lib/test/mapping_tests.py | 9 | ||||
-rw-r--r-- | Lib/test/test_dict.py | 6 |
3 files changed, 19 insertions, 3 deletions
diff --git a/Lib/test/list_tests.py b/Lib/test/list_tests.py index 26e9368..05e771f 100644 --- a/Lib/test/list_tests.py +++ b/Lib/test/list_tests.py @@ -53,10 +53,11 @@ class CommonTest(seq_tests.CommonTest): 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 ff82f4e..53f29f6 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 @@ 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(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 8013f37..4386eda 100644 --- a/Lib/test/test_dict.py +++ b/Lib/test/test_dict.py @@ -468,6 +468,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(RecursionError, repr, d) + def test_eq(self): self.assertEqual({}, {}) self.assertEqual({1: 2}, {1: 2}) |