Skip to content

Commit 4090151

Browse files
authored
bpo-42986: Fix parser crash when reporting syntax errors in f-string with newlines (pythonGH-24279)
1 parent a1e9a1e commit 4090151

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

Lib/test/test_fstring.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,9 @@ def test_parens_in_expressions(self):
664664
self.assertAllRaise(SyntaxError, 'unterminated string literal',
665665
["f'{\n}'",
666666
])
667+
def test_newlines_before_syntax_error(self):
668+
self.assertAllRaise(SyntaxError, "invalid syntax",
669+
["f'{.}'", "\nf'{.}'", "\n\nf'{.}'"])
667670

668671
def test_backslashes_in_string_part(self):
669672
self.assertEqual(f'\t', '\t')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix parser crash when reporting syntax errors in f-string with newlines.
2+
Patch by Pablo Galindo.

Parser/pegen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
454454
does not physically exist */
455455
assert(p->tok->fp == NULL || p->tok->fp == stdin || p->tok->done == E_EOF);
456456

457-
if (p->tok->lineno == lineno) {
457+
if (p->tok->lineno <= lineno) {
458458
Py_ssize_t size = p->tok->inp - p->tok->buf;
459459
error_line = PyUnicode_DecodeUTF8(p->tok->buf, size, "replace");
460460
}

0 commit comments

Comments
 (0)