[pypy-commit] pypy ffi-backend: Progress

arigo noreply at buildbot.pypy.org
Wed Jun 20 12:29:15 CEST 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: ffi-backend
Changeset: r55729:f814df5eb27a
Date: 2012-06-19 22:18 +0200
http://bitbucket.org/pypy/pypy/changeset/f814df5eb27a/

Log:	Progress

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
@@ -25,7 +25,16 @@
         return self.space.wrap("<cdata '%s'>" % self.ctype.name)
 
     def int(self):
-        return self.ctype.int(self)
+        w_result = self.ctype.int(self.cdata)
+        keepalive_until_here(self)
+        return w_result
+
+    def long(self):
+        w_result = self.int()
+        space = self.space
+        if space.is_w(space.type(w_result), space.w_int):
+            w_result = space.newlong(space.int_w(w_result))
+        return w_result
 
     def read_raw_signed_data(self):
         result = misc.read_raw_signed_data(self.cdata, self.ctype.size)
@@ -41,6 +50,11 @@
         misc.write_raw_integer_data(self.cdata, source, self.ctype.size)
         keepalive_until_here(self)
 
+    def convert_to_object(self):
+        w_obj = self.ctype.convert_to_object(self.cdata)
+        keepalive_until_here(self)
+        return w_obj
+
 
 class W_CDataOwn(W_CData):
 
@@ -57,6 +71,7 @@
     '_ffi_backend.CData',
     __repr__ = interp2app(W_CData.repr),
     __int__ = interp2app(W_CData.int),
+    __long__ = interp2app(W_CData.long),
     )
 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
@@ -23,11 +23,14 @@
     def cast(self, w_ob):
         raise NotImplementedError
 
-    def int(self, cdataobj):
+    def int(self, cdata):
         space = self.space
         raise operationerrfmt(space.w_TypeError,
                               "int() not supported on cdata '%s'", self.name)
 
+    def convert_to_object(self, cdata):
+        raise NotImplementedError
+
 
 class W_CTypePrimitive(W_CType):
 
@@ -48,26 +51,41 @@
 
 class W_CTypePrimitiveChar(W_CTypePrimitive):
 
-    def int(self, cdataobj):
+    def int(self, cdata):
         xxx
 
 
 class W_CTypePrimitiveSigned(W_CTypePrimitive):
 
-    def int(self, cdataobj):
+    def int(self, cdata):
         if self.value_fits_long:
             # this case is to handle enums, but also serves as a slight
             # performance improvement for some other primitive types
-            value = intmask(cdataobj.read_raw_signed_data())
+            value = intmask(misc.read_raw_signed_data(cdata, self.size))
             return self.space.wrap(value)
         else:
-            return cdataobj.convert_to_object()
+            return self.convert_to_object(cdata)
+
+    def convert_to_object(self, cdata):
+        value = misc.read_raw_signed_data(cdata, self.size)
+        # xxx enum
+        if self.value_fits_long:
+            return self.space.wrap(intmask(value))
+        else:
+            return self.space.wrap(value)    # r_longlong => on 32-bit, 'long'
 
 
 class W_CTypePrimitiveUnsigned(W_CTypePrimitive):
 
-    def int(self, cdataobj):
-        return cdataobj.convert_to_object()
+    def int(self, cdata):
+        return self.convert_to_object(cdata)
+
+    def convert_to_object(self, cdata):
+        value = misc.read_raw_unsigned_data(cdata, self.size)
+        if self.value_fits_long:
+            return self.space.wrap(intmask(value))
+        else:
+            return self.space.wrap(value)    # r_ulonglong => 'long' object
 
 
 W_CType.typedef = TypeDef(
diff --git a/pypy/module/_ffi_backend/newtype.py b/pypy/module/_ffi_backend/newtype.py
--- a/pypy/module/_ffi_backend/newtype.py
+++ b/pypy/module/_ffi_backend/newtype.py
@@ -23,7 +23,14 @@
 eptype("char",        lltype.Char,     ctypeobj.W_CTypePrimitiveChar)
 eptype("signed char", rffi.SIGNEDCHAR, ctypeobj.W_CTypePrimitiveSigned)
 eptype("short",       rffi.SHORT,      ctypeobj.W_CTypePrimitiveSigned)
-# xxx
+eptype("int",         rffi.INT,        ctypeobj.W_CTypePrimitiveSigned)
+eptype("long",        rffi.LONG,       ctypeobj.W_CTypePrimitiveSigned)
+eptype("long long",   rffi.LONGLONG,   ctypeobj.W_CTypePrimitiveSigned)
+eptype("unsigned char",      rffi.UCHAR,    ctypeobj.W_CTypePrimitiveUnsigned)
+eptype("unsigned short",     rffi.SHORT,    ctypeobj.W_CTypePrimitiveUnsigned)
+eptype("unsigned int",       rffi.INT,      ctypeobj.W_CTypePrimitiveUnsigned)
+eptype("unsigned long",      rffi.LONG,     ctypeobj.W_CTypePrimitiveUnsigned)
+eptype("unsigned long long", rffi.LONGLONG, ctypeobj.W_CTypePrimitiveUnsigned)
 
 
 @unwrap_spec(name=str)
diff --git a/pypy/module/_ffi_backend/test/test_c.py b/pypy/module/_ffi_backend/test/test_c.py
--- a/pypy/module/_ffi_backend/test/test_c.py
+++ b/pypy/module/_ffi_backend/test/test_c.py
@@ -68,6 +68,9 @@
         assert self.b.sizeof(p) == 2
 
     def test_integer_types(self):
+        new_primitive_type = self.b.new_primitive_type
+        sizeof = self.b.sizeof
+        cast = self.b.cast
         for name in ['signed char', 'short', 'int', 'long', 'long long']:
             p = new_primitive_type(name)
             size = sizeof(p)


More information about the pypy-commit mailing list