Skip to content

Commit 0affc3d

Browse files
authored
[3.12] gh-113703: Correctly identify incomplete f-strings in the codeop module (GH-113709) (#113733)
(cherry picked from commit 3003fbb)
1 parent 8435fbf commit 0affc3d

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

Lib/test/test_codeop.py

+3
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ def test_incomplete(self):
223223
ai("(x for x in")
224224
ai("(x for x in (")
225225

226+
ai('a = f"""')
227+
ai('a = \\')
228+
226229
def test_invalid(self):
227230
ai = self.assertInvalid
228231
ai("a b")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a regression in the :mod:`codeop` module that was causing it to incorrectly
2+
identify incomplete f-strings. Patch by Pablo Galindo

Parser/tokenizer.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -2851,9 +2851,13 @@ tok_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct
28512851
tok->lineno = the_current_tok->f_string_line_start;
28522852

28532853
if (current_tok->f_string_quote_size == 3) {
2854-
return MAKE_TOKEN(syntaxerror(tok,
2855-
"unterminated triple-quoted f-string literal"
2856-
" (detected at line %d)", start));
2854+
syntaxerror(tok,
2855+
"unterminated triple-quoted f-string literal"
2856+
" (detected at line %d)", start);
2857+
if (c != '\n') {
2858+
tok->done = E_EOFS;
2859+
}
2860+
return MAKE_TOKEN(ERRORTOKEN);
28572861
}
28582862
else {
28592863
return MAKE_TOKEN(syntaxerror(tok,

0 commit comments

Comments
 (0)