diff options
author | Raymond Hettinger <python@rcn.com> | 2003-06-17 05:05:49 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2003-06-17 05:05:49 (GMT) |
commit | d05abdec7b2620449369bb44a617684463ba50ac (patch) | |
tree | cb20408dd9bf2d158586d3187e22438b4427288e /Objects | |
parent | c8106e1f1dbd00e64aff6ecc5ecb53010168dd27 (diff) | |
download | cpython-d05abdec7b2620449369bb44a617684463ba50ac.zip cpython-d05abdec7b2620449369bb44a617684463ba50ac.tar.gz cpython-d05abdec7b2620449369bb44a617684463ba50ac.tar.bz2 |
SF #754014: list.index() should accept optional start, end arguments
Also, modified UserList.index() to match and expanded the related tests.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/listobject.c | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/Objects/listobject.c b/Objects/listobject.c index 7d03506..3979006 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -1827,11 +1827,16 @@ PyList_AsTuple(PyObject *v) } static PyObject * -listindex(PyListObject *self, PyObject *v) +listindex(PyListObject *self, PyObject *args) { - int i; + int i, start=0, stop=self->ob_size; + PyObject *v; - for (i = 0; i < self->ob_size; i++) { + if (!PyArg_ParseTuple(args, "O|ii:index", &v, &start, &stop)) + return NULL; + start = max(0, start); + stop = max(0, min(self->ob_size, stop)); + for (i = start; i < stop; i++) { int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ); if (cmp > 0) return PyInt_FromLong((long)i); @@ -2088,7 +2093,7 @@ PyDoc_STRVAR(pop_doc, PyDoc_STRVAR(remove_doc, "L.remove(value) -- remove first occurrence of value"); PyDoc_STRVAR(index_doc, -"L.index(value) -> integer -- return index of first occurrence of value"); +"L.index(value, [start, [stop]]) -> integer -- return first index of value"); PyDoc_STRVAR(count_doc, "L.count(value) -> integer -- return number of occurrences of value"); PyDoc_STRVAR(reverse_doc, @@ -2102,7 +2107,7 @@ static PyMethodDef list_methods[] = { {"extend", (PyCFunction)listextend, METH_O, extend_doc}, {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc}, {"remove", (PyCFunction)listremove, METH_O, remove_doc}, - {"index", (PyCFunction)listindex, METH_O, index_doc}, + {"index", (PyCFunction)listindex, METH_VARARGS, index_doc}, {"count", (PyCFunction)listcount, METH_O, count_doc}, {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc}, {"sort", (PyCFunction)listsort, METH_VARARGS, sort_doc}, |