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

Output of syntax error in f-string contains wrong filename #83145

Closed
ErikCederstrand mannequin opened this issue Dec 4, 2019 · 9 comments
Closed

Output of syntax error in f-string contains wrong filename #83145

ErikCederstrand mannequin opened this issue Dec 4, 2019 · 9 comments
Assignees
Labels
3.10 only security fixes type-bug An unexpected behavior, bug, or error

Comments

@ErikCederstrand
Copy link
Mannequin

ErikCederstrand mannequin commented Dec 4, 2019

BPO 38964
Nosy @ericvsmith, @lysnikolaou, @pablogsal, @isidentical
PRs
  • bpo-38964: Print correct filename on a SyntaxError in an fstring #20399
  • [3.9] bpo-38964: Print correct filename on a SyntaxError in an fstring (GH-20399) #20404
  • 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 = 'https://github.com/ericvsmith'
    closed_at = <Date 2020-05-26.06:51:28.491>
    created_at = <Date 2019-12-04.08:27:53.640>
    labels = ['type-bug', '3.10']
    title = 'Output of syntax error in f-string contains wrong filename'
    updated_at = <Date 2020-05-26.06:51:28.491>
    user = 'https://bugs.python.org/ErikCederstrand'

    bugs.python.org fields:

    activity = <Date 2020-05-26.06:51:28.491>
    actor = 'lys.nikolaou'
    assignee = 'eric.smith'
    closed = True
    closed_date = <Date 2020-05-26.06:51:28.491>
    closer = 'lys.nikolaou'
    components = []
    creation = <Date 2019-12-04.08:27:53.640>
    creator = 'Erik Cederstrand'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 38964
    keywords = ['patch']
    message_count = 9.0
    messages = ['357777', '357780', '357801', '357804', '357805', '357806', '357821', '369933', '369936']
    nosy_count = 5.0
    nosy_names = ['eric.smith', 'Erik Cederstrand', 'lys.nikolaou', 'pablogsal', 'BTaskaya']
    pr_nums = ['20399', '20404']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue38964'
    versions = ['Python 3.10']

    @ErikCederstrand
    Copy link
    Mannequin Author

    ErikCederstrand mannequin commented Dec 4, 2019

    When I have a normal syntax error in a file, Python reports the filename in the exception output:

    $ cat syntax_error.py 
    0x=5
    
    $ python3.8 syntax_error.py 
      File "syntax_error.py", line 1
        0x=5
         ^
    SyntaxError: invalid hexadecimal literal

    But if the syntax error is inside an f-string, Python reports 'File "<fstring>"' instead of the actual filename in the exception output.

    $ cat syntax_error_in_fstring.py 
    f'This is a syntax error: {0x=5}'
    
    $ python3.8 syntax_error_in_fstring.py 
      File "<fstring>", line 1
    SyntaxError: invalid hexadecimal literal

    @ErikCederstrand ErikCederstrand mannequin added 3.8 only security fixes type-bug An unexpected behavior, bug, or error labels Dec 4, 2019
    @ErikCederstrand
    Copy link
    Mannequin Author

    ErikCederstrand mannequin commented Dec 4, 2019

    Additionally, the output in the 2nd example does not contain the helpful text printing the context and location of the code containing the syntax error.

    @isidentical
    Copy link
    Sponsor Member

    This is actually specified behavior in PEP-498

    https://www.python.org/dev/peps/pep-0498/#expression-evaluation

    Expressions are parsed with the equivalent of ast.parse('(' + expression + ')', '<fstring>', 'eval')

    @pablogsal
    Copy link
    Member

    Although Batuhan is correct maybe we can consider trying to improve the debugging experience off-strings due to the fact that these errors in a big project may be difficult to find. Although you have always the back-trace on the other side.

    @pablogsal pablogsal reopened this Dec 4, 2019
    @ericvsmith
    Copy link
    Member

    I agree that although the PEP says that's how it behaves, that shouldn't be prescriptive of the error message. Clearly we can be more helpful here.

    I have a large, elaborate re-write of the error generating code that I've been working on, on and off for over a year, which would address this problem. I'm not sure when or if I'll finish it. It touches a lot of things, and my branch won't merge cleanly any more.

    Additionally, the idea of moving f-strings into the Python grammar would also fix this problem. That might be a better way forward, although it has a number of downsides, too.

    @ericvsmith ericvsmith self-assigned this Dec 4, 2019
    @pablogsal
    Copy link
    Member

    Additionally, the idea of moving f-strings into the Python grammar would also fix this problem. That might be a better way forward, although it has a number of downsides, too.

    For considerin this path we would need to wait to see first the future of the new experiment with the PEG parser to avoid potentially throwing away all the work in the parser and the tokenizer. Also, this approach will likely entail much more changes than reworking the error reporting code.

    @ericvsmith
    Copy link
    Member

    Yes, moving f-strings to the grammar would be a huge change, and not just for CPython.

    I discussed it at the last PyCon with the authors of various editors (for syntax highlighting) and other tools that parse python code. No one was hugely opposed to it, and I think even one person was very excited about it. But it's not a step to be taken lightly. I don't think I've discussed it with any other Python implementors outside of CPython.

    My biggest concern is that it makes naive string recognition fail. For example, this would become a valid f-string:
    f'{fn('some string')}'
    while
    '{fn('some string')}'
    is not a valid string.

    I now I think I've completely derailed this bug report. I think the action item here is for me to finish up my better error reporting.

    @lysnikolaou lysnikolaou added 3.10 only security fixes and removed 3.8 only security fixes labels May 25, 2020
    @pablogsal
    Copy link
    Member

    New changeset f7b1e46 by Lysandros Nikolaou in branch 'master':
    bpo-38964: Print correct filename on a SyntaxError in an fstring (GH-20399)
    f7b1e46

    @pablogsal
    Copy link
    Member

    New changeset 791a46e by Lysandros Nikolaou in branch '3.9':
    [3.9] bpo-38964: Print correct filename on a SyntaxError in an fstring (GH-20399) (GH-20404)
    791a46e

    @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 type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants