Skip to content

Commit 61b1f59

Browse files
committed
fix(fstring): Expressions nested too deeply error
1 parent f70f8af commit 61b1f59

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

Lib/test/test_fstring.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,11 @@ def test_mismatched_parens(self):
511511
])
512512
self.assertRaises(SyntaxError, eval, "f'{" + "("*500 + "}'")
513513

514+
def test_fstring_nested_too_deeply(self):
515+
self.assertAllRaise(SyntaxError,
516+
"f-string: expressions nested too deeply",
517+
['f"{1+2:{1+2:{1+1:{1}}}}"'])
518+
514519
def test_double_braces(self):
515520
self.assertEqual(f'{{', '{')
516521
self.assertEqual(f'a{{', 'a{')
@@ -741,6 +746,7 @@ def test_parens_in_expressions(self):
741746
self.assertAllRaise(SyntaxError, 'unterminated string literal',
742747
["f'{\n}'",
743748
])
749+
744750
def test_newlines_before_syntax_error(self):
745751
self.assertAllRaise(SyntaxError, "invalid syntax",
746752
["f'{.}'", "\nf'{.}'", "\n\nf'{.}'"])
@@ -1379,7 +1385,6 @@ def __repr__(self):
13791385
#self.assertEqual(f'X{x =}Y', 'Xx\t='+repr(x)+'Y')
13801386
#self.assertEqual(f'X{x = }Y', 'Xx\t=\t'+repr(x)+'Y')
13811387

1382-
13831388
def test_walrus(self):
13841389
x = 20
13851390
# This isn't an assignment expression, it's 'x', with a format

Parser/tokenizer.c

+4
Original file line numberDiff line numberDiff line change
@@ -2443,6 +2443,10 @@ tok_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct
24432443

24442444
if ((start_char == '{' && peek1 != '{') || (start_char == '}' && peek1 != '}')) {
24452445
if (start_char == '{') {
2446+
if (current_tok->bracket_mark_index >= MAX_EXPR_NEXTING) {
2447+
return MAKE_TOKEN(syntaxerror(tok, "f-string: expressions nested too deeply"));
2448+
}
2449+
24462450
current_tok->bracket_mark[++current_tok->bracket_mark_index] = current_tok->bracket_stack;
24472451
}
24482452
tok->tok_mode_stack[tok->tok_mode_stack_index].kind = TOK_REGULAR_MODE;

0 commit comments

Comments
 (0)