Author: Antonio Cuni <[email protected]>
Branch: ffistruct
Changeset: r49037:deebd66d7766
Date: 2011-11-09 17:56 +0100
http://bitbucket.org/pypy/pypy/changeset/deebd66d7766/

Log:    low level support for float fields

diff --git a/pypy/rlib/libffi.py b/pypy/rlib/libffi.py
--- a/pypy/rlib/libffi.py
+++ b/pypy/rlib/libffi.py
@@ -459,6 +459,16 @@
     _struct_setfield(lltype.SignedLongLong, addr, offset, value)
 
 
[email protected]('libffi_struct_getfield(ffitype, addr, offset)')
+def struct_getfield_float(ffitype, addr, offset):
+    value = _struct_getfield(lltype.Float, addr, offset)
+    return value
+
[email protected]('libffi_struct_setfield(ffitype, addr, offset, value)')
+def struct_setfield_float(ffitype, addr, offset, value):
+    _struct_setfield(lltype.Float, addr, offset, value)
+
+
 @specialize.arg(0)
 def _struct_getfield(TYPE, addr, offset):
     """
diff --git a/pypy/rlib/test/test_libffi.py b/pypy/rlib/test/test_libffi.py
--- a/pypy/rlib/test/test_libffi.py
+++ b/pypy/rlib/test/test_libffi.py
@@ -6,7 +6,8 @@
 from pypy.rlib.test.test_clibffi import BaseFfiTest, get_libm_name, 
make_struct_ffitype_e
 from pypy.rlib.libffi import CDLL, Func, get_libc_name, ArgChain, types
 from pypy.rlib.libffi import (IS_32_BIT, struct_getfield_int, 
struct_setfield_int,
-                              struct_getfield_longlong, 
struct_setfield_longlong)
+                              struct_getfield_longlong, 
struct_setfield_longlong,
+                              struct_getfield_float, struct_setfield_float)
 
 class TestLibffiMisc(BaseFfiTest):
 
@@ -95,6 +96,26 @@
         #
         lltype.free(p, flavor='raw')
 
+    def test_struct_fields_float(self):
+        POINT = lltype.Struct('POINT',
+                              ('x', rffi.DOUBLE),
+                              ('y', rffi.DOUBLE)
+                              )
+        y_ofs = 8
+        p = lltype.malloc(POINT, flavor='raw')
+        p.x = 123.4
+        p.y = 567.8
+        addr = rffi.cast(rffi.VOIDP, p)
+        assert struct_getfield_float(types.double, addr, 0) == 123.4
+        assert struct_getfield_float(types.double, addr, y_ofs) == 567.8
+        #
+        struct_setfield_float(types.double, addr, 0, 321.0)
+        struct_setfield_float(types.double, addr, y_ofs, 876.5)
+        assert p.x == 321.0
+        assert p.y == 876.5
+        #
+        lltype.free(p, flavor='raw')
+
 
 class TestLibffiCall(BaseFfiTest):
     """
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to