Author: Timo Paulssen <[email protected]>
Branch: 
Changeset: r44127:fde474200aae
Date: 2011-05-11 19:27 +0200
http://bitbucket.org/pypy/pypy/changeset/fde474200aae/

Log:    implement PySequence_Index with test case.

diff --git a/pypy/module/cpyext/sequence.py b/pypy/module/cpyext/sequence.py
--- a/pypy/module/cpyext/sequence.py
+++ b/pypy/module/cpyext/sequence.py
@@ -154,3 +154,26 @@
     equivalent of the Python statement del o[i]."""
     space.delitem(w_o, space.wrap(i))
     return 0
+
+@cpython_api([PyObject, PyObject], Py_ssize_t, error=-1)
+def PySequence_Index(space, w_seq, w_obj):
+    """Return the first index i for which o[i] == value.  On error, return
+    -1.    This is equivalent to the Python expression o.index(value).
+
+    This function returned an int type. This might require changes
+    in your code for properly supporting 64-bit systems."""
+
+    w_iter = space.iter(w_seq)
+    idx = 0
+    while True:
+        try:
+            w_next = space.next(w_iter)
+        except OperationError, e:
+            if e.match(space, space.w_StopIteration):
+                break
+            raise
+        if space.is_true(space.eq(w_next, w_obj)):
+            return idx
+        idx += 1
+
+    return -1
diff --git a/pypy/module/cpyext/stubs.py b/pypy/module/cpyext/stubs.py
--- a/pypy/module/cpyext/stubs.py
+++ b/pypy/module/cpyext/stubs.py
@@ -2021,15 +2021,6 @@
     in your code for properly supporting 64-bit systems."""
     raise NotImplementedError
 
-@cpython_api([PyObject, PyObject], Py_ssize_t, error=-1)
-def PySequence_Index(space, o, value):
-    """Return the first index i for which o[i] == value.  On error, return
-    -1.    This is equivalent to the Python expression o.index(value).
-
-    This function returned an int type. This might require changes
-    in your code for properly supporting 64-bit systems."""
-    raise NotImplementedError
-
 @cpython_api([PyObject], PyObjectP)
 def PySequence_Fast_ITEMS(space, o):
     """Return the underlying array of PyObject pointers.  Assumes that o was 
returned
diff --git a/pypy/module/cpyext/test/test_sequence.py 
b/pypy/module/cpyext/test/test_sequence.py
--- a/pypy/module/cpyext/test/test_sequence.py
+++ b/pypy/module/cpyext/test/test_sequence.py
@@ -105,3 +105,20 @@
 
         self.raises(space, api, IndexError, api.PySequence_DelItem,
                     w_l, 3)
+
+    def test_index(self, space, api):
+        thelist = [9, 8, 7, 6, 5, 4, 3, 2, 1]
+        w_l = space.wrap(thelist)
+        w_tofind = space.wrap(5)
+
+        result = api.PySequence_Index(w_l, w_tofind)
+        assert result == thelist.index(5)
+
+        w_tofind = space.wrap(9001)
+        result = api.PySequence_Index(w_l, w_tofind)
+        assert result == -1
+
+        gen = (x ** 2 for x in range(40))
+        w_tofind = space.wrap(16)
+        result = api.PySequence_Index(space.wrap(gen), w_tofind)
+        assert result == 4
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to