Author: Antonio Cuni <[email protected]>
Branch: ffistruct
Changeset: r49039:0aad7df24682
Date: 2011-11-09 18:02 +0100
http://bitbucket.org/pypy/pypy/changeset/0aad7df24682/

Log:    applevel support for float 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,10 @@
                 return space.wrap(r_uint(value))
             return space.wrap(value)
         #
+        if w_ffitype.is_double():
+            value = libffi.struct_getfield_float(w_ffitype.ffitype, 
self.rawmem, offset)
+            return space.wrap(value)
+        #
         assert False, 'unknown type'
 
     @unwrap_spec(name=str)
@@ -160,6 +164,11 @@
             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)
+            return
+        #
         assert False, 'unknown type'
 
 W__StructInstance.typedef = TypeDef(
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
@@ -168,6 +168,20 @@
         mem = self.read_raw_mem(struct.getaddr(), 'c_longlong', 2)
         assert mem == [-9223372036854775808, -1]
 
+    def test_getfield_setfield_float(self):
+        import sys
+        from _ffi import _StructDescr, Field, types
+        longsize = types.slong.sizeof()
+        fields = [
+            Field('x', types.double),
+            ]
+        descr = _StructDescr('foo', fields)
+        struct = descr.allocate()
+        struct.setfield('x', 123.4)
+        assert struct.getfield('x') == 123.4
+        mem = self.read_raw_mem(struct.getaddr(), 'c_double', 1)
+        assert mem == [123.4]
+
     def test_compute_shape(self):
         from _ffi import Structure, Field, types
         class Point(Structure):
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to