[pypy-svn] r79613 - in pypy/trunk/pypy/module/cpyext: . test

afa at codespeak.net afa at codespeak.net
Mon Nov 29 00:22:31 CET 2010


Author: afa
Date: Mon Nov 29 00:22:30 2010
New Revision: 79613

Modified:
   pypy/trunk/pypy/module/cpyext/sequence.py
   pypy/trunk/pypy/module/cpyext/stubs.py
   pypy/trunk/pypy/module/cpyext/test/test_sequence.py
Log:
Implement PySequence_Contains


Modified: pypy/trunk/pypy/module/cpyext/sequence.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/sequence.py	(original)
+++ pypy/trunk/pypy/module/cpyext/sequence.py	Mon Nov 29 00:22:30 2010
@@ -116,6 +116,14 @@
     This is the equivalent of the Python expression o1 + o2."""
     return space.add(w_o1, w_o2)
 
+ at cpython_api([PyObject, PyObject], rffi.INT_real, error=-1)
+def PySequence_Contains(space, w_obj, w_value):
+    """Determine if o contains value.  If an item in o is equal to value,
+    return 1, otherwise return 0. On error, return -1.  This is
+    equivalent to the Python expression value in o."""
+    w_res = space.contains(w_obj, w_value)
+    return space.int_w(w_res)
+
 @cpython_api([PyObject], PyObject)
 def PySeqIter_New(space, w_seq):
     """Return an iterator that works with a general sequence object, seq.  The

Modified: pypy/trunk/pypy/module/cpyext/stubs.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/stubs.py	(original)
+++ pypy/trunk/pypy/module/cpyext/stubs.py	Mon Nov 29 00:22:30 2010
@@ -2289,13 +2289,6 @@
     in your code for properly supporting 64-bit systems."""
     raise NotImplementedError
 
- at cpython_api([PyObject, PyObject], rffi.INT_real, error=-1)
-def PySequence_Contains(space, o, value):
-    """Determine if o contains value.  If an item in o is equal to value,
-    return 1, otherwise return 0. On error, return -1.  This is
-    equivalent to the Python expression value in o."""
-    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

Modified: pypy/trunk/pypy/module/cpyext/test/test_sequence.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_sequence.py	(original)
+++ pypy/trunk/pypy/module/cpyext/test/test_sequence.py	Mon Nov 29 00:22:30 2010
@@ -70,3 +70,11 @@
         assert space.unwrap(space.next(w_iter)) == 2
         exc = raises(OperationError, space.next, w_iter)
         assert exc.value.match(space, space.w_StopIteration)
+
+    def test_contains(self, space, api):
+        w_t = space.wrap((1, 'ha'))
+        assert api.PySequence_Contains(w_t, space.wrap(u'ha'))
+        assert not api.PySequence_Contains(w_t, space.wrap(2))
+        assert api.PySequence_Contains(space.w_None, space.wrap(2)) == -1
+        assert api.PyErr_Occurred()
+        api.PyErr_Clear()



More information about the Pypy-commit mailing list