From 1ba3e1999d7f795adce99538c80911933acde52e Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Tue, 8 Jun 2021 19:55:10 +0300 Subject: [PATCH] [3.9] bpo-11105: reduce the recursion limit for tests (GH-26550). (cherry picked from commit e58d762c1fb4ad5e021d016c80c2bc4513632d2f) Co-authored-by: Batuhan Taskaya --- Lib/test/support/__init__.py | 10 ++++++++++ Lib/test/test_ast.py | 6 ++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index aee3737ffc4097..229e1ac0a2a22e 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -3199,3 +3199,13 @@ def skip_if_broken_multiprocessing_synchronize(): synchronize.Lock(ctx=None) except OSError as exc: raise unittest.SkipTest(f"broken multiprocessing SemLock: {exc!r}") + + +@contextlib.contextmanager +def infinite_recursion(max_depth=75): + original_depth = sys.getrecursionlimit() + try: + sys.setrecursionlimit(max_depth) + yield + finally: + sys.setrecursionlimit(original_depth) diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py index b229921f5c07c5..c3e3be6335340b 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -1031,7 +1031,8 @@ def test_recursion_direct(self): e = ast.UnaryOp(op=ast.Not(), lineno=0, col_offset=0) e.operand = e with self.assertRaises(RecursionError): - compile(ast.Expression(e), "", "eval") + with support.infinite_recursion(): + compile(ast.Expression(e), "", "eval") def test_recursion_indirect(self): e = ast.UnaryOp(op=ast.Not(), lineno=0, col_offset=0) @@ -1039,7 +1040,8 @@ def test_recursion_indirect(self): e.operand = f f.operand = e with self.assertRaises(RecursionError): - compile(ast.Expression(e), "", "eval") + with support.infinite_recursion(): + compile(ast.Expression(e), "", "eval") class ASTValidatorTests(unittest.TestCase):