mirror of
https://github.com/python/cpython.git
synced 2025-10-17 04:08:28 +00:00
gh-95185: Check recursion depth in the AST constructor (#95186)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
3c94d3395e
commit
0047447294
5 changed files with 168 additions and 2 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue