]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-42986: Fix parser crash when reporting syntax errors in f-string with newlines...
authorPablo Galindo <Pablogsal@gmail.com>
Sun, 31 Jan 2021 22:48:23 +0000 (22:48 +0000)
committerGitHub <noreply@github.com>
Sun, 31 Jan 2021 22:48:23 +0000 (22:48 +0000)
Lib/test/test_fstring.py
Misc/NEWS.d/next/Core and Builtins/2021-01-20-23-44-15.bpo-42986.sWoaGf.rst [new file with mode: 0644]
Parser/pegen.c

index 7ca1512ebbf1bf4eac83cc3be4966a495890431a..d7143d154a1bc1aa004756aa1dd68179da1085d9 100644 (file)
@@ -664,6 +664,9 @@ x = (
         self.assertAllRaise(SyntaxError, 'unterminated string literal',
                             ["f'{\n}'",
                              ])
+    def test_newlines_before_syntax_error(self):
+        self.assertAllRaise(SyntaxError, "invalid syntax",
+                ["f'{.}'", "\nf'{.}'", "\n\nf'{.}'"])
 
     def test_backslashes_in_string_part(self):
         self.assertEqual(f'\t', '\t')
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-01-20-23-44-15.bpo-42986.sWoaGf.rst b/Misc/NEWS.d/next/Core and Builtins/2021-01-20-23-44-15.bpo-42986.sWoaGf.rst
new file mode 100644 (file)
index 0000000..6e4ed60
--- /dev/null
@@ -0,0 +1,2 @@
+Fix parser crash when reporting syntax errors in f-string with newlines.
+Patch by Pablo Galindo.
index 0e7f86bc99e4514825e804f9db61ee1788f1e130..2554273877f7832d13b6481027628690b8fd5e39 100644 (file)
@@ -454,7 +454,7 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
            does not physically exist */
         assert(p->tok->fp == NULL || p->tok->fp == stdin || p->tok->done == E_EOF);
 
-        if (p->tok->lineno == lineno) {
+        if (p->tok->lineno <= lineno) {
             Py_ssize_t size = p->tok->inp - p->tok->buf;
             error_line = PyUnicode_DecodeUTF8(p->tok->buf, size, "replace");
         }