]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-40176: Improve error messages for unclosed string literals (GH-19346)
authorBatuhan Taskaya <isidentical@gmail.com>
Wed, 20 Jan 2021 21:38:47 +0000 (00:38 +0300)
committerGitHub <noreply@github.com>
Wed, 20 Jan 2021 21:38:47 +0000 (13:38 -0800)
Automerge-Triggered-By: GH:isidentical
Include/errcode.h
Lib/test/test_eof.py
Lib/test/test_exceptions.py
Lib/test/test_fstring.py
Misc/NEWS.d/next/Core and Builtins/2021-01-20-22-31-01.bpo-40176.anjyWw.rst [new file with mode: 0644]
Parser/pegen.c
Parser/tokenizer.c

index 790518b8b7730ed5e24a64d0e99f9c921b48d8ec..f2671d6c9b30b41543a419b3b68893ad9d9866b8 100644 (file)
@@ -26,8 +26,6 @@ extern "C" {
 #define E_TOODEEP       20      /* Too many indentation levels */
 #define E_DEDENT        21      /* No matching outer block for dedent */
 #define E_DECODE        22      /* Error in decoding into Unicode */
-#define E_EOFS          23      /* EOF in triple-quoted string */
-#define E_EOLS          24      /* EOL in single-quoted string */
 #define E_LINECONT      25      /* Unexpected characters after a line continuation */
 #define E_BADSINGLE     27      /* Ill-formed single statement input */
 
index 2cf263d27463c4ba8999b7f5c9f519051678c3da..b370e27161cee65b51cd0f33e258ff52b474b8c5 100644 (file)
@@ -7,23 +7,25 @@ from test.support import script_helper
 import unittest
 
 class EOFTestCase(unittest.TestCase):
-    def test_EOFC(self):
-        expect = "EOL while scanning string literal (<string>, line 1)"
-        try:
-            eval("""'this is a test\
-            """)
-        except SyntaxError as msg:
-            self.assertEqual(str(msg), expect)
-        else:
-            raise support.TestFailed
+    def test_EOF_single_quote(self):
+        expect = "unterminated string literal (detected at line 1) (<string>, line 1)"
+        for quote in ("'", "\""):
+            try:
+                eval(f"""{quote}this is a test\
+                """)
+            except SyntaxError as msg:
+                self.assertEqual(str(msg), expect)
+                self.assertEqual(msg.offset, 1)
+            else:
+                raise support.TestFailed
 
     def test_EOFS(self):
-        expect = ("EOF while scanning triple-quoted string literal "
-                  "(<string>, line 1)")
+        expect = ("unterminated triple-quoted string literal (detected at line 1) (<string>, line 1)")
         try:
             eval("""'''this is a test""")
         except SyntaxError as msg:
             self.assertEqual(str(msg), expect)
+            self.assertEqual(msg.offset, 1)
         else:
             raise support.TestFailed
 
index eb70d7b4e4972418be7f61aeafa957af4d003a73..21878c39f4fec9478279e5de223c82b7b5bc0c01 100644 (file)
@@ -206,7 +206,7 @@ class ExceptionTests(unittest.TestCase):
         check(b'# -*- coding: cp1251 -*-\nPython = "\xcf\xb3\xf2\xee\xed" +',
               2, 19, encoding='cp1251')
         check(b'Python = "\xcf\xb3\xf2\xee\xed" +', 1, 18)
-        check('x = "a', 1, 7)
+        check('x = "a', 1, 5)
         check('lambda x: x = 2', 1, 1)
         check('f{a + b + c}', 1, 2)
         check('[file for str(file) in []\n])', 1, 11)
@@ -238,7 +238,7 @@ class ExceptionTests(unittest.TestCase):
 
             def baz():
                 '''quux'''
-            """, 9, 20)
+            """, 9, 24)
         check("pass\npass\npass\n(1+)\npass\npass\npass", 4, 4)
         check("(1+)", 1, 4)
 
index 2345832abce6242f42849db8872308e00ddafdfa..7ca1512ebbf1bf4eac83cc3be4966a495890431a 100644 (file)
@@ -661,7 +661,7 @@ x = (
                             ["f'{3)+(4}'",
                              ])
 
-        self.assertAllRaise(SyntaxError, 'EOL while scanning string literal',
+        self.assertAllRaise(SyntaxError, 'unterminated string literal',
                             ["f'{\n}'",
                              ])
 
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-01-20-22-31-01.bpo-40176.anjyWw.rst b/Misc/NEWS.d/next/Core and Builtins/2021-01-20-22-31-01.bpo-40176.anjyWw.rst
new file mode 100644 (file)
index 0000000..df7de3b
--- /dev/null
@@ -0,0 +1,2 @@
+Syntax errors for unterminated string literals now point to the start
+of the string instead of reporting EOF/EOL.
index 0d39030ea6ed180c8f0630461844ddf79898b632..0e7f86bc99e4514825e804f9db61ee1788f1e130 100644 (file)
@@ -327,12 +327,6 @@ tokenizer_error(Parser *p)
         case E_TOKEN:
             msg = "invalid token";
             break;
-        case E_EOFS:
-            RAISE_SYNTAX_ERROR("EOF while scanning triple-quoted string literal");
-            return -1;
-        case E_EOLS:
-            RAISE_SYNTAX_ERROR("EOL while scanning string literal");
-            return -1;
         case E_EOF:
             if (p->tok->level) {
                 raise_unclosed_parentheses_error(p);
index d3e846c0a5a1268690912cab99c9286b1d507ece..d9334aaf148ba205221af36a2f90903d308430f0 100644 (file)
@@ -1739,20 +1739,26 @@ tok_get(struct tok_state *tok, const char **p_start, const char **p_end)
         /* Get rest of string */
         while (end_quote_size != quote_size) {
             c = tok_nextc(tok);
-            if (c == EOF) {
+            if (c == EOF || (quote_size == 1 && c == '\n')) {
+                // shift the tok_state's location into
+                // the start of string, and report the error
+                // from the initial quote character
+                tok->cur = (char *)tok->start;
+                tok->cur++;
+                tok->line_start = tok->multi_line_start;
+                int start = tok->lineno;
+                tok->lineno = tok->first_lineno;
+
                 if (quote_size == 3) {
-                    tok->done = E_EOFS;
+                    return syntaxerror(tok,
+                                       "unterminated triple-quoted string literal"
+                                       " (detected at line %d)", start);
                 }
                 else {
-                    tok->done = E_EOLS;
+                    return syntaxerror(tok,
+                                       "unterminated string literal (detected at"
+                                       " line %d)", start);
                 }
-                tok->cur = tok->inp;
-                return ERRORTOKEN;
-            }
-            if (quote_size == 1 && c == '\n') {
-                tok->done = E_EOLS;
-                tok->cur = tok->inp;
-                return ERRORTOKEN;
             }
             if (c == quote) {
                 end_quote_size += 1;