Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REPL requests another line despite syntax error #88367

Closed
gvanrossum opened this issue May 21, 2021 · 8 comments
Closed

REPL requests another line despite syntax error #88367

gvanrossum opened this issue May 21, 2021 · 8 comments
Labels
3.10 only security fixes build The build process and cross-build

Comments

@gvanrossum
Copy link
Member

BPO 44201
Nosy @gvanrossum, @aroberge, @lysnikolaou, @pablogsal, @miss-islington
PRs
  • bpo-44201: Avoid side effects of "invalid_*" rules in the REPL #26298
  • [3.10] bpo-44201: Avoid side effects of "invalid_*" rules in the REPL (GH-26298) #26313
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-05-22.22:06:02.284>
    created_at = <Date 2021-05-21.00:34:22.722>
    labels = ['build', '3.10']
    title = 'REPL requests another line despite syntax error'
    updated_at = <Date 2021-05-22.22:23:29.788>
    user = 'https://github.com/gvanrossum'

    bugs.python.org fields:

    activity = <Date 2021-05-22.22:23:29.788>
    actor = 'pablogsal'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-05-22.22:06:02.284>
    closer = 'pablogsal'
    components = []
    creation = <Date 2021-05-21.00:34:22.722>
    creator = 'gvanrossum'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 44201
    keywords = ['patch', '3.10regression']
    message_count = 8.0
    messages = ['394087', '394091', '394101', '394109', '394110', '394142', '394149', '394191']
    nosy_count = 5.0
    nosy_names = ['gvanrossum', 'aroberge', 'lys.nikolaou', 'pablogsal', 'miss-islington']
    pr_nums = ['26298', '26313']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'compile error'
    url = 'https://bugs.python.org/issue44201'
    versions = ['Python 3.10']

    @gvanrossum
    Copy link
    Member Author

    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
    >>>

    @gvanrossum gvanrossum added 3.10 only security fixes build The build process and cross-build labels May 21, 2021
    @pablogsal
    Copy link
    Member

    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 :(

    @aroberge
    Copy link
    Mannequin

    aroberge mannequin commented May 21, 2021

    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?

    @pablogsal
    Copy link
    Member

    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

    @pablogsal
    Copy link
    Member

    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

    @gvanrossum
    Copy link
    Member Author

    Could we implement something in the REPL where it refuses to read another
    line when asked for a token in an error recovery rule?

    @pablogsal
    Copy link
    Member

    Yes, I was exactly working in that :)

    @pablogsal
    Copy link
    Member

    New changeset 1fb6b9e by Miss Islington (bot) in branch '3.10':
    bpo-44201: Avoid side effects of "invalid_*" rules in the REPL (GH-26298) (GH-26313)
    1fb6b9e

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes build The build process and cross-build
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants