]> granicus.if.org Git - python/commitdiff
Fix `SyntaxError` indicator printing too many spaces for multi-line strings (GH-14433)
authorAnthony Sottile <asottile@umich.edu>
Mon, 29 Jul 2019 13:59:13 +0000 (06:59 -0700)
committerPablo Galindo <Pablogsal@gmail.com>
Mon, 29 Jul 2019 13:59:13 +0000 (14:59 +0100)
Lib/test/test_cmd_line_script.py
Misc/NEWS.d/next/Core and Builtins/2019-06-27-15-01-14.bpo-37433.amNGqr.rst [new file with mode: 0644]
Parser/tokenizer.c

index 4677e60c8116c51ead1a8e8dbe95a3d4dede2909..633e0fd746f5edb515efb102a7aa608e5588e423 100644 (file)
@@ -613,6 +613,20 @@ class CmdLineTest(unittest.TestCase):
             self.assertNotIn("\f", text)
             self.assertIn("\n    1 + 1 = 2\n    ^", text)
 
+    def test_syntaxerror_multi_line_fstring(self):
+        script = 'foo = f"""{}\nfoo"""\n'
+        with support.temp_dir() as script_dir:
+            script_name = _make_test_script(script_dir, 'script', script)
+            exitcode, stdout, stderr = assert_python_failure(script_name)
+            self.assertEqual(
+                stderr.splitlines()[-3:],
+                [
+                    b'    foo = f"""{}',
+                    b'          ^',
+                    b'SyntaxError: f-string: empty expression not allowed',
+                ],
+            )
+
     def test_consistent_sys_path_for_direct_execution(self):
         # This test case ensures that the following all give the same
         # sys.path configuration:
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-06-27-15-01-14.bpo-37433.amNGqr.rst b/Misc/NEWS.d/next/Core and Builtins/2019-06-27-15-01-14.bpo-37433.amNGqr.rst
new file mode 100644 (file)
index 0000000..794ddbb
--- /dev/null
@@ -0,0 +1 @@
+Fix ``SyntaxError`` indicator printing too many spaces for multi-line strings - by Anthony Sottile.
index c2ec659fed888f769febaa302b90a4d63d009e97..31fe970c900382e681117285a44ea64f30d1945c 100644 (file)
@@ -956,6 +956,7 @@ tok_nextc(struct tok_state *tok)
             while (!done) {
                 Py_ssize_t curstart = tok->start == NULL ? -1 :
                           tok->start - tok->buf;
+                Py_ssize_t cur_multi_line_start = tok->multi_line_start - tok->buf;
                 Py_ssize_t curvalid = tok->inp - tok->buf;
                 Py_ssize_t newsize = curvalid + BUFSIZ;
                 char *newbuf = tok->buf;
@@ -968,6 +969,7 @@ tok_nextc(struct tok_state *tok)
                 }
                 tok->buf = newbuf;
                 tok->cur = tok->buf + cur;
+                tok->multi_line_start = tok->buf + cur_multi_line_start;
                 tok->line_start = tok->cur;
                 tok->inp = tok->buf + curvalid;
                 tok->end = tok->buf + newsize;