Commits

Armin Rigo committed f814df5

Progress

Comments (0)

Files changed (4)

pypy/module/_ffi_backend/cdataobj.py

         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)
         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):
 
     '_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
 

pypy/module/_ffi_backend/ctypeobj.py

     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):
 
 
 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(

pypy/module/_ffi_backend/newtype.py

 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)

pypy/module/_ffi_backend/test/test_c.py

         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)