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

This commit is contained in:
Pablo Galindo 2021-01-31 22:48:23 +00:00 committed by GitHub
parent a1e9a1e120
commit 4090151816
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 1 deletions

View file

@ -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')

View file

@ -0,0 +1,2 @@
Fix parser crash when reporting syntax errors in f-string with newlines.
Patch by Pablo Galindo.

View 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");
}