Skip to content

Commit

Permalink
pythongh-113703: Correctly identify incomplete f-strings in the codeo…
Browse files Browse the repository at this point in the history
…p module (python#113709)
  • Loading branch information
pablogsal authored Jan 5, 2024
1 parent 0ae60b6 commit 3003fbb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
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

0 comments on commit 3003fbb

Please sign in to comment.