This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: REPL requests another line despite syntax error
Type: compile error Stage: resolved
Components: Versions: Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: aroberge, gvanrossum, lys.nikolaou, miss-islington, pablogsal
Priority: normal Keywords: 3.10regression, patch

Created on 2021-05-21 00:34 by gvanrossum, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 26298 merged pablogsal, 2021-05-21 20:14
PR 26313 merged miss-islington, 2021-05-22 22:05
Messages (8)
msg394087 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-05-21 00:34
This seems a regression from 3.9.

>>> foo[x = 1
... ]
  File "<stdin>", line 1
    foo[x = 1
        ^^^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
>>>

Note how the syntax error is in line 1 and yet the REPL asks for more input (the "..." prompt) and doesn't stop until I type "]".

In 3.9 I just get "SyntaxError: invalid syntax" but the REPL doesn't ask for the second line:

>>> foo[x = 1
  File "<stdin>", line 1
    foo[x = 1
          ^
SyntaxError: invalid syntax
>>>
msg394091 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-05-21 01:07
Unfortunately this happens because to generate the error message we check for the equal and the right hand side of the equal (to also discard some false positives) and this triggers the parser to ask for additional tokens. Notice that you don't need to close the ']', just provide an additional token:

>>> x = [x=4
... f
  File "<stdin>", line 1
    x = [x=4
         ^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

Unfortunately is not going to be easy to solve without looking at the right hand side :(
msg394101 - (view) Author: Andre Roberge (aroberge) * Date: 2021-05-21 10:11
I believe that this is similar to, but not quite as severe as a similar bug in code.interact() https://bugs.python.org/issue43366 which affects IDLE; perhaps fixing this might fix the other issue?
msg394109 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-05-21 12:15
No, this issue is in the parser while the other is caused by the code module. As I mentioned, this case doesn't ask you for tokens until you close the brace, it just ask you for one extra token because is doing a lookahead.

If you give any extra token to satisfy the lookahead:

>>> foo[x = 1 $
  File "<stdin>", line 1
    foo[x = 1 $
        ^^^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
>>>

It fails immediately even if you didn't close the bracket
msg394110 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-05-21 12:16
I will try to see if we can do something about this but the only thing I can think of is removing the lookaheads in the invalid comparison rules but then we need to solve the false positives (like keyword arguments) and that's going to be very hard to do without lookaheads
msg394142 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-05-21 19:17
Could we implement something in the REPL where it refuses to read another
line when asked for a token in an error recovery rule?
msg394149 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-05-21 20:10
Yes, I was exactly working in that :)
msg394191 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-05-22 22:23
New changeset 1fb6b9e91d8685f7eca0fc33402589c65723bd94 by Miss Islington (bot) in branch '3.10':
bpo-44201: Avoid side effects of "invalid_*" rules in the REPL (GH-26298) (GH-26313)
https://github.com/python/cpython/commit/1fb6b9e91d8685f7eca0fc33402589c65723bd94
History
Date User Action Args
2022-04-11 14:59:45adminsetgithub: 88367
2021-05-22 22:23:29pablogsalsetmessages: + msg394191
2021-05-22 22:06:02pablogsalsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-05-22 22:05:08miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request24911
2021-05-21 20:14:22pablogsalsetkeywords: + patch
stage: patch review
pull_requests: + pull_request24901
2021-05-21 20:10:07pablogsalsetmessages: + msg394149
2021-05-21 19:17:02gvanrossumsetmessages: + msg394142
2021-05-21 12:16:57pablogsalsetmessages: + msg394110
2021-05-21 12:15:06pablogsalsetmessages: + msg394109
2021-05-21 10:11:49arobergesetnosy: + aroberge
messages: + msg394101
2021-05-21 01:07:01pablogsalsetmessages: + msg394091
2021-05-21 00:34:53gvanrossumsetkeywords: + 3.10regression
2021-05-21 00:34:42gvanrossumsetnosy: + lys.nikolaou, pablogsal
2021-05-21 00:34:22gvanrossumcreate