Skip to content

Commit dd36d01

Browse files
[3.14] gh-156091: Fix crash compiling deeply nested inlined comprehensions (GH-156957) (#156993)
1 parent 5532330 commit dd36d01

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
@@ -106,6 +106,7 @@ enum _PyCompile_FBlockType {
106106
COMPILE_FBLOCK_EXCEPTION_HANDLER,
107107
COMPILE_FBLOCK_EXCEPTION_GROUP_HANDLER,
108108
COMPILE_FBLOCK_ASYNC_COMPREHENSION_GENERATOR,
109+
COMPILE_FBLOCK_INLINED_COMPREHENSION,
109110
COMPILE_FBLOCK_STOP_ITERATION,
110111
};
111112

Lib/test/test_syntax.py

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

3228+
@support.cpython_only
3229+
def test_nested_inlined_comprehensions_block_limit(self):
3230+
# Each inlined comprehension with locals emits SETUP_FINALLY, which
3231+
# must count toward CO_MAXBLOCKS (gh-156091).
3232+
def src(depth):
3233+
e = "i for i in r"
3234+
for _ in range(depth - 1):
3235+
e = "[" + e + "] for i in r"
3236+
return "x = [" + e + "]"
3237+
3238+
CO_MAXBLOCKS = 21
3239+
compile(src(CO_MAXBLOCKS), "<testcase>", "exec")
3240+
self._check_error(src(CO_MAXBLOCKS + 1),
3241+
"too many statically nested blocks")
3242+
32283243
@support.cpython_only
32293244
def test_error_on_parser_stack_overflow(self):
32303245
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
@@ -527,6 +527,7 @@ codegen_unwind_fblock(compiler *c, location *ploc,
527527
case COMPILE_FBLOCK_EXCEPTION_HANDLER:
528528
case COMPILE_FBLOCK_EXCEPTION_GROUP_HANDLER:
529529
case COMPILE_FBLOCK_ASYNC_COMPREHENSION_GENERATOR:
530+
case COMPILE_FBLOCK_INLINED_COMPREHENSION:
530531
case COMPILE_FBLOCK_STOP_ITERATION:
531532
return SUCCESS;
532533

@@ -4685,8 +4686,11 @@ codegen_push_inlined_comprehension_locals(compiler *c, location loc,
46854686
NEW_JUMP_TARGET_LABEL(c, cleanup);
46864687
state->cleanup = cleanup;
46874688

4688-
// no need to push an fblock for this "virtual" try/finally; there can't
4689-
// be return/continue/break inside a comprehension
4689+
// Count against CO_MAXBLOCKS: SETUP_FINALLY consumes an except-stack
4690+
// slot even though return/continue/break cannot appear here.
4691+
RETURN_IF_ERROR(_PyCompile_PushFBlock(
4692+
c, loc, COMPILE_FBLOCK_INLINED_COMPREHENSION,
4693+
cleanup, NO_LABEL, NULL));
46904694
ADDOP_JUMP(c, loc, SETUP_FINALLY, cleanup);
46914695
}
46924696
return SUCCESS;
@@ -4732,6 +4736,8 @@ codegen_pop_inlined_comprehension_locals(compiler *c, location loc,
47324736
{
47334737
if (state->pushed_locals) {
47344738
ADDOP(c, NO_LOCATION, POP_BLOCK);
4739+
_PyCompile_PopFBlock(c, COMPILE_FBLOCK_INLINED_COMPREHENSION,
4740+
state->cleanup);
47354741

47364742
NEW_JUMP_TARGET_LABEL(c, end);
47374743
ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, end);

0 commit comments

Comments
 (0)