Commit 0452fb8d authored by Guido van Rossum's avatar Guido van Rossum

Make the error message for unsupported operand types cleaner, in

response to a message by Laura Creighton on c.l.py.  E.g.

    >>> 0+''
    TypeError: unsupported operand types for +: 'int' and 'str'

(previously this did not mention the operand types)

    >>> ''+0
    TypeError: cannot concatenate 'str' and 'int' objects
parent c8fe43c7
...@@ -383,8 +383,12 @@ binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name) ...@@ -383,8 +383,12 @@ binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
PyObject *result = binary_op1(v, w, op_slot); PyObject *result = binary_op1(v, w, op_slot);
if (result == Py_NotImplemented) { if (result == Py_NotImplemented) {
Py_DECREF(Py_NotImplemented); Py_DECREF(Py_NotImplemented);
PyErr_Format(PyExc_TypeError, PyErr_Format(
"unsupported operand type(s) for %s", op_name); PyExc_TypeError,
"unsupported operand type(s) for %s: '%s' and '%s'",
op_name,
v->ob_type->tp_name,
w->ob_type->tp_name);
return NULL; return NULL;
} }
return result; return result;
...@@ -533,9 +537,22 @@ ternary_op(PyObject *v, ...@@ -533,9 +537,22 @@ ternary_op(PyObject *v,
if (c >= 0) if (c >= 0)
return x; return x;
} }
PyErr_Format(PyExc_TypeError, "unsupported operand type(s) for %s", if (z == Py_None)
op_name); PyErr_Format(
PyExc_TypeError,
"unsupported operand type(s) for ** or pow(): "
"'%s' and '%s'",
v->ob_type->tp_name,
w->ob_type->tp_name);
else
PyErr_Format(
PyExc_TypeError,
"unsupported operand type(s) for pow(): "
"'%s', '%s', '%s'",
v->ob_type->tp_name,
w->ob_type->tp_name,
z->ob_type->tp_name);
return NULL; return NULL;
} }
...@@ -566,8 +583,11 @@ PyNumber_Add(PyObject *v, PyObject *w) ...@@ -566,8 +583,11 @@ PyNumber_Add(PyObject *v, PyObject *w)
result = (*m->sq_concat)(v, w); result = (*m->sq_concat)(v, w);
} }
else { else {
PyErr_SetString(PyExc_TypeError, PyErr_Format(
"unsupported operand types for +"); PyExc_TypeError,
"unsupported operand types for +: '%s' and '%s'",
v->ob_type->tp_name,
w->ob_type->tp_name);
result = NULL; result = NULL;
} }
} }
......
...@@ -691,7 +691,7 @@ string_concat(register PyStringObject *a, register PyObject *bb) ...@@ -691,7 +691,7 @@ string_concat(register PyStringObject *a, register PyObject *bb)
return PyUnicode_Concat((PyObject *)a, bb); return PyUnicode_Concat((PyObject *)a, bb);
#endif #endif
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"cannot add type \"%.200s\" to string", "cannot concatenate 'str' and '%.200s' objects",
bb->ob_type->tp_name); bb->ob_type->tp_name);
return NULL; return NULL;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment