aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_syntax.py
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2023-12-22 01:50:26 +0000
committerGitHub <noreply@github.com>2023-12-22 01:50:26 +0000
commitc31943af16f885c8cf5d5a690c25c366afdb2862 (patch)
tree07f37431665e481e8d96275dbb541588e9158fe2 /Lib/test/test_syntax.py
parent9afb0e1606cad41ed57c42ea0a53ac90433f211b (diff)
downloadcpython-c31943af16f885c8cf5d5a690c25c366afdb2862.tar.gz
cpython-c31943af16f885c8cf5d5a690c25c366afdb2862.zip
gh-113297: Fix segfault in compiler for with statement with 19 context managers (#113327)
Diffstat (limited to 'Lib/test/test_syntax.py')
-rw-r--r--Lib/test/test_syntax.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py
index ece13660767..8b3ca69c9fe 100644
--- a/Lib/test/test_syntax.py
+++ b/Lib/test/test_syntax.py
@@ -2017,6 +2017,7 @@ Invalid expressions in type scopes:
import re
import doctest
+import textwrap
import unittest
from test import support
@@ -2279,6 +2280,31 @@ if x:
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