From 3003fbbf00422bce6e327646063e97470afa9091 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Fri, 5 Jan 2024 12:16:46 +0000 Subject: [PATCH] gh-113703: Correctly identify incomplete f-strings in the codeop module (#113709) --- Lib/test/test_codeop.py | 3 +++ .../2024-01-04-17-15-30.gh-issue-113703.Zsk0pY.rst | 2 ++ Parser/lexer/lexer.c | 8 ++++++-- 3 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-01-04-17-15-30.gh-issue-113703.Zsk0pY.rst diff --git a/Lib/test/test_codeop.py b/Lib/test/test_codeop.py index 2abb6c6d935..787bd1b6a79 100644 --- a/Lib/test/test_codeop.py +++ b/Lib/test/test_codeop.py @@ -223,6 +223,9 @@ class CodeopTests(unittest.TestCase): 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") diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-01-04-17-15-30.gh-issue-113703.Zsk0pY.rst b/Misc/NEWS.d/next/Core and Builtins/2024-01-04-17-15-30.gh-issue-113703.Zsk0pY.rst new file mode 100644 index 00000000000..5db93e34472 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-01-04-17-15-30.gh-issue-113703.Zsk0pY.rst @@ -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 diff --git a/Parser/lexer/lexer.c b/Parser/lexer/lexer.c index ea4bdf7ce4a..ebf7686773f 100644 --- a/Parser/lexer/lexer.c +++ b/Parser/lexer/lexer.c @@ -1355,9 +1355,13 @@ f_string_middle: 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,