mirror of
https://github.com/python/cpython.git
synced 2025-08-02 16:13:13 +00:00
Fix sloppy index() implementation:
- don't use min() and max() - interpret negative start/stop argument like negative slice indices
This commit is contained in:
parent
77cdeaff55
commit
2743d87d79
2 changed files with 18 additions and 4 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue