Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9a4b38f

Browse files
authored
bpo-40267: Fix message when last input character produces a SyntaxError (GH-19521)
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.
1 parent 574547a commit 9a4b38f

5 files changed

Lines changed: 13 additions & 1 deletion

File tree

Include/token.h

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_fstring.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ def test_lambda(self):
713713

714714
# lambda doesn't work without parens, because the colon
715715
# makes the parser think it's a format_spec
716-
self.assertAllRaise(SyntaxError, 'unexpected EOF while parsing',
716+
self.assertAllRaise(SyntaxError, 'invalid syntax',
717717
["f'{lambda x:x}'",
718718
])
719719

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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`.

Parser/parsetok.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,9 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret,
332332
PyParser_AddToken(ps, (int)type, str,
333333
lineno, col_offset, tok->lineno, end_col_offset,
334334
&(err_ret->expected))) != E_OK) {
335+
if (tok->done == E_EOF && !ISWHITESPACE(type)) {
336+
tok->done = E_SYNTAX;
337+
}
335338
if (err_ret->error != E_DONE) {
336339
PyObject_FREE(str);
337340
err_ret->token = type;

Tools/scripts/generate_token.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ def update_file(file, content):
6969
#define ISTERMINAL(x) ((x) < NT_OFFSET)
7070
#define ISNONTERMINAL(x) ((x) >= NT_OFFSET)
7171
#define ISEOF(x) ((x) == ENDMARKER)
72+
#define ISWHITESPACE(x) ((x) == ENDMARKER || \\
73+
(x) == NEWLINE || \\
74+
(x) == INDENT || \\
75+
(x) == DEDENT)
7276
7377
7478
PyAPI_DATA(const char * const) _PyParser_TokenNames[]; /* Token names */

0 commit comments

Comments
 (0)