Fix sloppy index() implementation:

- don't use min() and max()
- interpret negative start/stop argument like negative slice indices
This commit is contained in:
Guido van Rossum 2003-06-17 14:25:14 +00:00
parent 77cdeaff55
commit 2743d87d79
2 changed files with 18 additions and 4 deletions

View file

@ -1834,8 +1834,18 @@ listindex(PyListObject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "O|ii:index", &v, &start, &stop))
return NULL;
start = max(0, start);
stop = max(0, min(self->ob_size, stop));
if (start < 0) {
start += self->ob_size;
if (start < 0)
start = 0;
}
if (stop < 0) {
stop += self->ob_size;
if (stop < 0)
stop = 0;
}
else if (stop > self->ob_size)
stop = self->ob_size;
for (i = start; i < stop; i++) {
int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
if (cmp > 0)