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

Show expected input for right shift operator usage in custom "print" error message #74906

Closed
CuriousLearner opened this issue Jun 21, 2017 · 12 comments
Labels
3.7 (EOL) end of life type-feature A feature request or enhancement

Comments

@CuriousLearner
Copy link
Member

BPO 30721
Nosy @terryjreedy, @pfmoore, @ncoghlan, @tjguk, @zware, @serhiy-storchaka, @zooba, @CuriousLearner
PRs
  • bpo-30721: Show correct syntax hint in Py3 when using Py2 redirection syntax #2345
  • bpo-30721: Addresses minor fixes of adding ? to redirection syntax #3131
  • [3.6] bpo-31232: Backport custom print rshift message #3155
  • 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 2017-08-18.12:19:05.924>
    created_at = <Date 2017-06-21.05:13:56.910>
    labels = ['type-feature', '3.7']
    title = 'Show expected input for right shift operator usage in custom "print" error message'
    updated_at = <Date 2017-08-19.06:59:41.955>
    user = 'https://github.com/CuriousLearner'

    bugs.python.org fields:

    activity = <Date 2017-08-19.06:59:41.955>
    actor = 'ncoghlan'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-08-18.12:19:05.924>
    closer = 'ncoghlan'
    components = []
    creation = <Date 2017-06-21.05:13:56.910>
    creator = 'CuriousLearner'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 30721
    keywords = []
    message_count = 12.0
    messages = ['296518', '296525', '299128', '299139', '299238', '300484', '300485', '300487', '300488', '300492', '300493', '300569']
    nosy_count = 8.0
    nosy_names = ['terry.reedy', 'paul.moore', 'ncoghlan', 'tim.golden', 'zach.ware', 'serhiy.storchaka', 'steve.dower', 'CuriousLearner']
    pr_nums = ['2345', '3131', '3155']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue30721'
    versions = ['Python 3.7']

    @CuriousLearner
    Copy link
    Member Author

    While working on issue: http://bugs.python.org/issue30597 to enhance the custom error message by showing expected output in Python3 syntax when someone uses Python2 syntax, a PR was raised: #2009, where we just handled the case for print with soft-space and excessive white-space.

    In the implementation discussion, an issue was raised to handle the case with right shift operator by Nick as in here:
    http://bugs.python.org/issue30597#msg295484

    Nick suggested here about the possible patch: #2009 (comment)

    @CuriousLearner CuriousLearner added 3.7 (EOL) end of life type-feature A feature request or enhancement labels Jun 21, 2017
    @ncoghlan
    Copy link
    Contributor

    The specific error in question here is the one where Python 3 reads the old Python 2 stream redirection syntax as a combination of the right shift operator and tuple creation:

    >>> print >> sys.stderr, "message"
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for >>: 'builtin_function_or_method' and '_io.TextIOWrapper'
    

    Searching for that error message indicates that people are hitting it reasonably frequently, so we may be able to save them a trip to Google by adding a custom 'Did you mean "print(<output>, file={:100!r})'.format(rhs)"? message when the right-shift operand dispatch is about to report the default "no implementation" type error, and the LHS is the print builtin.

    @terryjreedy
    Copy link
    Member

    The test added in the PR passes on linux (Travis) and Mac (op's machine), fails on Windows (Appveyor). The patch itself modifies abstract.c. Could some Windows expert take a look?

    test_string_with_stream_redirection (test.test_print.TestPy2MigrationHint) ... FAIL
    FAIL: test_string_with_stream_redirection (test.test_print.TestPy2MigrationHint)

    Traceback (most recent call last):
    File "C:\projects\cpython\lib\test\test_print.py", line 165, in test_string_with_stream_redirection
    'file=<output_stream>)', str(context.exception))
    AssertionError: 'Did you mean "print(, file=<output_stream>)' not found in "unsupported operand type(s) for >>: 'builtin_function_or_method' and '_io.StringIO'"

    @zooba
    Copy link
    Member

    zooba commented Jul 25, 2017

    The "op_slot == 96" looks suspicious - how is this value actually supposed to be calculated?

    @ncoghlan
    Copy link
    Contributor

    Checking how we do it elsewhere, NB_SLOT(nb_rshift) looks like the right replacement.

    That's a compiler-dependent struct field offset calculation, so a discrepancy there could easily be the cause of a Windows-only failure.

    @serhiy-storchaka
    Copy link
    Member

    New changeset 5e2eb35 by Serhiy Storchaka (Sanyam Khurana) in branch 'master':
    bpo-30721: Show correct syntax hint in Py3 when using Py2 redirection syntax (bpo-2345)
    5e2eb35

    @ncoghlan
    Copy link
    Contributor

    Checking the merged implementation locally, I belatedly noticed that we forgot the trailing question mark on the question:

    >>> import sys
    >>> print >> sys.stderr
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for >>: 'builtin_function_or_method' and '_io.TextIOWrapper'. Did you mean "print(<message>, file=<output_stream>)"
    

    So putting this back to "needs patch" (no NEWS entry needed, just the missing question marks in the patch and the tests).

    @CuriousLearner
    Copy link
    Member Author

    Ah, sorry for that. I'll just fix that right away in a few mins :)

    @ncoghlan
    Copy link
    Contributor

    Note that I filed a separate issue to ask Ned about potentially backporting this to 3.6: https://bugs.python.org/issue31232

    @ncoghlan
    Copy link
    Contributor

    New changeset a7c449b by Nick Coghlan (Sanyam Khurana) in branch 'master':
    bpo-30721: Add missing '?' to new error message (GH-3131)
    a7c449b

    @ncoghlan
    Copy link
    Contributor

    And done - thanks folks!

    @ncoghlan
    Copy link
    Contributor

    New changeset 1a05e87 by Nick Coghlan in branch '3.6':
    [3.6] bpo-31232: Backport custom print rshift message (GH-3155)
    1a05e87

    @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 type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants