gh-113703: Correctly identify incomplete f-strings in the codeop module (#113709)

This commit is contained in:
Pablo Galindo Salgado 2024-01-05 12:16:46 +00:00 committed by GitHub
parent 0ae60b66de
commit 3003fbbf00
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 2 deletions

View file

@ -223,6 +223,9 @@ def test_incomplete(self):
ai("(x for x in") ai("(x for x in")
ai("(x for x in (") ai("(x for x in (")
ai('a = f"""')
ai('a = \\')
def test_invalid(self): def test_invalid(self):
ai = self.assertInvalid ai = self.assertInvalid
ai("a b") ai("a b")

View file

@ -0,0 +1,2 @@
Fix a regression in the :mod:`codeop` module that was causing it to incorrectly
identify incomplete f-strings. Patch by Pablo Galindo

View file

@ -1355,9 +1355,13 @@ tok_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct
tok->lineno = the_current_tok->f_string_line_start; tok->lineno = the_current_tok->f_string_line_start;
if (current_tok->f_string_quote_size == 3) { if (current_tok->f_string_quote_size == 3) {
return MAKE_TOKEN(_PyTokenizer_syntaxerror(tok, _PyTokenizer_syntaxerror(tok,
"unterminated triple-quoted f-string literal" "unterminated triple-quoted f-string literal"
" (detected at line %d)", start)); " (detected at line %d)", start);
if (c != '\n') {
tok->done = E_EOFS;
}
return MAKE_TOKEN(ERRORTOKEN);
} }
else { else {
return MAKE_TOKEN(_PyTokenizer_syntaxerror(tok, return MAKE_TOKEN(_PyTokenizer_syntaxerror(tok,