]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-40267: Fix message when last input character produces a SyntaxError (GH-19521)
authorLysandros Nikolaou <lisandrosnik@gmail.com>
Wed, 15 Apr 2020 18:22:10 +0000 (21:22 +0300)
committerGitHub <noreply@github.com>
Wed, 15 Apr 2020 18:22:10 +0000 (11:22 -0700)
When there is a SyntaxError after reading the last input character from
the tokenizer and if no newline follows it, the error message used to be
`unexpected EOF while parsing`, which is wrong.

Include/token.h
Lib/test/test_fstring.py
Misc/NEWS.d/next/Core and Builtins/2020-04-14-18-54-50.bpo-40267.Q2N6Bw.rst [new file with mode: 0644]
Parser/parsetok.c
Tools/scripts/generate_token.py

index e08708baf196e8b1a63bb841f4c1ef8137aa9f4f..9b8a3aae0746747ed90a86e435ec0594390e0404 100644 (file)
@@ -78,6 +78,10 @@ extern "C" {
 #define ISTERMINAL(x)           ((x) < NT_OFFSET)
 #define ISNONTERMINAL(x)        ((x) >= NT_OFFSET)
 #define ISEOF(x)                ((x) == ENDMARKER)
+#define ISWHITESPACE(x)         ((x) == ENDMARKER || \
+                                 (x) == NEWLINE   || \
+                                 (x) == INDENT    || \
+                                 (x) == DEDENT)
 
 
 PyAPI_DATA(const char * const) _PyParser_TokenNames[]; /* Token names */
index 8fd7cf09a99f45ada36bc8b70b8bac0693e7d51e..fe465b7e1d43dc7bb5ae264b8b7929f4ab5d6c03 100644 (file)
@@ -713,7 +713,7 @@ non-important content
 
         # lambda doesn't work without parens, because the colon
         #  makes the parser think it's a format_spec
-        self.assertAllRaise(SyntaxError, 'unexpected EOF while parsing',
+        self.assertAllRaise(SyntaxError, 'invalid syntax',
                             ["f'{lambda x:x}'",
                              ])
 
diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-04-14-18-54-50.bpo-40267.Q2N6Bw.rst b/Misc/NEWS.d/next/Core and Builtins/2020-04-14-18-54-50.bpo-40267.Q2N6Bw.rst
new file mode 100644 (file)
index 0000000..a778594
--- /dev/null
@@ -0,0 +1 @@
+Fix the tokenizer to display the correct error message, when there is a SyntaxError on the last input character and no newline follows. It used to be `unexpected EOF while parsing`, while it should be `invalid syntax`.
\ No newline at end of file
index cb9472150f2ca8656074b8240d4b0912c7759053..37ca65c275a5817092c2b69b58188f0171b09118 100644 (file)
@@ -332,6 +332,9 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret,
              PyParser_AddToken(ps, (int)type, str,
                                lineno, col_offset, tok->lineno, end_col_offset,
                                &(err_ret->expected))) != E_OK) {
+            if (tok->done == E_EOF && !ISWHITESPACE(type)) {
+                tok->done = E_SYNTAX;
+            }
             if (err_ret->error != E_DONE) {
                 PyObject_FREE(str);
                 err_ret->token = type;
index f2745e8353fc3777da8240da850ea8605fc4daad..77bb5bd5eca02c1dc5482eb7319559d113d38cc3 100755 (executable)
@@ -69,6 +69,10 @@ extern "C" {
 #define ISTERMINAL(x)           ((x) < NT_OFFSET)
 #define ISNONTERMINAL(x)        ((x) >= NT_OFFSET)
 #define ISEOF(x)                ((x) == ENDMARKER)
+#define ISWHITESPACE(x)         ((x) == ENDMARKER || \\
+                                 (x) == NEWLINE   || \\
+                                 (x) == INDENT    || \\
+                                 (x) == DEDENT)
 
 
 PyAPI_DATA(const char * const) _PyParser_TokenNames[]; /* Token names */