[pypy-svn] r47533 - in pypy/dist/pypy/module/_ffi: . test

fijal at codespeak.net fijal at codespeak.net
Thu Oct 18 11:34:46 CEST 2007


Author: fijal
Date: Thu Oct 18 11:34:46 2007
New Revision: 47533

Modified:
   pypy/dist/pypy/module/_ffi/interp_ffi.py
   pypy/dist/pypy/module/_ffi/test/test__ffi.py
Log:
Support for None as a result type


Modified: pypy/dist/pypy/module/_ffi/interp_ffi.py
==============================================================================
--- pypy/dist/pypy/module/_ffi/interp_ffi.py	(original)
+++ pypy/dist/pypy/module/_ffi/interp_ffi.py	Thu Oct 18 11:34:46 2007
@@ -62,10 +62,16 @@
             raise OperationError(space.w_ValueError, space.wrap(
                 "Uknown type letter %s" % key))
 
-    def ptr(self, space, name, w_argtypes, restype):
+    def ptr(self, space, name, w_argtypes, w_restype):
         """ Get a pointer for function name with provided argtypes
         and restype
         """
+        if space.is_w(w_restype, space.w_None):
+            restype = 'v'
+            ffi_restype = ffi_type_void
+        else:
+            restype = space.str_w(w_restype)
+            ffi_restype = self.get_type(restype)
         w = space.wrap
         w_argtypes = space.newtuple(space.unpackiterable(w_argtypes))
         w_key = space.newtuple([w(name), w_argtypes, w(restype)])
@@ -79,7 +85,6 @@
         argtypes_w = space.unpackiterable(w_argtypes)
         argtypes = [space.str_w(w_arg) for w_arg in argtypes_w]
         ffi_argtypes = [self.get_type(arg) for arg in argtypes]
-        ffi_restype = self.get_type(restype)
         try:
             ptr = self.cdll.getpointer(name, ffi_argtypes, ffi_restype)
             w_funcptr = W_FuncPtr(ptr, argtypes, restype)
@@ -88,7 +93,7 @@
         except KeyError:
             raise OperationError(space.w_AttributeError, space.wrap(
                 "No symbol %s found in library %s" % (name, self.name)))
-    ptr.unwrap_spec = ['self', ObjSpace, str, W_Root, str]
+    ptr.unwrap_spec = ['self', ObjSpace, str, W_Root, W_Root]
 
 def descr_new_cdll(space, w_type, name):
     try:
@@ -219,7 +224,9 @@
             unwrap_value(space, push, self.ptr, i, argtype, w_arg, to_free)
             i += 1
         try:
-            return wrap_value(space, ptr_call, self.ptr, None, self.restype)
+            if self.restype != 'v':
+                return wrap_value(space, ptr_call, self.ptr, None, self.restype)
+            return space.w_None
         finally:
             for elem in to_free:
                 lltype.free(elem, flavor='raw')

Modified: pypy/dist/pypy/module/_ffi/test/test__ffi.py
==============================================================================
--- pypy/dist/pypy/module/_ffi/test/test__ffi.py	(original)
+++ pypy/dist/pypy/module/_ffi/test/test__ffi.py	Thu Oct 18 11:34:46 2007
@@ -25,6 +25,10 @@
            struct x* next;
         };
 
+        void nothing()
+        {
+        }
+
         char inner_struct_elem(struct x *x1)
         {
            return x1->next->x3;
@@ -226,6 +230,13 @@
         assert ptr1 is None
         assert X(get_array_elem_s(a, 1)).x2 == 3
 
+    def test_bad_parameters(self):
+        import _ffi
+        lib = _ffi.CDLL(self.lib_name)
+        nothing = lib.ptr('nothing', [], None)
+        assert nothing() is None
+        raises(AttributeError, "lib.ptr('get_charx', [], None)")
+
     def test_implicit_structure(self):
         skip("Does not work yet")
         import _ffi



More information about the Pypy-commit mailing list