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: Show expected input for right shift operator usage in custom "print" error message
Type: enhancement Stage: resolved
Components: Versions: Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: CuriousLearner, ncoghlan, paul.moore, serhiy.storchaka, steve.dower, terry.reedy, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2017-06-21 05:13 by CuriousLearner, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 2345 merged CuriousLearner, 2017-06-23 05:27
PR 3131 merged CuriousLearner, 2017-08-18 11:38
PR 3155 merged ncoghlan, 2017-08-19 06:16
Messages (12)
msg296518 - (view) Author: Sanyam Khurana (CuriousLearner) * (Python triager) Date: 2017-06-21 05:13
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: https://github.com/python/cpython/pull/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: https://github.com/python/cpython/pull/2009#issuecomment-307539241
msg296525 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2017-06-21 06:43
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.
msg299128 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2017-07-25 18:05
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'"
msg299139 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2017-07-25 21:06
The "op_slot == 96" looks suspicious - how is this value actually supposed to be calculated?
msg299238 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2017-07-26 14:56
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.
msg300484 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-08-18 10:37
New changeset 5e2eb35bbed3e84079165e576cdb50ef36e13493 by Serhiy Storchaka (Sanyam Khurana) in branch 'master':
bpo-30721: Show correct syntax hint in Py3 when using Py2 redirection syntax (#2345)
https://github.com/python/cpython/commit/5e2eb35bbed3e84079165e576cdb50ef36e13493
msg300485 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2017-08-18 11:00
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).
msg300487 - (view) Author: Sanyam Khurana (CuriousLearner) * (Python triager) Date: 2017-08-18 11:08
Ah, sorry for that. I'll just fix that right away in a few mins :)
msg300488 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2017-08-18 11:08
Note that I filed a separate issue to ask Ned about potentially backporting this to 3.6: https://bugs.python.org/issue31232
msg300492 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2017-08-18 12:18
New changeset a7c449b8c08933deabcf329fb74ed1336f6db34f by Nick Coghlan (Sanyam Khurana) in branch 'master':
bpo-30721: Add missing '?' to new error message (GH-3131)
https://github.com/python/cpython/commit/a7c449b8c08933deabcf329fb74ed1336f6db34f
msg300493 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2017-08-18 12:19
And done - thanks folks!
msg300569 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2017-08-19 06:59
New changeset 1a05e87ec75436d818f05a5dabcecaea67334cbd by Nick Coghlan in branch '3.6':
[3.6] bpo-31232: Backport custom print rshift message (GH-3155)
https://github.com/python/cpython/commit/1a05e87ec75436d818f05a5dabcecaea67334cbd
History
Date User Action Args
2022-04-11 14:58:47adminsetgithub: 74906
2017-08-19 06:59:41ncoghlansetmessages: + msg300569
2017-08-19 06:16:59ncoghlansetpull_requests: + pull_request3192
2017-08-18 12:19:05ncoghlansetstatus: open -> closed
resolution: fixed
messages: + msg300493

stage: needs patch -> resolved
2017-08-18 12:18:17ncoghlansetmessages: + msg300492
2017-08-18 11:38:08CuriousLearnersetpull_requests: + pull_request3165
2017-08-18 11:08:35ncoghlansetmessages: + msg300488
2017-08-18 11:08:25CuriousLearnersetmessages: + msg300487
2017-08-18 11:00:15ncoghlansetmessages: + msg300485
stage: patch review -> needs patch
2017-08-18 10:37:39serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg300484
2017-07-26 14:56:47ncoghlansetmessages: + msg299238
2017-07-25 21:06:22steve.dowersetmessages: + msg299139
2017-07-25 18:05:39terry.reedysetnosy: + terry.reedy, paul.moore, tim.golden, zach.ware, steve.dower

messages: + msg299128
stage: patch review
2017-06-23 05:27:16CuriousLearnersetpull_requests: + pull_request2390
2017-06-21 06:43:17ncoghlansetmessages: + msg296525
2017-06-21 05:13:56CuriousLearnercreate