Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5e2eb35

Browse files
CuriousLearnerserhiy-storchaka
authored andcommitted
bpo-30721: Show correct syntax hint in Py3 when using Py2 redirection syntax (#2345)
1 parent ad7eaed commit 5e2eb35

3 files changed

Lines changed: 50 additions & 0 deletions

File tree

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
@@ -819,6 +819,21 @@ binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
819819
PyObject *result = binary_op1(v, w, op_slot);
820820
if (result == Py_NotImplemented) {
821821
Py_DECREF(result);
822+
823+
if (op_slot == NB_SLOT(nb_rshift) &&
824+
PyCFunction_Check(v) &&
825+
strcmp(((PyCFunctionObject *)v)->m_ml->ml_name, "print") == 0)
826+
{
827+
PyErr_Format(PyExc_TypeError,
828+
"unsupported operand type(s) for %.100s: "
829+
"'%.100s' and '%.100s'. Did you mean \"print(<message>, "
830+
"file=<output_stream>)\"",
831+
op_name,
832+
v->ob_type->tp_name,
833+
w->ob_type->tp_name);
834+
return NULL;
835+
}
836+
822837
return binop_type_error(v, w, op_name);
823838
}
824839
return result;

0 commit comments

Comments
 (0)