mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +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
|
@ -364,18 +364,22 @@ b.insert(-2, "foo")
|
||||||
b.insert(-200, "left")
|
b.insert(-200, "left")
|
||||||
b.insert(200, "right")
|
b.insert(200, "right")
|
||||||
if b != ["left",-2,-1,0,0,"foo",1,2,"right"]: raise TestFailed, 'list insert2'
|
if b != ["left",-2,-1,0,0,"foo",1,2,"right"]: raise TestFailed, 'list insert2'
|
||||||
|
# a = [-2,-1,0,0,1,2]
|
||||||
if a.count(0) != 2: raise TestFailed, ' list count'
|
if a.count(0) != 2: raise TestFailed, ' list count'
|
||||||
if a.index(0) != 2: raise TestFailed, 'list index'
|
if a.index(0) != 2: raise TestFailed, 'list index'
|
||||||
if a.index(0,2) != 2: raise TestFailed, 'list index, start argument'
|
if a.index(0,2) != 2: raise TestFailed, 'list index, start argument'
|
||||||
if a.index(-2,-10) != 0: raise TestFailed, 'list index, negative start argument'
|
if a.index(0,-4) != 2: raise TestFailed, 'list index, -start argument'
|
||||||
|
if a.index(-2,-10) != 0: raise TestFailed, 'list index, very -start argument'
|
||||||
if a.index(0,3) != 3: raise TestFailed, 'list index, start argument'
|
if a.index(0,3) != 3: raise TestFailed, 'list index, start argument'
|
||||||
|
if a.index(0,-3) != 3: raise TestFailed, 'list index, -start argument'
|
||||||
if a.index(0,3,4) != 3: raise TestFailed, 'list index, stop argument'
|
if a.index(0,3,4) != 3: raise TestFailed, 'list index, stop argument'
|
||||||
|
if a.index(0,-3,-2) != 3: raise TestFailed, 'list index, -stop argument'
|
||||||
try:
|
try:
|
||||||
a.index(2,0,-10)
|
a.index(2,0,-10)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
raise TestFailed, 'list index, negative stop argument'
|
raise TestFailed, 'list index, very -stop argument'
|
||||||
a.remove(0)
|
a.remove(0)
|
||||||
try:
|
try:
|
||||||
a.index(2,0,4)
|
a.index(2,0,4)
|
||||||
|
|
|
@ -1834,8 +1834,18 @@ listindex(PyListObject *self, PyObject *args)
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "O|ii:index", &v, &start, &stop))
|
if (!PyArg_ParseTuple(args, "O|ii:index", &v, &start, &stop))
|
||||||
return NULL;
|
return NULL;
|
||||||
start = max(0, start);
|
if (start < 0) {
|
||||||
stop = max(0, min(self->ob_size, stop));
|
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++) {
|
for (i = start; i < stop; i++) {
|
||||||
int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
|
int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
|
||||||
if (cmp > 0)
|
if (cmp > 0)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue