[pypy-commit] pypy ffistruct: add support to write nested structures
antocuni
noreply at buildbot.pypy.org
Tue May 15 17:00:54 CEST 2012
Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: ffistruct
Changeset: r55107:bc08b45940f9
Date: 2012-05-15 16:59 +0200
http://bitbucket.org/pypy/pypy/changeset/bc08b45940f9/
Log: add support to write nested structures
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
@@ -292,8 +292,11 @@
libffi.struct_setfield_singlefloat(w_ffitype.get_ffitype(),
self.rawmem, self.offset, singlefloatval)
- ## def handle_struct(self, w_ffitype, w_structinstance):
- ## ...
+ def handle_struct(self, w_ffitype, w_structinstance):
+ dst = rffi.ptradd(self.rawmem, self.offset)
+ src = w_structinstance.rawmem
+ length = w_ffitype.sizeof()
+ rffi.c_memcpy(dst, src, length)
## def handle_char_p(self, w_ffitype, w_obj, strval):
## ...
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
@@ -281,11 +281,26 @@
#
struct = bar_descr.allocate()
struct.setfield('x', 40)
+ # reading a nested structure yields a reference to it
struct_foo = struct.getfield('foo')
struct_foo.setfield('x', 41)
struct_foo.setfield('y', 42)
mem = self.read_raw_mem(struct.getaddr(), 'c_long', 3)
assert mem == [40, 41, 42]
+ #
+ struct_foo2 = foo_descr.allocate()
+ struct_foo2.setfield('x', 141)
+ struct_foo2.setfield('y', 142)
+ # writing a nested structure copies its memory into the target
+ struct.setfield('foo', struct_foo2)
+ struct_foo2.setfield('x', 241)
+ struct_foo2.setfield('y', 242)
+ mem = self.read_raw_mem(struct.getaddr(), 'c_long', 3)
+ assert mem == [40, 141, 142]
+ mem = self.read_raw_mem(struct_foo2.getaddr(), 'c_long', 2)
+ assert mem == [241, 242]
+
+
def test_compute_shape(self):
from _ffi import Structure, Field, types
More information about the pypy-commit
mailing list