[Python-checkins] bpo-42986: Fix parser crash when reporting syntax errors in f-string with newlines (GH-24279)

pablogsal webhook-mailer at python.org
Sun Jan 31 17:48:30 EST 2021


https://github.com/python/cpython/commit/40901518167c66abc1ebc5b71c5b86d733cfa154
commit: 40901518167c66abc1ebc5b71c5b86d733cfa154
branch: master
author: Pablo Galindo <Pablogsal at gmail.com>
committer: pablogsal <Pablogsal at gmail.com>
date: 2021-01-31T22:48:23Z
summary:

bpo-42986: Fix parser crash when reporting syntax errors in f-string with newlines (GH-24279)

files:
A Misc/NEWS.d/next/Core and Builtins/2021-01-20-23-44-15.bpo-42986.sWoaGf.rst
M Lib/test/test_fstring.py
M Parser/pegen.c

diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py
index 7ca1512ebbf1b..d7143d154a1bc 100644
--- a/Lib/test/test_fstring.py
+++ b/Lib/test/test_fstring.py
@@ -664,6 +664,9 @@ def test_parens_in_expressions(self):
         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
index 0000000000000..6e4ed60bf224d
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-01-20-23-44-15.bpo-42986.sWoaGf.rst	
@@ -0,0 +1,2 @@
+Fix parser crash when reporting syntax errors in f-string with newlines.
+Patch by Pablo Galindo.
diff --git a/Parser/pegen.c b/Parser/pegen.c
index 0e7f86bc99e45..2554273877f78 100644
--- a/Parser/pegen.c
+++ b/Parser/pegen.c
@@ -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");
         }



More information about the Python-checkins mailing list