Skip to content

Commit 5b94f35

Browse files
asottilepablogsal
authored andcommitted
Fix SyntaxError indicator printing too many spaces for multi-line strings (GH-14433)
1 parent e1b9002 commit 5b94f35

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

Lib/test/test_cmd_line_script.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,20 @@ def test_syntaxerror_indented_caret_position(self):
613613
self.assertNotIn("\f", text)
614614
self.assertIn("\n 1 + 1 = 2\n ^", text)
615615

616+
def test_syntaxerror_multi_line_fstring(self):
617+
script = 'foo = f"""{}\nfoo"""\n'
618+
with support.temp_dir() as script_dir:
619+
script_name = _make_test_script(script_dir, 'script', script)
620+
exitcode, stdout, stderr = assert_python_failure(script_name)
621+
self.assertEqual(
622+
stderr.splitlines()[-3:],
623+
[
624+
b' foo = f"""{}',
625+
b' ^',
626+
b'SyntaxError: f-string: empty expression not allowed',
627+
],
628+
)
629+
616630
def test_consistent_sys_path_for_direct_execution(self):
617631
# This test case ensures that the following all give the same
618632
# sys.path configuration:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix ``SyntaxError`` indicator printing too many spaces for multi-line strings - by Anthony Sottile.

Parser/tokenizer.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,7 @@ tok_nextc(struct tok_state *tok)
956956
while (!done) {
957957
Py_ssize_t curstart = tok->start == NULL ? -1 :
958958
tok->start - tok->buf;
959+
Py_ssize_t cur_multi_line_start = tok->multi_line_start - tok->buf;
959960
Py_ssize_t curvalid = tok->inp - tok->buf;
960961
Py_ssize_t newsize = curvalid + BUFSIZ;
961962
char *newbuf = tok->buf;
@@ -968,6 +969,7 @@ tok_nextc(struct tok_state *tok)
968969
}
969970
tok->buf = newbuf;
970971
tok->cur = tok->buf + cur;
972+
tok->multi_line_start = tok->buf + cur_multi_line_start;
971973
tok->line_start = tok->cur;
972974
tok->inp = tok->buf + curvalid;
973975
tok->end = tok->buf + newsize;

0 commit comments

Comments
 (0)