summaryrefslogtreecommitdiffstats
path: root/Objects/abstract.c
diff options
context:
space:
mode:
Diffstat (limited to 'Objects/abstract.c')
-rw-r--r--Objects/abstract.c82
1 files changed, 43 insertions, 39 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c
index 91ba8c2..de88457 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -9,9 +9,9 @@
/* Shorthands to return certain errors */
static PyObject *
-type_error(const char *msg)
+type_error(const char *msg, PyObject *obj)
{
- PyErr_SetString(PyExc_TypeError, msg);
+ PyErr_Format(PyExc_TypeError, msg, obj->ob_type->tp_name);
return NULL;
}
@@ -125,10 +125,11 @@ PyObject_GetItem(PyObject *o, PyObject *key)
return PySequence_GetItem(o, key_value);
}
else if (o->ob_type->tp_as_sequence->sq_item)
- return type_error("sequence index must be integer");
+ return type_error("sequence index must "
+ "be integer, not '%.200s'", key);
}
- return type_error("unsubscriptable object");
+ return type_error("'%.200s' object is unsubscriptable", o);
}
int
@@ -153,12 +154,13 @@ PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value)
return PySequence_SetItem(o, key_value, value);
}
else if (o->ob_type->tp_as_sequence->sq_ass_item) {
- type_error("sequence index must be integer");
+ type_error("sequence index must be "
+ "integer, not '%.200s'", key);
return -1;
}
}
- type_error("object does not support item assignment");
+ type_error("'%.200s' object does not support item assignment", o);
return -1;
}
@@ -184,12 +186,13 @@ PyObject_DelItem(PyObject *o, PyObject *key)
return PySequence_DelItem(o, key_value);
}
else if (o->ob_type->tp_as_sequence->sq_ass_item) {
- type_error("sequence index must be integer");
+ type_error("sequence index must be "
+ "integer, not '%.200s'", key);
return -1;
}
}
- type_error("object does not support item deletion");
+ type_error("'%.200s' object does not support item deletion", o);
return -1;
}
@@ -408,7 +411,8 @@ static PyObject *
binop_type_error(PyObject *v, PyObject *w, const char *op_name)
{
PyErr_Format(PyExc_TypeError,
- "unsupported operand type(s) for %s: '%s' and '%s'",
+ "unsupported operand type(s) for %.100s: "
+ "'%.100s' and '%.100s'",
op_name,
v->ob_type->tp_name,
w->ob_type->tp_name);
@@ -514,14 +518,14 @@ ternary_op(PyObject *v,
PyErr_Format(
PyExc_TypeError,
"unsupported operand type(s) for ** or pow(): "
- "'%s' and '%s'",
+ "'%.100s' and '%.100s'",
v->ob_type->tp_name,
w->ob_type->tp_name);
else
PyErr_Format(
PyExc_TypeError,
"unsupported operand type(s) for pow(): "
- "'%s', '%s', '%s'",
+ "'%.100s', '%.100s', '%.100s'",
v->ob_type->tp_name,
w->ob_type->tp_name,
z->ob_type->tp_name);
@@ -568,8 +572,8 @@ sequence_repeat(ssizeargfunc repeatfunc, PyObject *seq, PyObject *n)
return NULL;
}
else {
- return type_error(
- "can't multiply sequence by non-int");
+ return type_error("can't multiply sequence by "
+ "non-int of type '%.200s'", n);
}
return (*repeatfunc)(seq, count);
}
@@ -776,7 +780,7 @@ PyNumber_Negative(PyObject *o)
if (m && m->nb_negative)
return (*m->nb_negative)(o);
- return type_error("bad operand type for unary -");
+ return type_error("bad operand type for unary -: '%.200s'", o);
}
PyObject *
@@ -790,7 +794,7 @@ PyNumber_Positive(PyObject *o)
if (m && m->nb_positive)
return (*m->nb_positive)(o);
- return type_error("bad operand type for unary +");
+ return type_error("bad operand type for unary +: '%.200s'", o);
}
PyObject *
@@ -804,7 +808,7 @@ PyNumber_Invert(PyObject *o)
if (m && m->nb_invert)
return (*m->nb_invert)(o);
- return type_error("bad operand type for unary ~");
+ return type_error("bad operand type for unary ~: '%.200s'", o);
}
PyObject *
@@ -818,7 +822,7 @@ PyNumber_Absolute(PyObject *o)
if (m && m->nb_absolute)
return m->nb_absolute(o);
- return type_error("bad operand type for abs()");
+ return type_error("bad operand type for abs(): '%.200s'", o);
}
/* Add a check for embedded NULL-bytes in the argument. */
@@ -898,7 +902,8 @@ PyNumber_Int(PyObject *o)
if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
return int_from_string((char*)buffer, buffer_len);
- return type_error("int() argument must be a string or a number");
+ return type_error("int() argument must be a string or a "
+ "number, not '%.200s'", o);
}
/* Add a check for embedded NULL-bytes in the argument. */
@@ -960,7 +965,8 @@ PyNumber_Long(PyObject *o)
if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
return long_from_string(buffer, buffer_len);
- return type_error("long() argument must be a string or a number");
+ return type_error("long() argument must be a string or a "
+ "number, not '%.200s'", o);
}
PyObject *
@@ -1014,7 +1020,7 @@ PySequence_Size(PyObject *s)
if (m && m->sq_length)
return m->sq_length(s);
- type_error("len() of unsized object");
+ type_error("object of type '%.200s' has no len()", s);
return -1;
}
@@ -1047,7 +1053,7 @@ PySequence_Concat(PyObject *s, PyObject *o)
return result;
Py_DECREF(result);
}
- return type_error("object can't be concatenated");
+ return type_error("'%.200s' object can't be concatenated", s);
}
PyObject *
@@ -1076,7 +1082,7 @@ PySequence_Repeat(PyObject *o, Py_ssize_t count)
return result;
Py_DECREF(result);
}
- return type_error("object can't be repeated");
+ return type_error("'%.200s' object can't be repeated", o);
}
PyObject *
@@ -1100,7 +1106,7 @@ PySequence_InPlaceConcat(PyObject *s, PyObject *o)
return result;
Py_DECREF(result);
}
- return type_error("object can't be concatenated");
+ return type_error("'%.200s' object can't be concatenated", s);
}
PyObject *
@@ -1129,7 +1135,7 @@ PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
return result;
Py_DECREF(result);
}
- return type_error("object can't be repeated");
+ return type_error("'%.200s' object can't be repeated", o);
}
PyObject *
@@ -1153,7 +1159,7 @@ PySequence_GetItem(PyObject *s, Py_ssize_t i)
return m->sq_item(s, i);
}
- return type_error("unindexable object");
+ return type_error("'%.200s' object is unindexable", s);
}
PyObject *
@@ -1188,7 +1194,7 @@ PySequence_GetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
return res;
}
- return type_error("unsliceable object");
+ return type_error("'%.200s' object is unsliceable", s);
}
int
@@ -1214,7 +1220,7 @@ PySequence_SetItem(PyObject *s, Py_ssize_t i, PyObject *o)
return m->sq_ass_item(s, i, o);
}
- type_error("object does not support item assignment");
+ type_error("'%.200s' object does not support item assignment", s);
return -1;
}
@@ -1241,7 +1247,7 @@ PySequence_DelItem(PyObject *s, Py_ssize_t i)
return m->sq_ass_item(s, i, (PyObject *)NULL);
}
- type_error("object doesn't support item deletion");
+ type_error("'%.200s' object doesn't support item deletion", s);
return -1;
}
@@ -1280,7 +1286,7 @@ PySequence_SetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2, PyObject *o)
return res;
}
- type_error("object doesn't support slice assignment");
+ type_error("'%.200s' object doesn't support slice assignment", s);
return -1;
}
@@ -1309,7 +1315,7 @@ PySequence_DelSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
}
return m->sq_ass_slice(s, i1, i2, (PyObject *)NULL);
}
- type_error("object doesn't support slice deletion");
+ type_error("'%.200s' object doesn't support slice deletion", s);
return -1;
}
@@ -1440,7 +1446,7 @@ PySequence_Fast(PyObject *v, const char *m)
it = PyObject_GetIter(v);
if (it == NULL) {
if (PyErr_ExceptionMatches(PyExc_TypeError))
- return type_error(m);
+ PyErr_SetString(PyExc_TypeError, m);
return NULL;
}
@@ -1470,7 +1476,7 @@ _PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
it = PyObject_GetIter(seq);
if (it == NULL) {
- type_error("iterable argument required");
+ type_error("argument of type '%.200s' is not iterable", seq);
return -1;
}
@@ -1603,7 +1609,7 @@ PyMapping_Size(PyObject *o)
if (m && m->mp_length)
return m->mp_length(o);
- type_error("len() of unsized object");
+ type_error("object of type '%.200s' has no len()", o);
return -1;
}
@@ -1701,7 +1707,7 @@ PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw)
"NULL result without error in PyObject_Call");
return result;
}
- PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
+ PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
func->ob_type->tp_name);
return NULL;
}
@@ -1790,7 +1796,7 @@ PyObject_CallMethod(PyObject *o, char *name, char *format, ...)
}
if (!PyCallable_Check(func)) {
- type_error("call of non-callable attribute");
+ type_error("attribute of type '%.200s' is not callable", func);
goto exit;
}
@@ -1829,7 +1835,7 @@ _PyObject_CallMethod_SizeT(PyObject *o, char *name, char *format, ...)
}
if (!PyCallable_Check(func)) {
- type_error("call of non-callable attribute");
+ type_error("attribute of type '%.200s' is not callable", func);
goto exit;
}
@@ -2170,9 +2176,7 @@ PyObject_GetIter(PyObject *o)
if (f == NULL) {
if (PySequence_Check(o))
return PySeqIter_New(o);
- PyErr_SetString(PyExc_TypeError,
- "iteration over non-sequence");
- return NULL;
+ return type_error("'%.200s' object is not iterable", o);
}
else {
PyObject *res = (*f)(o);