summaryrefslogtreecommitdiffstats
path: root/Objects/abstract.c
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2017-08-19 06:59:38 (GMT)
committerGitHub <noreply@github.com>2017-08-19 06:59:38 (GMT)
commit1a05e87ec75436d818f05a5dabcecaea67334cbd (patch)
tree1ce98f474172605ce4ca1916159a4f99e2a5a824 /Objects/abstract.c
parentb50e7683acac36ff16e6c6c2c32d9a15e46b5174 (diff)
downloadcpython-1a05e87ec75436d818f05a5dabcecaea67334cbd.zip
cpython-1a05e87ec75436d818f05a5dabcecaea67334cbd.tar.gz
cpython-1a05e87ec75436d818f05a5dabcecaea67334cbd.tar.bz2
[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.
Diffstat (limited to 'Objects/abstract.c')
-rw-r--r--Objects/abstract.c15
1 files changed, 15 insertions, 0 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c
index 38de774..6c6c86c 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -809,6 +809,21 @@ binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
PyObject *result = binary_op1(v, w, op_slot);
if (result == Py_NotImplemented) {
Py_DECREF(result);
+
+ if (op_slot == NB_SLOT(nb_rshift) &&
+ PyCFunction_Check(v) &&
+ strcmp(((PyCFunctionObject *)v)->m_ml->ml_name, "print") == 0)
+ {
+ PyErr_Format(PyExc_TypeError,
+ "unsupported operand type(s) for %.100s: "
+ "'%.100s' and '%.100s'. Did you mean \"print(<message>, "
+ "file=<output_stream>)\"?",
+ op_name,
+ v->ob_type->tp_name,
+ w->ob_type->tp_name);
+ return NULL;
+ }
+
return binop_type_error(v, w, op_name);
}
return result;