Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Lib/test/test_codeop.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ def test_incomplete(self):
ai("(x for x in")
ai("(x for x in (")

ai('a = f"""')
ai('a = \\')

def test_invalid(self):
ai = self.assertInvalid
ai("a b")
Expand Down
Original file line number Diff line number Diff line change
@@ -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
8 changes: 6 additions & 2 deletions Parser/lexer/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

if (current_tok->f_string_quote_size == 3) {
return MAKE_TOKEN(_PyTokenizer_syntaxerror(tok,
_PyTokenizer_syntaxerror(tok,
"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 {
return MAKE_TOKEN(_PyTokenizer_syntaxerror(tok,
Expand Down