[Python-checkins] r76058 - python/trunk/Objects/listobject.c

benjamin.peterson python-checkins at python.org
Mon Nov 2 17:14:20 CET 2009


Author: benjamin.peterson
Date: Mon Nov  2 17:14:19 2009
New Revision: 76058

Log:
grant list.index() a more informative error message #7252

Modified:
   python/trunk/Objects/listobject.c

Modified: python/trunk/Objects/listobject.c
==============================================================================
--- python/trunk/Objects/listobject.c	(original)
+++ python/trunk/Objects/listobject.c	Mon Nov  2 17:14:19 2009
@@ -2276,7 +2276,8 @@
 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 @@
 		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;
 }
 


More information about the Python-checkins mailing list