Skip to content

Commit d87b26a

Browse files
committed
gh-153569: test nesting without parser recursion
1 parent 9d023a8 commit d87b26a

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

Lib/test/test_fstring.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -663,10 +663,7 @@ def create_nested_fstring(n):
663663
prev = create_nested_fstring(n-1)
664664
return f'f"{{{prev}}}"'
665665

666-
compile(create_nested_fstring(149), "<string>", "eval")
667-
with self.assertRaisesRegex(
668-
SyntaxError, "too many nested f-strings or t-strings"):
669-
compile(create_nested_fstring(150), "<string>", "eval")
666+
raises_syntax_or_memory_error(create_nested_fstring(160))
670667
raises_syntax_or_memory_error("f'{" + "("*100 + "}'")
671668
raises_syntax_or_memory_error("f'{" + "("*1000 + "}'")
672669
raises_syntax_or_memory_error("f'{" + "("*10_000 + "}'")

Lib/test/test_tokenize.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2590,6 +2590,21 @@ def test_carriage_return_after_debug_comment(self):
25902590
("unexpected EOF in multi-line statement", (1, 7)),
25912591
)
25922592

2593+
def test_formatted_string_nesting_limit(self):
2594+
def nested_string(depth, prefix):
2595+
source = "'x'"
2596+
for _ in range(depth):
2597+
source = f'{prefix}"{{{source}}}"'
2598+
return source
2599+
2600+
for prefix in ("f", "t"):
2601+
with self.subTest(prefix=prefix):
2602+
self._get_tokens(nested_string(149, prefix))
2603+
with self.assertRaisesRegex(
2604+
tokenize.TokenError,
2605+
"too many nested f-strings or t-strings"):
2606+
self._get_tokens(nested_string(150, prefix))
2607+
25932608
def test_escaped_fstring_brace_has_a_position_gap(self):
25942609
tokens = self._get_tokens('f"a{{"', extra_tokens=True)
25952610
self.assertEqual(

0 commit comments

Comments
 (0)