[pypy-svn] pypy jitypes2: support for struct by valure return types also at applevel

antocuni commits-noreply at bitbucket.org
Thu Dec 23 17:34:57 CET 2010


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: jitypes2
Changeset: r40214:052d06548292
Date: 2010-12-23 17:34 +0100
http://bitbucket.org/pypy/pypy/changeset/052d06548292/

Log:	support for struct by valure return types also at applevel

diff --git a/pypy/module/_ffi/test/test__ffi.py b/pypy/module/_ffi/test/test__ffi.py
--- a/pypy/module/_ffi/test/test__ffi.py
+++ b/pypy/module/_ffi/test/test__ffi.py
@@ -252,6 +252,28 @@
         res = sum_point(p.buffer)
         assert res == 42
 
+    def test_byval_result(self):
+        """
+            struct Point make_point(long x, long y) {
+                struct Point p;
+                p.x = x;
+                p.y = y;
+                return p;
+            }
+        """
+        import _rawffi
+        from _ffi import CDLL, types
+        POINT = _rawffi.Structure([('x', 'l'), ('y', 'l')])
+        ffi_point = POINT.get_ffi_type()
+        libfoo = CDLL(self.libfoo_name)
+        make_point = libfoo.getfunc('make_point', [types.slong, types.slong], ffi_point)
+        #
+        adr = make_point(12, 34)
+        p = POINT.fromaddress(adr)
+        assert p.x == 12
+        assert p.y == 34
+        p.free()
+
     def test_TypeError_numargs(self):
         from _ffi import CDLL, types
         libfoo = CDLL(self.libfoo_name)

diff --git a/pypy/module/_ffi/interp_ffi.py b/pypy/module/_ffi/interp_ffi.py
--- a/pypy/module/_ffi/interp_ffi.py
+++ b/pypy/module/_ffi/interp_ffi.py
@@ -124,6 +124,9 @@
             return space.wrap(floatres)
         elif reskind == 'I' or reskind == 'U':
             return self._call_longlong(space, argchain, reskind)
+        elif reskind == 'S':
+            # we return the address of the buffer as an integer
+            return self._call_uint(space, argchain)
         else:
             assert reskind == 'v'
             voidres = self.func.call(argchain, lltype.Void)
@@ -171,7 +174,7 @@
             # special case
             uintres = call(argchain, rffi.ULONG)
             return space.wrap(uintres)
-        elif restype is libffi.types.pointer:
+        elif restype is libffi.types.pointer or restype.c_type == libffi.FFI_TYPE_STRUCT:
             uintres = rffi.cast(rffi.ULONG, call(argchain, rffi.VOIDP))
             return space.wrap(uintres)
         elif restype is libffi.types.uint:


More information about the Pypy-commit mailing list