Skip to content

Commit 87f9bfd

Browse files
authored
Merge pull request python#60 from mgmacias95/fix_nested_bug
2 parents a0edfb1 + 61b1f59 commit 87f9bfd

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

Lib/test/test_fstring.py

Lines changed: 6 additions & 1 deletion
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'{.}'"])
@@ -1390,7 +1396,6 @@ def __repr__(self):
13901396
#self.assertEqual(f'X{x =}Y', 'Xx\t='+repr(x)+'Y')
13911397
#self.assertEqual(f'X{x = }Y', 'Xx\t=\t'+repr(x)+'Y')
13921398

1393-
13941399
def test_walrus(self):
13951400
x = 20
13961401
# This isn't an assignment expression, it's 'x', with a format

Parser/tokenizer.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2458,6 +2458,10 @@ tok_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct
24582458

24592459
if ((start_char == '{' && peek1 != '{') || (start_char == '}' && peek1 != '}')) {
24602460
if (start_char == '{') {
2461+
if (current_tok->bracket_mark_index >= MAX_EXPR_NEXTING) {
2462+
return MAKE_TOKEN(syntaxerror(tok, "f-string: expressions nested too deeply"));
2463+
}
2464+
24612465
current_tok->bracket_mark[++current_tok->bracket_mark_index] = current_tok->bracket_stack;
24622466
}
24632467
tok->tok_mode_stack[tok->tok_mode_stack_index].kind = TOK_REGULAR_MODE;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy