diff options
author | Pablo Galindo Salgado <Pablogsal@gmail.com> | 2022-07-24 14:58:52 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-24 14:58:52 (GMT) |
commit | 00474472944944b346d8409cfded84bb299f601a (patch) | |
tree | 3f8fce146302fb3f681544c8981b6ee46a86d621 /Lib | |
parent | 3c94d3395edbcd299cbdacd09660ed88e654eeef (diff) | |
download | cpython-00474472944944b346d8409cfded84bb299f601a.zip cpython-00474472944944b346d8409cfded84bb299f601a.tar.gz cpython-00474472944944b346d8409cfded84bb299f601a.tar.bz2 |
gh-95185: Check recursion depth in the AST constructor (#95186)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_ast.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py index 480089a..9734218 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -793,6 +793,27 @@ class AST_Tests(unittest.TestCase): return self enum._test_simple_enum(_Precedence, ast._Precedence) + @support.cpython_only + def test_ast_recursion_limit(self): + fail_depth = sys.getrecursionlimit() * 3 + crash_depth = sys.getrecursionlimit() * 300 + success_depth = int(fail_depth * 0.75) + + def check_limit(prefix, repeated): + expect_ok = prefix + repeated * success_depth + ast.parse(expect_ok) + for depth in (fail_depth, crash_depth): + broken = prefix + repeated * depth + details = "Compiling ({!r} + {!r} * {})".format( + prefix, repeated, depth) + with self.assertRaises(RecursionError, msg=details): + ast.parse(broken) + + check_limit("a", "()") + check_limit("a", ".b") + check_limit("a", "[0]") + check_limit("a", "*a") + class ASTHelpers_Test(unittest.TestCase): maxDiff = None |