[pypy-commit] pypy ffistruct: add support for char/unichar fields

antocuni noreply at buildbot.pypy.org
Wed Nov 9 19:12:11 CET 2011


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: ffistruct
Changeset: r49042:5ed2330756bb
Date: 2011-11-09 19:11 +0100
http://bitbucket.org/pypy/pypy/changeset/5ed2330756bb/

Log:	add support for char/unichar fields

diff --git a/pypy/module/_ffi/interp_struct.py b/pypy/module/_ffi/interp_struct.py
--- a/pypy/module/_ffi/interp_struct.py
+++ b/pypy/module/_ffi/interp_struct.py
@@ -145,6 +145,14 @@
                 return space.wrap(r_uint(value))
             return space.wrap(value)
         #
+        if w_ffitype.is_char():
+            value = libffi.struct_getfield_int(w_ffitype.ffitype, self.rawmem, offset)
+            return space.wrap(chr(value))
+        #
+        if w_ffitype.is_unichar():
+            value = libffi.struct_getfield_int(w_ffitype.ffitype, self.rawmem, offset)
+            return space.wrap(unichr(value))
+        #
         if w_ffitype.is_double():
             value = libffi.struct_getfield_float(w_ffitype.ffitype, self.rawmem, offset)
             return space.wrap(value)
@@ -168,6 +176,11 @@
             libffi.struct_setfield_int(w_ffitype.ffitype, self.rawmem, offset, value)
             return
         #
+        if w_ffitype.is_char() or w_ffitype.is_unichar():
+            value = space.int_w(space.ord(w_value))
+            libffi.struct_setfield_int(w_ffitype.ffitype, self.rawmem, offset, value)
+            return
+        #
         if w_ffitype.is_double():
             value = space.float_w(w_value)
             libffi.struct_setfield_float(w_ffitype.ffitype, self.rawmem, offset, value)
diff --git a/pypy/module/_ffi/test/test_struct.py b/pypy/module/_ffi/test/test_struct.py
--- a/pypy/module/_ffi/test/test_struct.py
+++ b/pypy/module/_ffi/test/test_struct.py
@@ -137,6 +137,8 @@
             Field('ushort', types.ushort),
             Field('uint', types.uint),
             Field('ulong', types.ulong),
+            Field('char', types.char),
+            Field('unichar', types.unichar),
             ]
         descr = _StructDescr('foo', fields)
         struct = descr.allocate()
@@ -150,6 +152,11 @@
         assert struct.getfield('ulong') == sys.maxint*2 + 1
         struct.setfield('ulong', sys.maxint*2 + 2)
         assert struct.getfield('ulong') == 0
+        struct.setfield('char', 'a')
+        assert struct.getfield('char') == 'a'
+        struct.setfield('unichar', u'\u1234')
+        assert struct.getfield('unichar') == u'\u1234'
+        
 
     def test_getfield_setfield_longlong(self):
         import sys


More information about the pypy-commit mailing list