GH-103727: Avoid advancing tokenizer too far in f-string mode (GH-103775)

This commit is contained in:
Lysandros Nikolaou 2023-04-24 12:30:21 -06:00 committed by GitHub
parent ab25c7e311
commit cb157a1a35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 14 deletions

View File

@ -940,15 +940,13 @@ x = (
"f'{lambda :x}'", "f'{lambda :x}'",
"f'{lambda *arg, :x}'", "f'{lambda *arg, :x}'",
"f'{1, lambda:x}'", "f'{1, lambda:x}'",
"f'{lambda x:}'",
"f'{lambda :}'",
]) ])
# but don't emit the paren warning in general cases # but don't emit the paren warning in general cases
self.assertAllRaise(SyntaxError, with self.assertRaisesRegex(SyntaxError, "f-string: expecting a valid expression after '{'"):
"f-string: expecting a valid expression after '{'", eval("f'{+ lambda:None}'")
["f'{lambda x:}'",
"f'{lambda :}'",
"f'{+ lambda:None}'",
])
def test_valid_prefixes(self): def test_valid_prefixes(self):
self.assertEqual(F'{1}', "1") self.assertEqual(F'{1}', "1")

View File

@ -2481,19 +2481,21 @@ tok_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct
// If we start with a bracket, we defer to the normal mode as there is nothing for us to tokenize // If we start with a bracket, we defer to the normal mode as there is nothing for us to tokenize
// before it. // before it.
int start_char = tok_nextc(tok); int start_char = tok_nextc(tok);
int peek1 = tok_nextc(tok); if (start_char == '{') {
tok_backup(tok, peek1); int peek1 = tok_nextc(tok);
tok_backup(tok, start_char); tok_backup(tok, peek1);
tok_backup(tok, start_char);
if ((start_char == '{' && peek1 != '{') || (start_char == '}' && peek1 != '}')) { if (peek1 != '{') {
if (start_char == '{') {
current_tok->curly_bracket_expr_start_depth++; current_tok->curly_bracket_expr_start_depth++;
if (current_tok->curly_bracket_expr_start_depth >= MAX_EXPR_NESTING) { if (current_tok->curly_bracket_expr_start_depth >= MAX_EXPR_NESTING) {
return MAKE_TOKEN(syntaxerror(tok, "f-string: expressions nested too deeply")); return MAKE_TOKEN(syntaxerror(tok, "f-string: expressions nested too deeply"));
} }
TOK_GET_MODE(tok)->kind = TOK_REGULAR_MODE;
return tok_get_normal_mode(tok, current_tok, token);
} }
TOK_GET_MODE(tok)->kind = TOK_REGULAR_MODE; }
return tok_get_normal_mode(tok, current_tok, token); else {
tok_backup(tok, start_char);
} }
// Check if we are at the end of the string // Check if we are at the end of the string