[pypy-svn] r55902 - in pypy/branch/faster-ctypes/pypy/module/_ctypes: . test

fijal at codespeak.net fijal at codespeak.net
Mon Jun 16 21:15:12 CEST 2008


Author: fijal
Date: Mon Jun 16 21:15:11 2008
New Revision: 55902

Modified:
   pypy/branch/faster-ctypes/pypy/module/_ctypes/interp_func.py
   pypy/branch/faster-ctypes/pypy/module/_ctypes/test/test_basic.py
Log:
* Simplify a bit, by using args_w instead of Arguments
* Support assigning to argtypes


Modified: pypy/branch/faster-ctypes/pypy/module/_ctypes/interp_func.py
==============================================================================
--- pypy/branch/faster-ctypes/pypy/module/_ctypes/interp_func.py	(original)
+++ pypy/branch/faster-ctypes/pypy/module/_ctypes/interp_func.py	Mon Jun 16 21:15:11 2008
@@ -23,41 +23,63 @@
         res = TYPEMAP[self.restype]
         self.handle = self.lib.getpointer(self.name, args, res)
 
-    def _guess_argtypes(self, args_w):
-        # XXX
-        return ['d']
+    def _guess_argtypes(self, space, args_w):
+        res = []
+        for w_arg in args_w:
+            if space.is_true(space.isinstance(w_arg, space.w_int)):
+                res.append('i')
+            elif space.is_true(space.isinstance(w_arg, space.w_float)):
+                res.append('d')
+            else:
+                raise NotImplementedError("Arg: %s" % str(w_arg))
+        return res
+
+    def push_arg(self, space, argtype, w_arg):
+        if argtype == 'i':
+            self.handle.push_arg(space.int_w(w_arg))
+        elif argtype == 'd':
+            self.handle.push_arg(space.float_w(w_arg))
+        elif argtype == 'f':
+            self.handle.push_arg(space.float_w(w_arg))
+        else:
+            raise NotImplementedError("Argtype %s" % argtype)
 
-    def call(self, space, args):
+    def call(self, space, args_w):
         """ Calling routine - note that we cache handle to ll
         lib, in order to speed up calls. In case arguments or restype
         is defined, we invalidate a cache and call new handle
         """
-        assert not self.has_argtypes
-        if args:
-            args_w, kwds_w = args.unpack()
-            if kwds_w:
-                raise OperationError(space.w_ValueError, space.wrap(
-                    "Cannot pass keyword arguments to C function"))
-            if args_w:
-                argtypes = self._guess_argtypes(args_w)
-                if self.argtypes != argtypes:
-                    self.argtypes = argtypes
-                    self._regen_handle()
-                for arg in args_w:
-                    self.handle.push_arg(space.float_w(arg))
+        if args_w:
+            if self.has_argtypes:
+                if len(args_w) != len(self.argtypes):
+                    raise OperationError(space.w_TypeError, space.wrap(
+                        "Expected %d args, got %d" % (len(self.argtypes),
+                                                      len(args_w))))
+                argtypes = self.argtypes
+            else:
+                argtypes = self._guess_argtypes(space, args_w)
+            if self.argtypes != argtypes:
+                self.argtypes = argtypes
+                self._regen_handle()
+            for i in range(len(argtypes)):
+                argtype = argtypes[i]
+                w_arg = args_w[i]
+                self.push_arg(space, argtype, w_arg)
         if self.restype == 'i':
             return space.wrap(self.handle.call(lltype.Signed))
         elif self.restype == 'd':
             return space.wrap(self.handle.call(lltype.Float))
         else:
             raise NotImplementedError("restype = %s" % self.restype)
-    call.unwrap_spec = ['self', ObjSpace, Arguments]
+    call.unwrap_spec = ['self', ObjSpace, 'args_w']
 
     def getargtypes(space, self):
-        xxx
+        return self.w_argtypes
 
     def setargtypes(space, self, w_value):
-        xxx
+        self.argtypes = [space.getattr(w_arg, space.wrap('_type_')) for
+                         w_arg in space.unpackiterable(w_value)]
+        self.w_argtypes = w_value
 
     def getrestype(space, self):
         return self.w_restype

Modified: pypy/branch/faster-ctypes/pypy/module/_ctypes/test/test_basic.py
==============================================================================
--- pypy/branch/faster-ctypes/pypy/module/_ctypes/test/test_basic.py	(original)
+++ pypy/branch/faster-ctypes/pypy/module/_ctypes/test/test_basic.py	Mon Jun 16 21:15:11 2008
@@ -14,7 +14,7 @@
         f.restype = c_int
         assert f() == 42
 
-    def test_float_restype(self):
+    def test_double_restype(self):
         from _ctypes import _SimpleCData
         class c_float(_SimpleCData):
             _type_ = 'd'
@@ -22,3 +22,14 @@
         f = self.dll.my_sqrt
         f.restype = c_float
         assert f(4.0) == 2.0
+
+    def test_argtypes(self):
+        from _ctypes import _SimpleCData
+        class c_float(_SimpleCData):
+            _type_ = 'd'
+
+        f = self.dll.my_sqrt
+        f.restype = c_float
+        f.argtypes = [c_float]
+        assert f(4.0) == 2.0
+



More information about the Pypy-commit mailing list