[pypy-svn] r79774 - in pypy/branch/jitypes2/pypy/module/_ffi: . test

antocuni at codespeak.net antocuni at codespeak.net
Fri Dec 3 13:28:16 CET 2010


Author: antocuni
Date: Fri Dec  3 13:28:14 2010
New Revision: 79774

Modified:
   pypy/branch/jitypes2/pypy/module/_ffi/interp_ffi.py
   pypy/branch/jitypes2/pypy/module/_ffi/test/test__ffi.py
Log:
add support for pointers as arguments/return value. Since _ffi is only a thin layer, pointers are represented just as integers at app-level


Modified: pypy/branch/jitypes2/pypy/module/_ffi/interp_ffi.py
==============================================================================
--- pypy/branch/jitypes2/pypy/module/_ffi/interp_ffi.py	(original)
+++ pypy/branch/jitypes2/pypy/module/_ffi/interp_ffi.py	Fri Dec  3 13:28:14 2010
@@ -124,6 +124,8 @@
             intres = rffi.cast(rffi.LONG, call(argchain, rffi.SHORT))
         elif restype is libffi.types.schar:
             intres = rffi.cast(rffi.LONG, call(argchain, rffi.SIGNEDCHAR))
+        elif restype is libffi.types.pointer:
+            intres = rffi.cast(rffi.LONG, call(argchain, rffi.VOIDP))
         else:
             raise OperationError(space.w_ValueError,
                                  space.wrap('Unsupported restype'))

Modified: pypy/branch/jitypes2/pypy/module/_ffi/test/test__ffi.py
==============================================================================
--- pypy/branch/jitypes2/pypy/module/_ffi/test/test__ffi.py	(original)
+++ pypy/branch/jitypes2/pypy/module/_ffi/test/test__ffi.py	Fri Dec  3 13:28:14 2010
@@ -103,6 +103,26 @@
         assert get_dummy() == 0
         assert set_dummy(42) is None
         assert get_dummy() == 42
+        set_dummy(0)
+
+    def test_pointer_args(self):
+        """
+            extern int dummy; // defined in test_void_result 
+            int* get_dummy_ptr() { return &dummy; }
+            void set_val_to_ptr(int* ptr, int val) { *ptr = val; }
+        """
+        from _ffi import CDLL, types
+        libfoo = CDLL(self.libfoo_name)
+        get_dummy = libfoo.getfunc('get_dummy', [], types.sint)
+        get_dummy_ptr = libfoo.getfunc('get_dummy_ptr', [], types.pointer)
+        set_val_to_ptr = libfoo.getfunc('set_val_to_ptr',
+                                        [types.pointer, types.sint],
+                                        types.void)
+        assert get_dummy() == 0
+        ptr = get_dummy_ptr()
+        set_val_to_ptr(ptr, 123)
+        assert get_dummy() == 123
+        set_val_to_ptr(ptr, 0)
 
     def test_unsigned_long_args(self):
         """
@@ -147,6 +167,7 @@
         res = sum_xy(12.34, 56.78)
         assert res == self.f_12_34_plus_56_78
 
+
     def test_TypeError_numargs(self):
         from _ffi import CDLL, types
         libfoo = CDLL(self.libfoo_name)



More information about the Pypy-commit mailing list