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: Backport the new custom "print >> sys.stderr" error message?
Type: enhancement Stage: resolved
Components: Versions: Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: barry, eric.smith, ncoghlan, ned.deily, petr.viktorin, rhettinger, serhiy.storchaka
Priority: normal Keywords:

Created on 2017-08-18 11:07 by ncoghlan, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 3155 merged ncoghlan, 2017-08-19 06:16
Messages (9)
msg300486 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2017-08-18 11:07
https://bugs.python.org/issue30721 introduces a new custom error message, such that in 3.7+ "print >> sys.stderr" will report:

```
>>> 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>)"?
```

This is quite clearly an enhancement to the error reporting rather than a bug fix, but similar to the syntax errors for print statements without parentheses, it's an enhancement that only touches an error handling code path (specifically, the one where >> is already in the process of raising TypeError after operand coercion failed), and relates to an issue that a lot of ad hoc Python scripts are likely to encounter.

As such, before I propose it as a downstream patch for Fedora's Python 3.6 stack, I figured I'd propose it as an upstream maintenance backport first.
msg300494 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2017-08-18 12:59
I'm in favor of backporting to 3.6. It's not an intrusive change and is helpful in porting 2.x scripts.
msg300498 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2017-08-18 14:21
I am OK with adding it to 3.6.x.
msg300521 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2017-08-18 17:28
It's probably fine, since it should be a rare occurrence, but it of course has the potential to break things like tests (doc and otherwise).  Unlikely, but it should be pointed out.  Still, I'm also fine with backporting it.
msg300523 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-08-18 18:34
But this backport would break my code:

    >>> print = 10
    >>> print >> 1
    5

Just kidding ;-)

+1 for the backport.  It will likely save some headaches.
msg300525 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-08-18 18:43
It will not break this code. The only visible effect is changing error messages of some TypeError exceptions.
msg300566 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2017-08-19 06:04
The condition we (mostly Serhiy) came up with for the check is actually kinda neat, since it's based on the value of the LHS and is hard to trigger accidentally:

```
>>> printf = print
>>> print = 10
>>> print >> 1
5
>>> printf >> 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for >>: 'builtin_function_or_method' and 'int'. Did you mean "print(<message>, file=<output_stream>)"
```

Anyway, I'll put together a PR that combines both the original patch and the follow up fix to add the missing question mark.
msg300568 - (view) Author: Nick 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
msg300570 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2017-08-19 07:00
Thanks all!
History
Date User Action Args
2022-04-11 14:58:50adminsetgithub: 75415
2017-08-19 07:00:45ncoghlansetstatus: open -> closed
resolution: fixed
messages: + msg300570

stage: resolved
2017-08-19 06:59:41ncoghlansetmessages: + msg300568
2017-08-19 06:16:59ncoghlansetpull_requests: + pull_request3191
2017-08-19 06:04:01ncoghlansetmessages: + msg300566
2017-08-18 18:43:46serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg300525
2017-08-18 18:34:30rhettingersetnosy: + rhettinger
messages: + msg300523
2017-08-18 17:28:56barrysetnosy: + barry
messages: + msg300521
2017-08-18 14:21:26ned.deilysetmessages: + msg300498
2017-08-18 12:59:43eric.smithsetnosy: + eric.smith
messages: + msg300494
2017-08-18 11:07:17ncoghlancreate