Skip to content

Commit cf52bd0

Browse files
Fix SyntaxError indicator printing too many spaces for multi-line strings (GH-14433)
(cherry picked from commit 5b94f35) Co-authored-by: Anthony Sottile <asottile@umich.edu>
1 parent 36fd7b6 commit cf52bd0

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
@@ -601,6 +601,20 @@ def test_syntaxerror_indented_caret_position(self):
601601
self.assertNotIn("\f", text)
602602
self.assertIn("\n 1 + 1 = 2\n ^", text)
603603

604+
def test_syntaxerror_multi_line_fstring(self):
605+
script = 'foo = f"""{}\nfoo"""\n'
606+
with support.temp_dir() as script_dir:
607+
script_name = _make_test_script(script_dir, 'script', script)
608+
exitcode, stdout, stderr = assert_python_failure(script_name)
609+
self.assertEqual(
610+
stderr.splitlines()[-3:],
611+
[
612+
b' foo = f"""{}',
613+
b' ^',
614+
b'SyntaxError: f-string: empty expression not allowed',
615+
],
616+
)
617+
604618
def test_consistent_sys_path_for_direct_execution(self):
605619
# This test case ensures that the following all give the same
606620
# 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)