[pypy-svn] pypy jitypes2: support for signed/unsigned bytes (i.e., C chars but treated as numbers at applevel)

antocuni commits-noreply at bitbucket.org
Wed Jan 12 17:52:07 CET 2011


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: jitypes2
Changeset: r40630:971dfd6678e1
Date: 2011-01-12 17:37 +0100
http://bitbucket.org/pypy/pypy/changeset/971dfd6678e1/

Log:	support for signed/unsigned bytes (i.e., C chars but treated as
	numbers at applevel)

diff --git a/pypy/module/_ffi/test/test__ffi.py b/pypy/module/_ffi/test/test__ffi.py
--- a/pypy/module/_ffi/test/test__ffi.py
+++ b/pypy/module/_ffi/test/test__ffi.py
@@ -187,6 +187,34 @@
         assert sum_xy(32000, 8000) == 40000
         assert sum_xy(60000, 30000) == 90000 % 65536
 
+    def test_unsigned_byte_args(self):
+        """
+            DLLEXPORT unsigned char sum_xy_ub(unsigned char x, unsigned char y)
+            {
+                return x+y;
+            }
+        """
+        from _ffi import CDLL, types
+        libfoo = CDLL(self.libfoo_name)
+        sum_xy = libfoo.getfunc('sum_xy_us', [types.ubyte, types.ubyte],
+                                types.ubyte)
+        assert sum_xy(100, 40) == 140
+        assert sum_xy(200, 60) == 260 % 256
+
+    def test_signed_byte_args(self):
+        """
+            DLLEXPORT signed char sum_xy_sb(signed char x, signed char y)
+            {
+                return x+y;
+            }
+        """
+        from _ffi import CDLL, types
+        libfoo = CDLL(self.libfoo_name)
+        sum_xy = libfoo.getfunc('sum_xy_sb', [types.sbyte, types.sbyte],
+                                types.sbyte)
+        assert sum_xy(10, 20) == 30
+        assert sum_xy(100, 28) == -128
+
     def test_single_float_args(self):
         """
             DLLEXPORT float sum_xy_float(float x, float y)

diff --git a/pypy/module/_ffi/interp_ffi.py b/pypy/module/_ffi/interp_ffi.py
--- a/pypy/module/_ffi/interp_ffi.py
+++ b/pypy/module/_ffi/interp_ffi.py
@@ -32,13 +32,15 @@
         return (shape == 'i' or
                 shape == 'l' or
                 shape == 'h' or
+                shape == 'b' or
                 shape == 'q')
 
     def is_unsigned(self):
         shape = self.shape
-        return (shape == 'I' or
-                shape == 'L' or
+        return (shape == 'L' or
+                shape == 'I' or
                 shape == 'H' or
+                shape == 'B' or
                 shape == 'P' or
                 shape == 'Q')
     
@@ -70,21 +72,23 @@
 def build_ffi_types():
     from pypy.rlib.clibffi import FFI_TYPE_P
     types = [
+        W_FFIType('slong',     'l', libffi.types.slong),
         W_FFIType('sint',      'i', libffi.types.sint),
-        W_FFIType('slong',     'l', libffi.types.slong),
         W_FFIType('sshort',    'h', libffi.types.sshort),
+        W_FFIType('sbyte',     'b', libffi.types.schar),
         W_FFIType('slonglong', 'q', libffi.types.slonglong),
         #
+        W_FFIType('ulong',     'L', libffi.types.ulong),
         W_FFIType('uint',      'I', libffi.types.uint),
-        W_FFIType('ulong',     'L', libffi.types.ulong),
         W_FFIType('ushort',    'H', libffi.types.ushort),
+        W_FFIType('ubyte',     'B', libffi.types.uchar),
         W_FFIType('ulonglong', 'Q', libffi.types.ulonglong),
         #
         W_FFIType('double',    'd', libffi.types.double),
         W_FFIType('float',     'f', libffi.types.float),
         W_FFIType('void',      '0', libffi.types.void),
         W_FFIType('pointer',   'P', libffi.types.pointer),
-        
+        #
         # missing types:
         ## 'c' : ffi_type_uchar,
         ## 'b' : ffi_type_schar,


More information about the Pypy-commit mailing list