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.
This commit is contained in:
Thomas Heller 2006-07-06 08:48:35 +00:00
parent 2329b64c20
commit 5becdbee96
3 changed files with 41 additions and 3 deletions

View file

@ -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__':