[pypy-svn] pypy fast-forward: When a callback function is not given enough arguments, an exception is raised.

amauryfa commits-noreply at bitbucket.org
Fri Jan 14 18:24:54 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: fast-forward
Changeset: r40687:ab15ce45d181
Date: 2011-01-14 15:17 +0100
http://bitbucket.org/pypy/pypy/changeset/ab15ce45d181/

Log:	When a callback function is not given enough arguments, an exception
	is raised. This is different from the case where the callback code
	itself raises an exception: it's caught and 0 is returned...

diff --git a/lib_pypy/_ctypes/function.py b/lib_pypy/_ctypes/function.py
--- a/lib_pypy/_ctypes/function.py
+++ b/lib_pypy/_ctypes/function.py
@@ -164,7 +164,24 @@
     
     def __call__(self, *args):
         if self.callable is not None:
-            args = args[:len(self._argtypes_)]
+            if len(args) == len(self._argtypes_):
+                pass
+            elif self._flags_ & _rawffi.FUNCFLAG_CDECL:
+                if len(args) < len(self._argtypes_):
+                    plural = len(self._argtypes_) > 1 and "s" or ""
+                    raise TypeError(
+                        "This function takes at least %d argument%s (%s given)"
+                        % (len(self._argtypes_), plural, len(args)))
+                else:
+                    # For cdecl functions, we allow more actual arguments
+                    # than the length of the argtypes tuple.
+                    args = args[:len(self._argtypes_)]
+            else:
+                plural = len(self._argtypes_) > 1 and "s" or ""
+                raise TypeError(
+                    "This function takes %d argument%s (%s given)"
+                    % (len(self._argtypes_), plural, len(args)))
+
             try:
                 res = self.callable(*args)
             except:


More information about the Pypy-commit mailing list