summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Lib/test/support/__init__.py10
-rw-r--r--Lib/test/test_ast.py6
2 files changed, 14 insertions, 2 deletions
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index 80f3a04fcc..b9040a9ac5 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -1982,3 +1982,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 e08a965d96..a44f8f551c 100644
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -1101,7 +1101,8 @@ Module(
e = ast.UnaryOp(op=ast.Not(), lineno=0, col_offset=0)
e.operand = e
with self.assertRaises(RecursionError):
- compile(ast.Expression(e), "<test>", "eval")
+ with support.infinite_recursion():
+ compile(ast.Expression(e), "<test>", "eval")
def test_recursion_indirect(self):
e = ast.UnaryOp(op=ast.Not(), lineno=0, col_offset=0)
@@ -1109,7 +1110,8 @@ Module(
e.operand = f
f.operand = e
with self.assertRaises(RecursionError):
- compile(ast.Expression(e), "<test>", "eval")
+ with support.infinite_recursion():
+ compile(ast.Expression(e), "<test>", "eval")
class ASTValidatorTests(unittest.TestCase):