Skip to content

Commit c98c990

Browse files
iritkatrielmiss-islington
authored andcommitted
gh-156091: Fix crash compiling deeply nested inlined comprehensions (GH-156957)
(cherry picked from commit 14a93f4) Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
1 parent 5e19ff3 commit c98c990

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
@@ -3503,6 +3503,21 @@ def test_syntax_error_on_deeply_nested_blocks(self):
35033503
"""
35043504
self._check_error(source, "too many statically nested blocks")
35053505

3506+
@support.cpython_only
3507+
def test_nested_inlined_comprehensions_block_limit(self):
3508+
# Each inlined comprehension with locals emits SETUP_FINALLY, which
3509+
# must count toward CO_MAXBLOCKS (gh-156091).
3510+
def src(depth):
3511+
e = "i for i in r"
3512+
for _ in range(depth - 1):
3513+
e = "[" + e + "] for i in r"
3514+
return "x = [" + e + "]"
3515+
3516+
CO_MAXBLOCKS = 21
3517+
compile(src(CO_MAXBLOCKS), "<testcase>", "exec")
3518+
self._check_error(src(CO_MAXBLOCKS + 1),
3519+
"too many statically nested blocks")
3520+
35063521
@support.cpython_only
35073522
def test_error_on_parser_stack_overflow(self):
35083523
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
@@ -540,6 +540,7 @@ codegen_unwind_fblock(compiler *c, location *ploc,
540540
case COMPILE_FBLOCK_EXCEPTION_HANDLER:
541541
case COMPILE_FBLOCK_EXCEPTION_GROUP_HANDLER:
542542
case COMPILE_FBLOCK_ASYNC_COMPREHENSION_GENERATOR:
543+
case COMPILE_FBLOCK_INLINED_COMPREHENSION:
543544
case COMPILE_FBLOCK_STOP_ITERATION:
544545
return SUCCESS;
545546

@@ -4873,8 +4874,11 @@ codegen_push_inlined_comprehension_locals(compiler *c, location loc,
48734874
NEW_JUMP_TARGET_LABEL(c, cleanup);
48744875
state->cleanup = cleanup;
48754876

4876-
// no need to push an fblock for this "virtual" try/finally; there can't
4877-
// be return/continue/break inside a comprehension
4877+
// Count against CO_MAXBLOCKS: SETUP_FINALLY consumes an except-stack
4878+
// slot even though return/continue/break cannot appear here.
4879+
RETURN_IF_ERROR(_PyCompile_PushFBlock(
4880+
c, loc, COMPILE_FBLOCK_INLINED_COMPREHENSION,
4881+
cleanup, NO_LABEL, NULL));
48784882
ADDOP_JUMP(c, loc, SETUP_FINALLY, cleanup);
48794883
}
48804884
return SUCCESS;
@@ -4920,6 +4924,8 @@ codegen_pop_inlined_comprehension_locals(compiler *c, location loc,
49204924
{
49214925
if (state->pushed_locals) {
49224926
ADDOP(c, NO_LOCATION, POP_BLOCK);
4927+
_PyCompile_PopFBlock(c, COMPILE_FBLOCK_INLINED_COMPREHENSION,
4928+
state->cleanup);
49234929

49244930
NEW_JUMP_TARGET_LABEL(c, end);
49254931
ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, end);

0 commit comments

Comments
 (0)