Skip to content

Commit 14a93f4

Browse files
authored
gh-156091: Fix crash compiling deeply nested inlined comprehensions (#156957)
1 parent ba4a079 commit 14a93f4

4 files changed

Lines changed: 27 additions & 2 deletions

File tree

Include/internal/pycore_compile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ enum _PyCompile_FBlockType {
110110
COMPILE_FBLOCK_EXCEPTION_HANDLER,
111111
COMPILE_FBLOCK_EXCEPTION_GROUP_HANDLER,
112112
COMPILE_FBLOCK_ASYNC_COMPREHENSION_GENERATOR,
113+
COMPILE_FBLOCK_INLINED_COMPREHENSION,
113114
COMPILE_FBLOCK_STOP_ITERATION,
114115
};
115116

Lib/test/test_syntax.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3519,6 +3519,21 @@ def test_syntax_error_on_deeply_nested_blocks(self):
35193519
"""
35203520
self._check_error(source, "too many statically nested blocks")
35213521

3522+
@support.cpython_only
3523+
def test_nested_inlined_comprehensions_block_limit(self):
3524+
# Each inlined comprehension with locals emits SETUP_FINALLY, which
3525+
# must count toward CO_MAXBLOCKS (gh-156091).
3526+
def src(depth):
3527+
e = "i for i in r"
3528+
for _ in range(depth - 1):
3529+
e = "[" + e + "] for i in r"
3530+
return "x = [" + e + "]"
3531+
3532+
CO_MAXBLOCKS = 21
3533+
compile(src(CO_MAXBLOCKS), "<testcase>", "exec")
3534+
self._check_error(src(CO_MAXBLOCKS + 1),
3535+
"too many statically nested blocks")
3536+
35223537
@support.cpython_only
35233538
def test_error_on_parser_stack_overflow(self):
35243539
source = "-" * 100000 + "4"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a crash when compiling deeply nested inlined list, set, or dict
2+
comprehensions. A :exc:`SyntaxError` is now raised when the nesting exceeds
3+
the compiler's static block limit.

Python/codegen.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ codegen_unwind_fblock(compiler *c, location *ploc,
551551
case COMPILE_FBLOCK_EXCEPTION_HANDLER:
552552
case COMPILE_FBLOCK_EXCEPTION_GROUP_HANDLER:
553553
case COMPILE_FBLOCK_ASYNC_COMPREHENSION_GENERATOR:
554+
case COMPILE_FBLOCK_INLINED_COMPREHENSION:
554555
case COMPILE_FBLOCK_STOP_ITERATION:
555556
return SUCCESS;
556557

@@ -4976,8 +4977,11 @@ codegen_push_inlined_comprehension_locals(compiler *c, location loc,
49764977
NEW_JUMP_TARGET_LABEL(c, cleanup);
49774978
state->cleanup = cleanup;
49784979

4979-
// no need to push an fblock for this "virtual" try/finally; there can't
4980-
// be return/continue/break inside a comprehension
4980+
// Count against CO_MAXBLOCKS: SETUP_FINALLY consumes an except-stack
4981+
// slot even though return/continue/break cannot appear here.
4982+
RETURN_IF_ERROR(_PyCompile_PushFBlock(
4983+
c, loc, COMPILE_FBLOCK_INLINED_COMPREHENSION,
4984+
cleanup, NO_LABEL, NULL));
49814985
ADDOP_JUMP(c, loc, SETUP_FINALLY, cleanup);
49824986
}
49834987
return SUCCESS;
@@ -5023,6 +5027,8 @@ codegen_pop_inlined_comprehension_locals(compiler *c, location loc,
50235027
{
50245028
if (state->pushed_locals) {
50255029
ADDOP(c, NO_LOCATION, POP_BLOCK);
5030+
_PyCompile_PopFBlock(c, COMPILE_FBLOCK_INLINED_COMPREHENSION,
5031+
state->cleanup);
50265032

50275033
NEW_JUMP_TARGET_LABEL(c, end);
50285034
ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, end);

0 commit comments

Comments
 (0)