[pypy-svn] pypy default: Importing PySequence changes from psycopg2compatibility branch.

ademan commits-noreply at bitbucket.org
Fri Jan 28 00:47:04 CET 2011


Author: Daniel Roberts <Ademan555 at gmail.com>
Branch: 
Changeset: r41414:8b1b43cbb854
Date: 2011-01-27 15:45 -0800
http://bitbucket.org/pypy/pypy/changeset/8b1b43cbb854/

Log:	Importing PySequence changes from psycopg2compatibility branch.

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
@@ -1,11 +1,16 @@
 
-from pypy.interpreter.error import OperationError
+from pypy.interpreter.error import OperationError, operationerrfmt
 from pypy.module.cpyext.api import (
     cpython_api, CANNOT_FAIL, CONST_STRING, Py_ssize_t)
 from pypy.module.cpyext.pyobject import PyObject, borrow_from
 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, Py_DecRef
+
+from pypy.module.cpyext.dictobject import PyDict_Check
+
 @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
@@ -132,3 +137,28 @@
     """
     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."""
+    if PyDict_Check(space, w_o) or not PySequence_Check(space, w_o):
+        raise operationerrfmt(space.w_TypeError, "'%s' object does not support item assignment",
+                             space.str_w(space.repr(space.type(w_o)))) # FIXME: looks like lisp...
+    space.setitem(w_o, space.wrap(i), w_v)
+    return 0
+
+ at cpython_api([PyObject, Py_ssize_t], rffi.INT_real, error=-1)
+def PySequence_DelItem(space, w_o, i):
+    """Delete the ith element of object o.  Returns -1 on failure.  This is the
+    equivalent of the Python statement del o[i].
+    
+    This function used an int type for i. This might require
+    changes in your code for properly supporting 64-bit systems."""
+
+    # FIXME: May be too lenient
+    space.delitem(w_o, space.wrap(i))
+    return 0

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
@@ -78,3 +78,31 @@
         assert api.PySequence_Contains(space.w_None, space.wrap(2)) == -1
         assert api.PyErr_Occurred()
         api.PyErr_Clear()
+
+    def test_setitem(self, space, api):
+        w_value = space.wrap(42)
+
+        l = api.PyList_New(1)
+        result = api.PySequence_SetItem(l, 0, w_value)
+        assert result != -1
+        assert space.eq_w(space.getitem(l, space.wrap(0)), w_value)
+
+        self.raises(space, api, IndexError, api.PySequence_SetItem,
+                    l, 3, w_value)
+
+        self.raises(space, api, TypeError, api.PySequence_SetItem,
+                    api.PyTuple_New(1), 0, w_value)
+
+        self.raises(space, api, TypeError, api.PySequence_SetItem,
+                    space.newdict(), 0, w_value)
+
+    def test_delitem(self, space, api):
+        w_l = space.wrap([1, 2, 3, 4])
+
+        result = api.PySequence_DelItem(w_l, 2)
+        assert result == 0
+        assert space.eq_w(w_l, space.wrap([1, 2, 4]))
+
+        self.raises(space, api, IndexError, api.PySequence_DelItem,
+                    w_l, 3)
+


More information about the Pypy-commit mailing list