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

Erroneous suggestion in print statement #76866

Closed
storymode7 mannequin opened this issue Jan 27, 2018 · 9 comments
Closed

Erroneous suggestion in print statement #76866

storymode7 mannequin opened this issue Jan 27, 2018 · 9 comments
Labels
3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@storymode7
Copy link
Mannequin

storymode7 mannequin commented Jan 27, 2018

BPO 32685
Nosy @ncoghlan, @serhiy-storchaka, @CuriousLearner, @csabella, @nitishch, @storymode7
PRs
  • bpo-32685: Improve suggestion for print statement #5375
  • bpo-32685: Fixes print suggestion hint in case of multiple statements on the same line #5376
  • [3.6] bpo-32685: Improve suggestion for print statement (GH-5375) #5380
  • Files
  • printBug.py
  • 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 2018-01-28.11:33:27.849>
    created_at = <Date 2018-01-27.10:31:56.216>
    labels = ['interpreter-core', 'type-bug', '3.7']
    title = 'Erroneous suggestion in print statement'
    updated_at = <Date 2018-01-28.11:33:27.847>
    user = 'https://github.com/storymode7'

    bugs.python.org fields:

    activity = <Date 2018-01-28.11:33:27.847>
    actor = 'ncoghlan'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-01-28.11:33:27.849>
    closer = 'ncoghlan'
    components = ['Interpreter Core']
    creation = <Date 2018-01-27.10:31:56.216>
    creator = 'storymode7'
    dependencies = []
    files = ['47412']
    hgrepos = []
    issue_num = 32685
    keywords = ['patch']
    message_count = 9.0
    messages = ['310852', '310853', '310855', '310857', '310899', '310920', '310934', '310937', '310938']
    nosy_count = 6.0
    nosy_names = ['ncoghlan', 'serhiy.storchaka', 'CuriousLearner', 'cheryl.sabella', 'nitishch', 'storymode7']
    pr_nums = ['5375', '5376', '5380']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue32685'
    versions = ['Python 3.6', 'Python 3.7']

    @storymode7
    Copy link
    Mannequin Author

    storymode7 mannequin commented Jan 27, 2018

    The suggestion given when a for and print with the syntax for python 2.x are used together seems erroneous.

    Steps to reproduce:
    Just type following in the python interpreter (3.x):

    for p in some_list: print p

    The error produced is:

    SyntaxError: Missing parentheses in call to 'print'. Did you mean print(in some_list: print p)?

    I am also attaching a small file, that produces the error when run with:

    python3 printBug.py

    @storymode7 storymode7 mannequin added 3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Jan 27, 2018
    @csabella
    Copy link
    Contributor

    Thank you for the report. The change to the exception message was added in bpo-30597, but it looks like it may be too greedy. Adding the developer to the nosy list.

    @CuriousLearner
    Copy link
    Member

    Thanks for the report Mayank!

    As we discussed offline, it is indeed reproducible if someone uses print on the same line (such as in this case, where a loop is used).

    I will look into this issue.

    @ncoghlan
    Copy link
    Contributor

    Re-reading the code for _set_legacy_print_statement_msg, I noticed that we're not currently taking the "start" parameter into account, and that's the offset the compiler passes in to tell us where on the line it found the text "print ". Oops :)

    We're also currently going to mishandle code like "x = 1; print x; pass" as well: the suggestion for that is "print(x)", but we would currently suggest "print(print x; pass)".

    Fixing the left offset shouldn't be too difficult, as I believe the correct value for that is just "PRINT_OFFSET + start".

    For the right offset, I think we'll want to drop the initial XStrip call added for bpo-32028 entirely, and instead calculate the right offset as a PyUnicode_FindChar search for ";" (replacing it with the length of the input string if the separator isn't found).

    Any trailing whitespace (either before the ";", if one is present, or before the end of the line otherwise) will then be stripped by the remaining XStrip call (the one after the PyUnicode_Substring call).

    (Even with those improvements, there will still be cases where the heuristic gives an incorrect suggestion due to a syntax error within the expression to be displayed, like "print value:", but there isn't anything simple we can do about those short of trying to compile the calculated suggestion, and I'm not prepared to go that far yet)

    @nitishch
    Copy link
    Mannequin

    nitishch mannequin commented Jan 27, 2018

    For the right offset, I think we'll want to drop the initial XStrip call added for bpo-32028 entirely, and instead calculate the right offset as a PyUnicode_FindChar search for ";" (replacing it with the length of the input string if the separator isn't found).

    Are ';' and whitespace the only allowed characters following the print expression?

    @ncoghlan
    Copy link
    Contributor

    They're the only characters we won't want to include as part of the suggestion, since they indicate the end of the statement.

    @ncoghlan
    Copy link
    Contributor

    New changeset 43c0f1a by Nick Coghlan (Nitish Chandra) in branch 'master':
    bpo-32685: Improve suggestion for print statement (GH-5375)
    43c0f1a

    @ncoghlan
    Copy link
    Contributor

    New changeset b3b4b81 by Nick Coghlan (Miss Islington (bot)) in branch '3.6':
    bpo-32685: Improve suggestion for print statement (GH-5380)
    b3b4b81

    @ncoghlan
    Copy link
    Contributor

    Thanks for the patches, Sanyam & Nitish, and for the original bug report Mayank.

    Thanks also to Cheryl for nudging us to get it resolved :)

    @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.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants