5
dll = CDLL(_ctypes_test.__file__)
8
CALLBACK_FUNCTYPE = WINFUNCTYPE
10
# fake to enable this test on Linux
11
CALLBACK_FUNCTYPE = CFUNCTYPE
13
class POINT(Structure):
14
_fields_ = [("x", c_int), ("y", c_int)]
16
class BasicWrapTestCase(unittest.TestCase):
17
def wrap(self, param):
20
def test_wchar_parm(self):
25
f = dll._testfunc_i_bhilfd
26
f.argtypes = [c_byte, c_wchar, c_int, c_long, c_float, c_double]
27
result = f(self.wrap(1), self.wrap("x"), self.wrap(3), self.wrap(4), self.wrap(5.0), self.wrap(6.0))
28
self.assertEqual(result, 139)
29
self.assertTrue(type(result), int)
31
def test_pointers(self):
33
f.restype = POINTER(c_int)
34
f.argtypes = [POINTER(c_int)]
36
# This only works if the value c_int(42) passed to the
37
# function is still alive while the pointer (the result) is
42
self.assertEqual(pointer(v).contents.value, 42)
43
result = f(self.wrap(pointer(v)))
44
self.assertEqual(type(result), POINTER(c_int))
45
self.assertEqual(result.contents.value, 42)
48
result = f(self.wrap(pointer(v)))
49
self.assertEqual(result.contents.value, v.value)
51
p = pointer(c_int(99))
52
result = f(self.wrap(p))
53
self.assertEqual(result.contents.value, 99)
55
def test_shorts(self):
56
f = dll._testfunc_callback_i_if
59
expected = [262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048,
60
1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1]
66
CallBack = CFUNCTYPE(c_int, c_int)
68
cb = CallBack(callback)
69
f(self.wrap(2**18), self.wrap(cb))
70
self.assertEqual(args, expected)
72
################################################################
74
def test_callbacks(self):
75
f = dll._testfunc_callback_i_if
79
MyCallback = CFUNCTYPE(c_int, c_int)
82
#print "called back with", value
85
cb = MyCallback(callback)
87
result = f(self.wrap(-10), self.wrap(cb))
88
self.assertEqual(result, -18)
91
f.argtypes = [c_int, MyCallback]
92
cb = MyCallback(callback)
94
result = f(self.wrap(-10), self.wrap(cb))
95
self.assertEqual(result, -18)
97
result = f(self.wrap(-10), self.wrap(cb))
98
self.assertEqual(result, -18)
100
AnotherCallback = CALLBACK_FUNCTYPE(c_int, c_int, c_int, c_int, c_int)
102
# check that the prototype works: we call f with wrong
104
cb = AnotherCallback(callback)
105
self.assertRaises(ArgumentError, f, self.wrap(-10), self.wrap(cb))
107
def test_callbacks_2(self):
108
# Can also use simple datatypes as argument type specifiers
109
# for the callback function.
110
# In this case the call receives an instance of that type
111
f = dll._testfunc_callback_i_if
114
MyCallback = CFUNCTYPE(c_int, c_int)
116
f.argtypes = [c_int, MyCallback]
119
#print "called back with", value
120
self.assertEqual(type(value), int)
123
cb = MyCallback(callback)
124
result = f(self.wrap(-10), self.wrap(cb))
125
self.assertEqual(result, -18)
127
def test_longlong_callbacks(self):
129
f = dll._testfunc_callback_q_qf
130
f.restype = c_longlong
132
MyCallback = CFUNCTYPE(c_longlong, c_longlong)
134
f.argtypes = [c_longlong, MyCallback]
137
self.assertIsInstance(value, int)
138
return value & 0x7FFFFFFF
140
cb = MyCallback(callback)
142
self.assertEqual(13577625587, int(f(self.wrap(1000000000000), self.wrap(cb))))
144
def test_byval(self):
148
# EXPORT int _testfunc_byval(point in, point *pout)
149
result = dll._testfunc_byval(ptin, byref(ptout))
150
got = result, ptout.x, ptout.y
152
self.assertEqual(got, expected)
155
ptin = POINT(101, 102)
157
dll._testfunc_byval.argtypes = (POINT, POINTER(POINT))
158
dll._testfunc_byval.restype = c_int
159
result = dll._testfunc_byval(self.wrap(ptin), byref(ptout))
160
got = result, ptout.x, ptout.y
161
expected = 203, 101, 102
162
self.assertEqual(got, expected)
164
def test_struct_return_2H(self):
165
class S2H(Structure):
166
_fields_ = [("x", c_short),
168
dll.ret_2h_func.restype = S2H
169
dll.ret_2h_func.argtypes = [S2H]
171
s2h = dll.ret_2h_func(self.wrap(inp))
172
self.assertEqual((s2h.x, s2h.y), (99*2, 88*3))
174
def test_struct_return_8H(self):
175
class S8I(Structure):
176
_fields_ = [("a", c_int),
184
dll.ret_8i_func.restype = S8I
185
dll.ret_8i_func.argtypes = [S8I]
186
inp = S8I(9, 8, 7, 6, 5, 4, 3, 2)
187
s8i = dll.ret_8i_func(self.wrap(inp))
188
self.assertEqual((s8i.a, s8i.b, s8i.c, s8i.d, s8i.e, s8i.f, s8i.g, s8i.h),
189
(9*2, 8*3, 7*4, 6*5, 5*6, 4*7, 3*8, 2*9))
191
def test_recursive_as_param(self):
192
from ctypes import c_int
199
with self.assertRaises(RuntimeError):
203
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
205
class AsParamWrapper(object):
206
def __init__(self, param):
207
self._as_parameter_ = param
209
class AsParamWrapperTestCase(BasicWrapTestCase):
210
wrap = AsParamWrapper
212
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
214
class AsParamPropertyWrapper(object):
215
def __init__(self, param):
218
def getParameter(self):
220
_as_parameter_ = property(getParameter)
222
class AsParamPropertyWrapperTestCase(BasicWrapTestCase):
223
wrap = AsParamPropertyWrapper
225
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
227
if __name__ == '__main__':