]> granicus.if.org Git - python/commitdiff
SF #754014: list.index() should accept optional start, end arguments
authorRaymond Hettinger <python@rcn.com>
Tue, 17 Jun 2003 05:05:49 +0000 (05:05 +0000)
committerRaymond Hettinger <python@rcn.com>
Tue, 17 Jun 2003 05:05:49 +0000 (05:05 +0000)
Also, modified UserList.index() to match and expanded the related tests.

Lib/UserList.py
Lib/test/test_types.py
Lib/test/test_userlist.py
Misc/NEWS
Objects/listobject.c

index 76cf31fda04babe9e52df9c875072a8069c08bd9..dd1b927ccb94abc1ded47b0a0ba026a57fca5da5 100644 (file)
@@ -75,7 +75,7 @@ class UserList:
     def pop(self, i=-1): return self.data.pop(i)
     def remove(self, item): self.data.remove(item)
     def count(self, item): return self.data.count(item)
-    def index(self, item): return self.data.index(item)
+    def index(self, item, *args): return self.data.index(item, *args)
     def reverse(self): self.data.reverse()
     def sort(self, *args): self.data.sort(*args)
     def extend(self, other):
index 1cb148492cea224c213511113aeb76939c3d87a1..9599456a2212f1bd7407dba5a5300d7e45b73cb0 100644 (file)
@@ -366,7 +366,23 @@ b.insert(200, "right")
 if b != ["left",-2,-1,0,0,"foo",1,2,"right"]: raise TestFailed, 'list insert2'
 if a.count(0) != 2: raise TestFailed, ' list count'
 if a.index(0) != 2: raise TestFailed, 'list index'
+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,3) != 3: raise TestFailed, 'list index, start argument'
+if a.index(0,3,4) != 3: raise TestFailed, 'list index, stop argument'
+try:
+    a.index(2,0,-10)
+except ValueError:
+    pass
+else:
+    raise TestFailed, 'list index, negative stop argument'
 a.remove(0)
+try:
+    a.index(2,0,4)
+except ValueError:
+    pass
+else:
+    raise TestFailed, 'list index, stop argument.'
 if a != [-2,-1,0,1,2]: raise TestFailed, 'list remove'
 a.reverse()
 if a != [2,1,0,-1,-2]: raise TestFailed, 'list reverse'
index 467a47c5f8c4e4133b83ea4ffd87a429542afb47..2d11a2424389a2487835ec075000ea45d749daf0 100644 (file)
@@ -206,6 +206,15 @@ class UserListTest(unittest.TestCase):
         self.assertEqual(u.index(1), 1)
         self.assertRaises(ValueError, u.index, 2)
 
+        u = UserList([-2,-1,0,0,1,2])
+        self.assertEqual(u.count(0), 2)
+        self.assertEqual(u.index(0), 2)
+        self.assertEqual(u.index(0,2), 2)
+        self.assertEqual(u.index(-2,-10), 0)
+        self.assertEqual(u.index(0,3), 3)
+        self.assertEqual(u.index(0,3,4), 3)
+        self.assertRaises(ValueError, u.index, 2,0,-10)
+
     def test_reverse(self):
         u = UserList((0, 1))
         u2 = u[:]
index 0ee20460f539d2e22d465ede6bfcd5d72860c5f9..b8b9a9bf3c90e5496e6209d2a4537435eac34647 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 2.3 beta 2?
 Core and builtins
 -----------------
 
+- list.index() now accepts optional start and stop arguments.  Similar
+  changes were made to UserList.index(). SF feature request 754014.
+
 - SF patch 751998 fixes an unwanted side effect of the previous fix
   for SF bug 742860 (the next item).
 
index 7d03506412211e73ed3dce3e940e6e8e9abc6cfa..3979006e9ebcf696733d6bb2fa7969c599933d08 100644 (file)
@@ -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},