[pypy-svn] r79903 - in pypy/branch/psycopg2compatibility/pypy/module/cpyext: . test

dan at codespeak.net dan at codespeak.net
Wed Dec 8 18:22:36 CET 2010


Author: dan
Date: Wed Dec  8 18:22:34 2010
New Revision: 79903

Modified:
   pypy/branch/psycopg2compatibility/pypy/module/cpyext/sequence.py
   pypy/branch/psycopg2compatibility/pypy/module/cpyext/stubs.py
   pypy/branch/psycopg2compatibility/pypy/module/cpyext/test/test_sequence.py
Log:
Added support for PySequence_SetItem.

Modified: pypy/branch/psycopg2compatibility/pypy/module/cpyext/sequence.py
==============================================================================
--- pypy/branch/psycopg2compatibility/pypy/module/cpyext/sequence.py	(original)
+++ pypy/branch/psycopg2compatibility/pypy/module/cpyext/sequence.py	Wed Dec  8 18:22:34 2010
@@ -6,6 +6,9 @@
 from pypy.rpython.lltypesystem import rffi, lltype
 from pypy.objspace.std import listobject, tupleobject
 
+from pypy.module.cpyext.tupleobject import PyTuple_Check, PyTuple_SetItem
+from pypy.module.cpyext.object import Py_IncRef
+
 @cpython_api([PyObject, Py_ssize_t], PyObject)
 def PySequence_Repeat(space, w_obj, count):
     """Return the result of repeating sequence object o count times, or NULL on
@@ -124,3 +127,24 @@
     """
     return space.iter(w_seq)
 
+ at cpython_api([PyObject, Py_ssize_t, PyObject], rffi.INT_real, error=-1)
+def PySequence_SetItem(space, w_o, i, w_v):
+    """Assign object v to the ith element of o.  Returns -1 on failure.  This
+    is the equivalent of the Python statement o[i] = v.  This function does
+    not steal a reference to v.
+    
+    This function used an int type for i. This might require
+    changes in your code for properly supporting 64-bit systems."""
+
+    Py_IncRef(space, w_v) # XXX: seriously CPython, why should one Py*_SetItem steal but not another!?
+    if PyTuple_Check(space, w_o):
+        return PyTuple_SetItem(space, w_o, i, w_v)
+
+    try:
+        space.setitem(w_o, space.wrap(i), w_v)
+        return 0
+    except OperationError, e:
+        if e.match(space, space.w_IndexError):
+            return -1
+        else:
+            raise

Modified: pypy/branch/psycopg2compatibility/pypy/module/cpyext/stubs.py
==============================================================================
--- pypy/branch/psycopg2compatibility/pypy/module/cpyext/stubs.py	(original)
+++ pypy/branch/psycopg2compatibility/pypy/module/cpyext/stubs.py	Wed Dec  8 18:22:34 2010
@@ -2223,16 +2223,6 @@
     changes in your code for properly supporting 64-bit systems."""
     raise NotImplementedError
 
- at cpython_api([PyObject, Py_ssize_t, PyObject], rffi.INT_real, error=-1)
-def PySequence_SetItem(space, o, i, v):
-    """Assign object v to the ith element of o.  Returns -1 on failure.  This
-    is the equivalent of the Python statement o[i] = v.  This function does
-    not steal a reference to v.
-    
-    This function used an int type for i. This might require
-    changes in your code for properly supporting 64-bit systems."""
-    raise NotImplementedError
-
 @cpython_api([PyObject, Py_ssize_t], rffi.INT_real, error=-1)
 def PySequence_DelItem(space, o, i):
     """Delete the ith element of object o.  Returns -1 on failure.  This is the

Modified: pypy/branch/psycopg2compatibility/pypy/module/cpyext/test/test_sequence.py
==============================================================================
--- pypy/branch/psycopg2compatibility/pypy/module/cpyext/test/test_sequence.py	(original)
+++ pypy/branch/psycopg2compatibility/pypy/module/cpyext/test/test_sequence.py	Wed Dec  8 18:22:34 2010
@@ -70,3 +70,21 @@
         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_setitem(self, space, api):
+        value = api.PyInt_FromLong(42)
+        tup = api.PyTuple_New(1)
+
+        result = api.PySequence_SetItem(tup, 0, value)
+        assert result != -1
+
+        assert space.eq_w(space.getitem(tup, space.wrap(0)), value)
+
+        l = api.PyList_New(1)
+
+        result = api.PySequence_SetItem(l, 0, value)
+        assert result != -1
+
+        assert space.eq_w(space.getitem(l, space.wrap(0)), value)
+        api.Py_DecRef(value)
+



More information about the Pypy-commit mailing list