summaryrefslogtreecommitdiffstats
path: root/Lib/ctypes
diff options
context:
space:
mode:
authorThomas Heller <theller@ctypes.org>2006-07-06 08:48:35 (GMT)
committerThomas Heller <theller@ctypes.org>2006-07-06 08:48:35 (GMT)
commit5becdbee96abef0ec5d5009618d7c3316678168e (patch)
treebf9e1813c6cfc7741827c97f0798500df0c27a5f /Lib/ctypes
parent2329b64c20d0c8891102aa2fb65c836efeea3d7c (diff)
downloadcpython-5becdbee96abef0ec5d5009618d7c3316678168e.zip
cpython-5becdbee96abef0ec5d5009618d7c3316678168e.tar.gz
cpython-5becdbee96abef0ec5d5009618d7c3316678168e.tar.bz2
Patch #1517790: It is now possible to use custom objects in the ctypes
foreign function argtypes sequence as long as they provide a from_param method, no longer is it required that the object is a ctypes type.
Diffstat (limited to 'Lib/ctypes')
-rw-r--r--Lib/ctypes/test/test_parameters.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/Lib/ctypes/test/test_parameters.py b/Lib/ctypes/test/test_parameters.py
index 9537400..1b7f0dc 100644
--- a/Lib/ctypes/test/test_parameters.py
+++ b/Lib/ctypes/test/test_parameters.py
@@ -147,6 +147,41 @@ class SimpleTypesTestCase(unittest.TestCase):
## def test_performance(self):
## check_perf()
+ def test_noctypes_argtype(self):
+ import _ctypes_test
+ from ctypes import CDLL, c_void_p, ArgumentError
+
+ func = CDLL(_ctypes_test.__file__)._testfunc_p_p
+ func.restype = c_void_p
+ # TypeError: has no from_param method
+ self.assertRaises(TypeError, setattr, func, "argtypes", (object,))
+
+ class Adapter(object):
+ def from_param(cls, obj):
+ return None
+
+ func.argtypes = (Adapter(),)
+ self.failUnlessEqual(func(None), None)
+ self.failUnlessEqual(func(object()), None)
+
+ class Adapter(object):
+ def from_param(cls, obj):
+ return obj
+
+ func.argtypes = (Adapter(),)
+ # don't know how to convert parameter 1
+ self.assertRaises(ArgumentError, func, object())
+ self.failUnlessEqual(func(c_void_p(42)), 42)
+
+ class Adapter(object):
+ def from_param(cls, obj):
+ raise ValueError(obj)
+
+ func.argtypes = (Adapter(),)
+ # ArgumentError: argument 1: ValueError: 99
+ self.assertRaises(ArgumentError, func, 99)
+
+
################################################################
if __name__ == '__main__':