bpo-11105: reduce the recursion limit for tests (GH-26550) · python/cpython@e58d762 (original) (raw)
2 files changed
lines changed
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1999,3 +1999,12 @@ def check_disallow_instantiation(testcase, tp, *args, **kwds): | ||
1999 | 1999 | qualname = f"{name}" |
2000 | 2000 | msg = f"cannot create '{re.escape(qualname)}' instances" |
2001 | 2001 | testcase.assertRaisesRegex(TypeError, msg, tp, *args, **kwds) |
2002 | + | |
2003 | +@contextlib.contextmanager | |
2004 | +def infinite_recursion(max_depth=75): | |
2005 | +original_depth = sys.getrecursionlimit() | |
2006 | +try: | |
2007 | +sys.setrecursionlimit(max_depth) | |
2008 | +yield | |
2009 | +finally: | |
2010 | +sys.setrecursionlimit(original_depth) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1102,15 +1102,17 @@ def test_recursion_direct(self): | ||
1102 | 1102 | e = ast.UnaryOp(op=ast.Not(), lineno=0, col_offset=0) |
1103 | 1103 | e.operand = e |
1104 | 1104 | with self.assertRaises(RecursionError): |
1105 | -compile(ast.Expression(e), "", "eval") | |
1105 | +with support.infinite_recursion(): | |
1106 | +compile(ast.Expression(e), "", "eval") | |
1106 | 1107 | |
1107 | 1108 | def test_recursion_indirect(self): |
1108 | 1109 | e = ast.UnaryOp(op=ast.Not(), lineno=0, col_offset=0) |
1109 | 1110 | f = ast.UnaryOp(op=ast.Not(), lineno=0, col_offset=0) |
1110 | 1111 | e.operand = f |
1111 | 1112 | f.operand = e |
1112 | 1113 | with self.assertRaises(RecursionError): |
1113 | -compile(ast.Expression(e), "", "eval") | |
1114 | +with support.infinite_recursion(): | |
1115 | +compile(ast.Expression(e), "", "eval") | |
1114 | 1116 | |
1115 | 1117 | |
1116 | 1118 | class ASTValidatorTests(unittest.TestCase): |