[pypy-svn] pypy jitypes2: support for unichars

antocuni commits-noreply at bitbucket.org
Thu Jan 13 13:27:10 CET 2011


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: jitypes2
Changeset: r40640:f9111f4a5e92
Date: 2011-01-13 10:56 +0100
http://bitbucket.org/pypy/pypy/changeset/f9111f4a5e92/

Log:	support for unichars

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
@@ -228,6 +228,22 @@
                                     types.char)
         assert my_toupper('c') == 'C'
 
+    def test_unichar_args(self):
+        """
+            #include <stddef.h>
+            DLLEXPORT wchar_t sum_xy_wc(wchar_t x, wchar_t y)
+            {
+                return x + y;
+            }
+        """
+        from _ffi import CDLL, types
+        libfoo = CDLL(self.libfoo_name)
+        sum_xy = libfoo.getfunc('sum_xy_wc', [types.unichar, types.unichar],
+                                types.unichar)
+        res = sum_xy(unichr(1000), unichr(2000))
+        assert type(res) is unicode
+        assert ord(res) == 3000
+
     def test_single_float_args(self):
         """
             DLLEXPORT float sum_xy_float(float x, float y)

diff --git a/pypy/rlib/libffi.py b/pypy/rlib/libffi.py
--- a/pypy/rlib/libffi.py
+++ b/pypy/rlib/libffi.py
@@ -32,6 +32,7 @@
         cls.ulong = clibffi.cast_type_to_ffitype(rffi.ULONG)
         cls.slonglong = clibffi.cast_type_to_ffitype(rffi.LONGLONG)
         cls.ulonglong = clibffi.cast_type_to_ffitype(rffi.ULONGLONG)
+        cls.wchar_t = clibffi.cast_type_to_ffitype(lltype.UniChar)
         del cls._import
 
     @staticmethod

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
@@ -46,7 +46,10 @@
 
     def is_char(self):
         return self.shape == 'c'
-    
+
+    def is_unichar(self):
+        return self.shape == 'u'
+
     def is_longlong(self):
         shape = self.shape
         return libffi.IS_32_BIT and (shape == 'q' or shape == 'Q')
@@ -91,16 +94,16 @@
         W_FFIType('ubyte',     'B', libffi.types.uchar),
         W_FFIType('ulonglong', 'Q', libffi.types.ulonglong),
         #
-        W_FFIType('char',     'c', libffi.types.uchar),
-    
-       
+        W_FFIType('char',      'c', libffi.types.uchar),
+        W_FFIType('unichar',   'u', libffi.types.wchar_t),
+        #
         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:
-        ## 'u' : cast_type_to_ffitype(lltype.UniChar),
+
         ## 's' : ffi_type_pointer,
         ## 'z' : ffi_type_pointer,
         ## 'O' : ffi_type_pointer,
@@ -162,6 +165,9 @@
             elif w_argtype.is_char():
                 w_arg = space.ord(w_arg)
                 argchain.arg(space.int_w(w_arg))
+            elif w_argtype.is_unichar():
+                w_arg = space.ord(w_arg)
+                argchain.arg(space.int_w(w_arg))
             elif w_argtype.is_double():
                 argchain.arg(space.float_w(w_arg))
             elif w_argtype.is_singlefloat():
@@ -208,6 +214,9 @@
         elif w_restype.is_char():
             intres = self.func.call(argchain, rffi.UCHAR)
             return space.wrap(chr(intres))
+        elif w_restype.is_unichar():
+            intres = self.func.call(argchain, rffi.WCHAR_T)
+            return space.wrap(unichr(intres))
         elif w_restype.is_double():
             floatres = self.func.call(argchain, rffi.DOUBLE)
             return space.wrap(floatres)


More information about the Pypy-commit mailing list