[pypy-commit] pypy ffi-backend: Next test

arigo noreply at buildbot.pypy.org
Sat Jun 23 10:51:00 CEST 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: ffi-backend
Changeset: r55765:0c5df1e17ace
Date: 2012-06-23 10:50 +0200
http://bitbucket.org/pypy/pypy/changeset/0c5df1e17ace/

Log:	Next test

diff --git a/pypy/module/_ffi_backend/cdataobj.py b/pypy/module/_ffi_backend/cdataobj.py
--- a/pypy/module/_ffi_backend/cdataobj.py
+++ b/pypy/module/_ffi_backend/cdataobj.py
@@ -49,6 +49,15 @@
         keepalive_until_here(self)
         return w_result
 
+    def len(self):
+        from pypy.module._ffi_backend import ctypeobj
+        space = self.space
+        if isinstance(self.ctype, ctypeobj.W_CTypeArray):
+            return space.wrap(self.get_array_length())
+        raise operationerrfmt(space.w_TypeError,
+                              "cdata of type '%s' has no len()",
+                              self.ctype.name)
+
     def str(self):
         w_result = self.ctype.try_str(self._cdata)
         keepalive_until_here(self)
@@ -76,7 +85,6 @@
         return self.space.wrap(h)
 
     def getitem(self, w_index):
-        from pypy.module._ffi_backend import ctypeobj
         space = self.space
         i = space.getindex_w(w_index, space.w_IndexError)
         self.ctype._check_subscript_index(self, i)
@@ -86,6 +94,16 @@
         keepalive_until_here(self)
         return w_o
 
+    def setitem(self, w_index, w_value):
+        space = self.space
+        i = space.getindex_w(w_index, space.w_IndexError)
+        self.ctype._check_subscript_index(self, i)
+        ctitem = self.ctype.ctitem
+        ctitem.convert_from_object(
+            rffi.ptradd(self._cdata, i * ctitem.size),
+            w_value)
+        keepalive_until_here(self)
+
     def read_raw_signed_data(self):
         result = misc.read_raw_signed_data(self._cdata, self.ctype.size)
         keepalive_until_here(self)
@@ -114,6 +132,14 @@
         keepalive_until_here(self)
         return w_obj
 
+    def get_array_length(self):
+        from pypy.module._ffi_backend import ctypeobj
+        ctype = self.ctype
+        assert isinstance(ctype, ctypeobj.W_CTypeArray)
+        length = ctype.length
+        assert length >= 0
+        return length
+
 
 class W_CDataOwnFromCasted(W_CData):
 
@@ -132,6 +158,15 @@
         return ' owning %d bytes' % (self.ctype.size,)
 
 
+class W_CDataOwnLength(W_CDataOwn):
+
+    def __init__(self, space, size, ctype, length):
+        W_CDataOwn.__init__(self, space, size, ctype)
+        self.length = length
+
+    def get_array_length(self):
+        return self.length
+
 
 W_CData.typedef = TypeDef(
     '_ffi_backend.CData',
@@ -140,10 +175,12 @@
     __int__ = interp2app(W_CData.int),
     __long__ = interp2app(W_CData.long),
     __float__ = interp2app(W_CData.float),
+    __len__ = interp2app(W_CData.len),
     __str__ = interp2app(W_CData.str),
     __eq__ = interp2app(W_CData.eq),
     __ne__ = interp2app(W_CData.ne),
     __hash__ = interp2app(W_CData.hash),
     __getitem__ = interp2app(W_CData.getitem),
+    __setitem__ = interp2app(W_CData.setitem),
     )
 W_CData.acceptable_as_base_class = False
diff --git a/pypy/module/_ffi_backend/ctypeobj.py b/pypy/module/_ffi_backend/ctypeobj.py
--- a/pypy/module/_ffi_backend/ctypeobj.py
+++ b/pypy/module/_ffi_backend/ctypeobj.py
@@ -50,7 +50,9 @@
 
     def _check_subscript_index(self, w_cdata, i):
         space = self.space
-        raise OperationError(xxx)
+        raise operationerrfmt(space.w_TypeError,
+                              "cdata of type '%s' cannot be indexed",
+                              self.name)
 
     def try_str(self, cdata):
         return None
@@ -118,8 +120,32 @@
     def __init__(self, space, ctptr, length, arraysize, extra):
         W_CTypePtrOrArray.__init__(self, space, arraysize, extra, 0,
                                    ctptr.ctitem)
+        self.length = length
         self.ctptr = ctptr
 
+    def newp(self, w_init):
+        space = self.space
+        datasize = self.size
+        if datasize < 0:
+            xxx
+            cdataobj.W_CDataOwnLength(space, )
+            xxx
+        cdata = cdataobj.W_CDataOwn(space, datasize, self)
+        if not space.is_w(w_init, space.w_None):
+            self.convert_from_object(cdata._cdata, w_init)
+            keepalive_until_here(cdata)
+        return cdata
+
+    def _check_subscript_index(self, w_cdata, i):
+        space = self.space
+        if i < 0:
+            raise OperationError(space.w_IndexError,
+                                 space.wrap("negative index not supported"))
+        if i >= w_cdata.get_array_length():
+            raise operationerrfmt(space.w_IndexError,
+                "index too large for cdata '%s' (expected %d < %d)",
+                self.name, i, w_cdata.get_array_length())
+
 
 class W_CTypePrimitive(W_CType):
 


More information about the pypy-commit mailing list