Skip to content

Commit 1a05e87

Browse files
authored
[3.6] bpo-31232: Backport custom print rshift message (GH-3155)
bpo-30721 added a "Did you mean ...?" suggestion to rshift TypeError messages that triggers when the LHS is a Python C function called "print". Since this custom error message is expected to be triggered primarily by attempts to use Python 2 print redirection syntax in Python 3, and is incredibly unlikely to be encountered otherwise, it is also being backported to the next 3.6 maintenance release. Initial patch by Sanyam Khurana.
1 parent b50e768 commit 1a05e87

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

‎Lib/test/test_print.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
import sys
23
from io import StringIO
34

45
from test import support
@@ -155,6 +156,38 @@ def test_string_with_excessive_whitespace(self):
155156

156157
self.assertIn('print("Hello World", end=" ")', str(context.exception))
157158

159+
def test_stream_redirection_hint_for_py2_migration(self):
160+
# Test correct hint produced for Py2 redirection syntax
161+
with self.assertRaises(TypeError) as context:
162+
print >> sys.stderr, "message"
163+
self.assertIn('Did you mean "print(<message>, '
164+
'file=<output_stream>)"?', str(context.exception))
165+
166+
# Test correct hint is produced in the case where RHS implements
167+
# __rrshift__ but returns NotImplemented
168+
with self.assertRaises(TypeError) as context:
169+
print >> 42
170+
self.assertIn('Did you mean "print(<message>, '
171+
'file=<output_stream>)"?', str(context.exception))
172+
173+
# Test stream redirection hint is specific to print
174+
with self.assertRaises(TypeError) as context:
175+
max >> sys.stderr
176+
self.assertNotIn('Did you mean ', str(context.exception))
177+
178+
# Test stream redirection hint is specific to rshift
179+
with self.assertRaises(TypeError) as context:
180+
print << sys.stderr
181+
self.assertNotIn('Did you mean', str(context.exception))
182+
183+
# Ensure right operand implementing rrshift still works
184+
class OverrideRRShift:
185+
def __rrshift__(self, lhs):
186+
return 42 # Force result independent of LHS
187+
188+
self.assertEqual(print >> OverrideRRShift(), 42)
189+
190+
158191

159192
if __name__ == "__main__":
160193
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``print`` now shows correct usage hint for using Python 2 redirection
2+
syntax. Patch by Sanyam Khurana.

‎Objects/abstract.c‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,21 @@ binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
809809
PyObject *result = binary_op1(v, w, op_slot);
810810
if (result == Py_NotImplemented) {
811811
Py_DECREF(result);
812+
813+
if (op_slot == NB_SLOT(nb_rshift) &&
814+
PyCFunction_Check(v) &&
815+
strcmp(((PyCFunctionObject *)v)->m_ml->ml_name, "print") == 0)
816+
{
817+
PyErr_Format(PyExc_TypeError,
818+
"unsupported operand type(s) for %.100s: "
819+
"'%.100s' and '%.100s'. Did you mean \"print(<message>, "
820+
"file=<output_stream>)\"?",
821+
op_name,
822+
v->ob_type->tp_name,
823+
w->ob_type->tp_name);
824+
return NULL;
825+
}
826+
812827
return binop_type_error(v, w, op_name);
813828
}
814829
return result;

0 commit comments

Comments
 (0)