summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_exceptions.py
diff options
context:
space:
mode:
authorMark Shannon <mark@hotpy.org>2021-03-02 10:36:38 (GMT)
committerGitHub <noreply@github.com>2021-03-02 10:36:38 (GMT)
commit8b795ab5541d8a4e69be4137dfdc207714270b77 (patch)
treed7fcc96f3243d082ea024964a643a32a004a5fbd /Lib/test/test_exceptions.py
parentf836e5f2194857b24ec03adfcfcce05375868f88 (diff)
downloadcpython-8b795ab5541d8a4e69be4137dfdc207714270b77.zip
cpython-8b795ab5541d8a4e69be4137dfdc207714270b77.tar.gz
cpython-8b795ab5541d8a4e69be4137dfdc207714270b77.tar.bz2
bpo-42500: Fix recursion in or after except (GH-23568) (#24501)
* Use counter, rather boolean state when handling soft overflows. (cherry picked from commit 4e7a69bdb63a104587759d7784124492dcdd496e)
Diffstat (limited to 'Lib/test/test_exceptions.py')
-rw-r--r--Lib/test/test_exceptions.py52
1 files changed, 50 insertions, 2 deletions
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index 8d125b5..9c09bdc 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -1043,7 +1043,7 @@ class ExceptionTests(unittest.TestCase):
# tstate->recursion_depth is equal to (recursion_limit - 1)
# and is equal to recursion_limit when _gen_throw() calls
# PyErr_NormalizeException().
- recurse(setrecursionlimit(depth + 2) - depth - 1)
+ recurse(setrecursionlimit(depth + 2) - depth)
finally:
sys.setrecursionlimit(recursionlimit)
print('Done.')
@@ -1073,6 +1073,54 @@ class ExceptionTests(unittest.TestCase):
b'while normalizing an exception', err)
self.assertIn(b'Done.', out)
+
+ def test_recursion_in_except_handler(self):
+
+ def set_relative_recursion_limit(n):
+ depth = 1
+ while True:
+ try:
+ sys.setrecursionlimit(depth)
+ except RecursionError:
+ depth += 1
+ else:
+ break
+ sys.setrecursionlimit(depth+n)
+
+ def recurse_in_except():
+ try:
+ 1/0
+ except:
+ recurse_in_except()
+
+ def recurse_after_except():
+ try:
+ 1/0
+ except:
+ pass
+ recurse_after_except()
+
+ def recurse_in_body_and_except():
+ try:
+ recurse_in_body_and_except()
+ except:
+ recurse_in_body_and_except()
+
+ recursionlimit = sys.getrecursionlimit()
+ try:
+ set_relative_recursion_limit(10)
+ for func in (recurse_in_except, recurse_after_except, recurse_in_body_and_except):
+ with self.subTest(func=func):
+ try:
+ func()
+ except RecursionError:
+ pass
+ else:
+ self.fail("Should have raised a RecursionError")
+ finally:
+ sys.setrecursionlimit(recursionlimit)
+
+
@cpython_only
def test_recursion_normalizing_with_no_memory(self):
# Issue #30697. Test that in the abort that occurs when there is no
@@ -1109,7 +1157,7 @@ class ExceptionTests(unittest.TestCase):
except MemoryError as e:
tb = e.__traceback__
else:
- self.fail("Should have raises a MemoryError")
+ self.fail("Should have raised a MemoryError")
return traceback.format_tb(tb)
tb1 = raiseMemError()