[pypy-svn] pypy default: Make ctypes warn about calling functions before declaring argument

iko commits-noreply at bitbucket.org
Thu Apr 28 16:14:00 CEST 2011


Author: Anders Hammarquist <iko at iko.pp.se>
Branch: 
Changeset: r43712:78112cfc1526
Date: 2011-04-28 15:58 +0200
http://bitbucket.org/pypy/pypy/changeset/78112cfc1526/

Log:	Make ctypes warn about calling functions before declaring argument
	and return value types.

diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_functions.py b/pypy/module/test_lib_pypy/ctypes_tests/test_functions.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_functions.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_functions.py
@@ -427,3 +427,28 @@
         a[1].x = 33
         u = dll.ret_un_func(a[1])
         assert u.y == 33*10000
+
+    def test_warnings(self):
+        import warnings
+        warnings.simplefilter("always")
+        with warnings.catch_warnings(record=True) as w:
+            dll.get_an_integer()
+            assert len(w) == 2
+            assert issubclass(w[0].category, RuntimeWarning)
+            assert issubclass(w[1].category, RuntimeWarning)
+            assert "C function without declared arguments called" in str(w[0].message)
+            assert "C function without declared return type called" in str(w[1].message)
+
+        with warnings.catch_warnings(record=True) as w:
+            dll.get_an_integer.argtypes = []
+            dll.get_an_integer()
+            assert len(w) == 1
+            assert issubclass(w[0].category, RuntimeWarning)
+            assert "C function without declared return type called" in str(w[0].message)
+            
+        with warnings.catch_warnings(record=True) as w:
+            dll.get_an_integer.restype = None
+            dll.get_an_integer()
+            assert len(w) == 0
+            
+        warnings.resetwarnings()

diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_callbacks.py b/pypy/module/test_lib_pypy/ctypes_tests/test_callbacks.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_callbacks.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_callbacks.py
@@ -217,6 +217,7 @@
         CTP = CFUNCTYPE(None)
         cfunc = dll._testfunc_callback_void
         cfunc.argtypes = [CTP]
+        cfunc.restype = int
         cfunc(CTP(callback))
         out, err = capsys.readouterr()
         assert (out, err) == ("", "")

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
@@ -1,6 +1,7 @@
 import _rawffi
 import sys
 import traceback
+import warnings
 
 from _ctypes.basics import ArgumentError, keepalive_key
 from _ctypes.basics import _CData, _CDataMeta, cdata_from_address
@@ -69,6 +70,8 @@
     _com_index = None
     _com_iid = None
 
+    __restype_set = False
+
     def _getargtypes(self):
         return self._argtypes_
 
@@ -134,6 +137,7 @@
         return self._restype_
 
     def _setrestype(self, restype):
+        self.__restype_set = True
         self._ptr = None
         if restype is int:
             from ctypes import c_int
@@ -288,7 +292,13 @@
             return
 
         if argtypes is None:
+            warnings.warn('C function without declared arguments called',
+                          RuntimeWarning, stacklevel=2)
             argtypes = []
+            
+        if not self.__restype_set:
+            warnings.warn('C function without declared return type called',
+                          RuntimeWarning, stacklevel=2)
 
         if self._com_index:
             from ctypes import cast, c_void_p, POINTER


More information about the Pypy-commit mailing list