Skip to content

Commit

Permalink
bpo-40267: Fix message when last input character produces a SyntaxErr…
Browse files Browse the repository at this point in the history
…or (pythonGH-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.
  • Loading branch information
lysnikolaou authored Apr 15, 2020
1 parent 574547a commit 9a4b38f
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Include/token.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Lib/test/test_fstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ def test_lambda(self):

# 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}'",
])

Expand Down
Original file line number Diff line number Diff line change
@@ -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`.
3 changes: 3 additions & 0 deletions Parser/parsetok.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions Tools/scripts/generate_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ def update_file(file, content):
#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 */
Expand Down

0 comments on commit 9a4b38f

Please sign in to comment.