[pypy-svn] r43661 - in pypy/branch/kill-ctypes/pypy/rpython/lltypesystem: . test
fijal at codespeak.net
fijal at codespeak.net
Sat May 26 12:00:37 CEST 2007
Author: fijal
Date: Sat May 26 12:00:36 2007
New Revision: 43661
Modified:
pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/rffi.py
pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/test/test_rffi.py
Log:
Another bit - a structure: test and a small helper
Modified: pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/rffi.py
==============================================================================
--- pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/rffi.py (original)
+++ pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/rffi.py Sat May 26 12:00:36 2007
@@ -8,6 +8,19 @@
sources=tuple(sources),
includes=tuple(includes))
+def cstruct(name, *fields, **kwds):
+ """ A small helper to create external C structure, not the
+ pypy one
+ """
+ if 'hints' not in kwds:
+ kwds['hints'] = {}
+ kwds['hints']['external'] = 'C'
+ kwds['hints']['c_name'] = name
+ # XXX obscure hack, stolen for unknown reason from ctypes,
+ # probably because of _ attributes
+ c_fields = [('c_' + key, value) for key, value in fields]
+ return lltype.Ptr(lltype.Struct(name, *c_fields, **kwds))
+
# char *
CCHARP = lltype.Ptr(lltype.Array(lltype.Char, hints={'nolength': True}))
Modified: pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/test/test_rffi.py
==============================================================================
--- pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/test/test_rffi.py (original)
+++ pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/test/test_rffi.py Sat May 26 12:00:36 2007
@@ -78,3 +78,41 @@
xf = compile(f, [], backendopt=False)
assert xf() == 8
+
+def test_struct():
+ h_source = """
+ struct xx {
+ int one;
+ char two;
+ int three;
+ };
+ """
+ h_file = udir.join("structxx.h")
+ h_file.write(h_source)
+
+ c_source = """
+ #include "structxx.h"
+ int f(struct xx* z)
+ {
+ return (z->one + z->three);
+ }
+ """
+ TP = cstruct('xx', ('one', Signed), ('two', Char), ('three', Signed))
+
+ c_file = udir.join("structxx.c")
+ c_file.write(c_source)
+ z = llexternal('f', [TP], Signed, sources=[str(c_file)],
+ includes=[str(h_file)])
+
+ def f():
+ struct = lltype.malloc(TP.TO, flavor='raw')
+ struct.c_one = 3
+ struct.c_two = '\x33'
+ struct.c_three = 5
+ result = z(struct)
+ lltype.free(struct, flavor='raw')
+ return result
+
+ fn = compile(f, [], backendopt=False)
+ assert fn() == 8
+
More information about the Pypy-commit
mailing list