diff options
author | Benjamin Peterson <benjamin@python.org> | 2009-11-02 16:14:19 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2009-11-02 16:14:19 (GMT) |
commit | c45a0cfb5fe29cf58fa17a7b3b46ae8fcb5c1c88 (patch) | |
tree | 9dfc0b49663aa80dd772cb78115ae5d139b2a8fe /Objects | |
parent | e2caf1f60ec182558de96526cae12962926ae92a (diff) | |
download | cpython-c45a0cfb5fe29cf58fa17a7b3b46ae8fcb5c1c88.zip cpython-c45a0cfb5fe29cf58fa17a7b3b46ae8fcb5c1c88.tar.gz cpython-c45a0cfb5fe29cf58fa17a7b3b46ae8fcb5c1c88.tar.bz2 |
grant list.index() a more informative error message #7252
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/listobject.c | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/Objects/listobject.c b/Objects/listobject.c index 39b8b1a..dea51ed 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -2276,7 +2276,8 @@ static PyObject * listindex(PyListObject *self, PyObject *args) { Py_ssize_t i, start=0, stop=Py_SIZE(self); - PyObject *v; + PyObject *v, *format_tuple, *err_string; + static PyObject *err_format = NULL; if (!PyArg_ParseTuple(args, "O|O&O&:index", &v, _PyEval_SliceIndex, &start, @@ -2299,7 +2300,20 @@ listindex(PyListObject *self, PyObject *args) else if (cmp < 0) return NULL; } - PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list"); + if (err_format == NULL) { + err_format = PyString_FromString("%r is not in list"); + if (err_format == NULL) + return NULL; + } + format_tuple = PyTuple_Pack(1, v); + if (format_tuple == NULL) + return NULL; + err_string = PyString_Format(err_format, format_tuple); + Py_DECREF(format_tuple); + if (err_string == NULL) + return NULL; + PyErr_SetObject(PyExc_ValueError, err_string); + Py_DECREF(err_string); return NULL; } |