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
2 changes: 1 addition & 1 deletion Include/internal/pycore_flowgraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ typedef struct {


typedef struct {
struct _PyCfgBasicblock_ *handlers[CO_MAXBLOCKS+1];
struct _PyCfgBasicblock_ *handlers[CO_MAXBLOCKS+2];
int depth;
} _PyCfgExceptStack;

Expand Down
26 changes: 26 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -1995,6 +1995,7 @@ def f(x: *b)

import re
import doctest
import textwrap
import unittest

from test import support
Expand Down Expand Up @@ -2241,6 +2242,31 @@ def test_nested_named_except_blocks(self):
code += f"{' '*4*12}pass"
self._check_error(code, "too many statically nested blocks")

@support.cpython_only
def test_with_statement_many_context_managers(self):
# See gh-113297

def get_code(n):
code = textwrap.dedent("""
def bug():
with (
a
""")
for i in range(n):
code += f" as a{i}, a\n"
code += "): yield a"
return code

CO_MAXBLOCKS = 20 # static nesting limit of the compiler

for n in range(CO_MAXBLOCKS):
with self.subTest(f"within range: {n=}"):
compile(get_code(n), "<string>", "exec")

for n in range(CO_MAXBLOCKS, CO_MAXBLOCKS + 5):
with self.subTest(f"out of range: {n=}"):
self._check_error(get_code(n), "too many statically nested blocks")

def test_barry_as_flufl_with_syntax_errors(self):
# The "barry_as_flufl" rule can produce some "bugs-at-a-distance" if
# is reading the wrong token in the presence of syntax errors later
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix segfault in the compiler on with statement with 19 context managers.
1 change: 1 addition & 0 deletions Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ push_except_block(ExceptStack *stack, cfg_instr *setup) {
if (opcode == SETUP_WITH || opcode == SETUP_CLEANUP) {
target->b_preserve_lasti = 1;
}
assert(stack->depth <= CO_MAXBLOCKS);
stack->handlers[++stack->depth] = target;
return target;
}
Expand Down