Skip to content

Commit

Permalink
Merge pull request python#60 from mgmacias95/fix_nested_bug
Browse files Browse the repository at this point in the history
  • Loading branch information
pablogsal authored Mar 28, 2023
2 parents a0edfb1 + 61b1f59 commit 87f9bfd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Lib/test/test_fstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,11 @@ def test_mismatched_parens(self):
])
self.assertRaises(SyntaxError, eval, "f'{" + "("*500 + "}'")

def test_fstring_nested_too_deeply(self):
self.assertAllRaise(SyntaxError,
"f-string: expressions nested too deeply",
['f"{1+2:{1+2:{1+1:{1}}}}"'])

def test_double_braces(self):
self.assertEqual(f'{{', '{')
self.assertEqual(f'a{{', 'a{')
Expand Down Expand Up @@ -741,6 +746,7 @@ def test_parens_in_expressions(self):
self.assertAllRaise(SyntaxError, 'unterminated string literal',
["f'{\n}'",
])

def test_newlines_before_syntax_error(self):
self.assertAllRaise(SyntaxError, "invalid syntax",
["f'{.}'", "\nf'{.}'", "\n\nf'{.}'"])
Expand Down Expand Up @@ -1390,7 +1396,6 @@ def __repr__(self):
#self.assertEqual(f'X{x =}Y', 'Xx\t='+repr(x)+'Y')
#self.assertEqual(f'X{x = }Y', 'Xx\t=\t'+repr(x)+'Y')


def test_walrus(self):
x = 20
# This isn't an assignment expression, it's 'x', with a format
Expand Down
4 changes: 4 additions & 0 deletions Parser/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -2458,6 +2458,10 @@ tok_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct

if ((start_char == '{' && peek1 != '{') || (start_char == '}' && peek1 != '}')) {
if (start_char == '{') {
if (current_tok->bracket_mark_index >= MAX_EXPR_NEXTING) {
return MAKE_TOKEN(syntaxerror(tok, "f-string: expressions nested too deeply"));
}

current_tok->bracket_mark[++current_tok->bracket_mark_index] = current_tok->bracket_stack;
}
tok->tok_mode_stack[tok->tok_mode_stack_index].kind = TOK_REGULAR_MODE;
Expand Down

0 comments on commit 87f9bfd

Please sign in to comment.