From e67ac6e97d552fcd5e919b85e05a35c680503d0b Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Wed, 20 Jan 2021 23:43:27 +0000 Subject: [PATCH 1/3] Fix parser crash when reporting syntax errors in f-string with newlines --- Lib/test/test_exceptions.py | 1 + Parser/pegen.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index 21878c39f4fec9..4d5548110dbfa5 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -212,6 +212,7 @@ def testSyntaxErrorOffset(self): check('[file for str(file) in []\n])', 1, 11) check('[\nfile\nfor str(file)\nin\n[]\n]', 3, 5) check('[file for\n str(file) in []]', 2, 2) + check('"\n\nf\'{.}\'"' , 1, 1) # Errors thrown by compile.c check('class foo:return 1', 1, 11) diff --git a/Parser/pegen.c b/Parser/pegen.c index 0e7f86bc99e451..2554273877f783 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"); } From b82dc7741d7a921d47a2ea373f0651a67353362f Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Wed, 20 Jan 2021 23:44:22 +0000 Subject: [PATCH 2/3] Add NEWS entry --- .../Core and Builtins/2021-01-20-23-44-15.bpo-42986.sWoaGf.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2021-01-20-23-44-15.bpo-42986.sWoaGf.rst 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 00000000000000..6e4ed60bf224d6 --- /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. From f95cf783edbda5ddc991d1e21cfe6c0af2d15b91 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Wed, 20 Jan 2021 23:53:00 +0000 Subject: [PATCH 3/3] fixup! Add NEWS entry --- Lib/test/test_exceptions.py | 1 - Lib/test/test_fstring.py | 3 +++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index 4d5548110dbfa5..21878c39f4fec9 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -212,7 +212,6 @@ def testSyntaxErrorOffset(self): check('[file for str(file) in []\n])', 1, 11) check('[\nfile\nfor str(file)\nin\n[]\n]', 3, 5) check('[file for\n str(file) in []]', 2, 2) - check('"\n\nf\'{.}\'"' , 1, 1) # Errors thrown by compile.c check('class foo:return 1', 1, 11) diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py index 7ca1512ebbf1bf..d7143d154a1bc1 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')