[3.14] gh-156091: Fix crash compiling deeply nested inlined comprehensions (GH-156957) by miss-islington · Pull Request #156993 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Include/internal/pycore_compile.h
15 changes: 15 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -3225,6 +3225,21 @@ def test_syntax_error_on_deeply_nested_blocks(self):
"""
self._check_error(source, "too many statically nested blocks")

@support.cpython_only
def test_nested_inlined_comprehensions_block_limit(self):
# Each inlined comprehension with locals emits SETUP_FINALLY, which
# must count toward CO_MAXBLOCKS (gh-156091).
def src(depth):
e = "i for i in r"
for _ in range(depth - 1):
e = "[" + e + "] for i in r"
return "x = [" + e + "]"

CO_MAXBLOCKS = 21
compile(src(CO_MAXBLOCKS), "<testcase>", "exec")
self._check_error(src(CO_MAXBLOCKS + 1),
"too many statically nested blocks")

@support.cpython_only
def test_error_on_parser_stack_overflow(self):
source = "-" * 100000 + "4"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a crash when compiling deeply nested inlined list, set, or dict
comprehensions. A :exc:`SyntaxError` is now raised when the nesting exceeds
the compiler's static block limit.
10 changes: 8 additions & 2 deletions Python/codegen.c
Loading