1 | n/a | import unittest |
---|
2 | n/a | from ctypes import * |
---|
3 | n/a | from ctypes.test import need_symbol |
---|
4 | n/a | import _ctypes_test |
---|
5 | n/a | |
---|
6 | n/a | dll = CDLL(_ctypes_test.__file__) |
---|
7 | n/a | |
---|
8 | n/a | try: |
---|
9 | n/a | CALLBACK_FUNCTYPE = WINFUNCTYPE |
---|
10 | n/a | except NameError: |
---|
11 | n/a | # fake to enable this test on Linux |
---|
12 | n/a | CALLBACK_FUNCTYPE = CFUNCTYPE |
---|
13 | n/a | |
---|
14 | n/a | class POINT(Structure): |
---|
15 | n/a | _fields_ = [("x", c_int), ("y", c_int)] |
---|
16 | n/a | |
---|
17 | n/a | class BasicWrapTestCase(unittest.TestCase): |
---|
18 | n/a | def wrap(self, param): |
---|
19 | n/a | return param |
---|
20 | n/a | |
---|
21 | n/a | @need_symbol('c_wchar') |
---|
22 | n/a | def test_wchar_parm(self): |
---|
23 | n/a | f = dll._testfunc_i_bhilfd |
---|
24 | n/a | f.argtypes = [c_byte, c_wchar, c_int, c_long, c_float, c_double] |
---|
25 | n/a | result = f(self.wrap(1), self.wrap("x"), self.wrap(3), self.wrap(4), self.wrap(5.0), self.wrap(6.0)) |
---|
26 | n/a | self.assertEqual(result, 139) |
---|
27 | n/a | self.assertTrue(type(result), int) |
---|
28 | n/a | |
---|
29 | n/a | def test_pointers(self): |
---|
30 | n/a | f = dll._testfunc_p_p |
---|
31 | n/a | f.restype = POINTER(c_int) |
---|
32 | n/a | f.argtypes = [POINTER(c_int)] |
---|
33 | n/a | |
---|
34 | n/a | # This only works if the value c_int(42) passed to the |
---|
35 | n/a | # function is still alive while the pointer (the result) is |
---|
36 | n/a | # used. |
---|
37 | n/a | |
---|
38 | n/a | v = c_int(42) |
---|
39 | n/a | |
---|
40 | n/a | self.assertEqual(pointer(v).contents.value, 42) |
---|
41 | n/a | result = f(self.wrap(pointer(v))) |
---|
42 | n/a | self.assertEqual(type(result), POINTER(c_int)) |
---|
43 | n/a | self.assertEqual(result.contents.value, 42) |
---|
44 | n/a | |
---|
45 | n/a | # This on works... |
---|
46 | n/a | result = f(self.wrap(pointer(v))) |
---|
47 | n/a | self.assertEqual(result.contents.value, v.value) |
---|
48 | n/a | |
---|
49 | n/a | p = pointer(c_int(99)) |
---|
50 | n/a | result = f(self.wrap(p)) |
---|
51 | n/a | self.assertEqual(result.contents.value, 99) |
---|
52 | n/a | |
---|
53 | n/a | def test_shorts(self): |
---|
54 | n/a | f = dll._testfunc_callback_i_if |
---|
55 | n/a | |
---|
56 | n/a | args = [] |
---|
57 | n/a | expected = [262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048, |
---|
58 | n/a | 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1] |
---|
59 | n/a | |
---|
60 | n/a | def callback(v): |
---|
61 | n/a | args.append(v) |
---|
62 | n/a | return v |
---|
63 | n/a | |
---|
64 | n/a | CallBack = CFUNCTYPE(c_int, c_int) |
---|
65 | n/a | |
---|
66 | n/a | cb = CallBack(callback) |
---|
67 | n/a | f(self.wrap(2**18), self.wrap(cb)) |
---|
68 | n/a | self.assertEqual(args, expected) |
---|
69 | n/a | |
---|
70 | n/a | ################################################################ |
---|
71 | n/a | |
---|
72 | n/a | def test_callbacks(self): |
---|
73 | n/a | f = dll._testfunc_callback_i_if |
---|
74 | n/a | f.restype = c_int |
---|
75 | n/a | f.argtypes = None |
---|
76 | n/a | |
---|
77 | n/a | MyCallback = CFUNCTYPE(c_int, c_int) |
---|
78 | n/a | |
---|
79 | n/a | def callback(value): |
---|
80 | n/a | #print "called back with", value |
---|
81 | n/a | return value |
---|
82 | n/a | |
---|
83 | n/a | cb = MyCallback(callback) |
---|
84 | n/a | |
---|
85 | n/a | result = f(self.wrap(-10), self.wrap(cb)) |
---|
86 | n/a | self.assertEqual(result, -18) |
---|
87 | n/a | |
---|
88 | n/a | # test with prototype |
---|
89 | n/a | f.argtypes = [c_int, MyCallback] |
---|
90 | n/a | cb = MyCallback(callback) |
---|
91 | n/a | |
---|
92 | n/a | result = f(self.wrap(-10), self.wrap(cb)) |
---|
93 | n/a | self.assertEqual(result, -18) |
---|
94 | n/a | |
---|
95 | n/a | result = f(self.wrap(-10), self.wrap(cb)) |
---|
96 | n/a | self.assertEqual(result, -18) |
---|
97 | n/a | |
---|
98 | n/a | AnotherCallback = CALLBACK_FUNCTYPE(c_int, c_int, c_int, c_int, c_int) |
---|
99 | n/a | |
---|
100 | n/a | # check that the prototype works: we call f with wrong |
---|
101 | n/a | # argument types |
---|
102 | n/a | cb = AnotherCallback(callback) |
---|
103 | n/a | self.assertRaises(ArgumentError, f, self.wrap(-10), self.wrap(cb)) |
---|
104 | n/a | |
---|
105 | n/a | def test_callbacks_2(self): |
---|
106 | n/a | # Can also use simple datatypes as argument type specifiers |
---|
107 | n/a | # for the callback function. |
---|
108 | n/a | # In this case the call receives an instance of that type |
---|
109 | n/a | f = dll._testfunc_callback_i_if |
---|
110 | n/a | f.restype = c_int |
---|
111 | n/a | |
---|
112 | n/a | MyCallback = CFUNCTYPE(c_int, c_int) |
---|
113 | n/a | |
---|
114 | n/a | f.argtypes = [c_int, MyCallback] |
---|
115 | n/a | |
---|
116 | n/a | def callback(value): |
---|
117 | n/a | #print "called back with", value |
---|
118 | n/a | self.assertEqual(type(value), int) |
---|
119 | n/a | return value |
---|
120 | n/a | |
---|
121 | n/a | cb = MyCallback(callback) |
---|
122 | n/a | result = f(self.wrap(-10), self.wrap(cb)) |
---|
123 | n/a | self.assertEqual(result, -18) |
---|
124 | n/a | |
---|
125 | n/a | def test_longlong_callbacks(self): |
---|
126 | n/a | |
---|
127 | n/a | f = dll._testfunc_callback_q_qf |
---|
128 | n/a | f.restype = c_longlong |
---|
129 | n/a | |
---|
130 | n/a | MyCallback = CFUNCTYPE(c_longlong, c_longlong) |
---|
131 | n/a | |
---|
132 | n/a | f.argtypes = [c_longlong, MyCallback] |
---|
133 | n/a | |
---|
134 | n/a | def callback(value): |
---|
135 | n/a | self.assertIsInstance(value, int) |
---|
136 | n/a | return value & 0x7FFFFFFF |
---|
137 | n/a | |
---|
138 | n/a | cb = MyCallback(callback) |
---|
139 | n/a | |
---|
140 | n/a | self.assertEqual(13577625587, int(f(self.wrap(1000000000000), self.wrap(cb)))) |
---|
141 | n/a | |
---|
142 | n/a | def test_byval(self): |
---|
143 | n/a | # without prototype |
---|
144 | n/a | ptin = POINT(1, 2) |
---|
145 | n/a | ptout = POINT() |
---|
146 | n/a | # EXPORT int _testfunc_byval(point in, point *pout) |
---|
147 | n/a | result = dll._testfunc_byval(ptin, byref(ptout)) |
---|
148 | n/a | got = result, ptout.x, ptout.y |
---|
149 | n/a | expected = 3, 1, 2 |
---|
150 | n/a | self.assertEqual(got, expected) |
---|
151 | n/a | |
---|
152 | n/a | # with prototype |
---|
153 | n/a | ptin = POINT(101, 102) |
---|
154 | n/a | ptout = POINT() |
---|
155 | n/a | dll._testfunc_byval.argtypes = (POINT, POINTER(POINT)) |
---|
156 | n/a | dll._testfunc_byval.restype = c_int |
---|
157 | n/a | result = dll._testfunc_byval(self.wrap(ptin), byref(ptout)) |
---|
158 | n/a | got = result, ptout.x, ptout.y |
---|
159 | n/a | expected = 203, 101, 102 |
---|
160 | n/a | self.assertEqual(got, expected) |
---|
161 | n/a | |
---|
162 | n/a | def test_struct_return_2H(self): |
---|
163 | n/a | class S2H(Structure): |
---|
164 | n/a | _fields_ = [("x", c_short), |
---|
165 | n/a | ("y", c_short)] |
---|
166 | n/a | dll.ret_2h_func.restype = S2H |
---|
167 | n/a | dll.ret_2h_func.argtypes = [S2H] |
---|
168 | n/a | inp = S2H(99, 88) |
---|
169 | n/a | s2h = dll.ret_2h_func(self.wrap(inp)) |
---|
170 | n/a | self.assertEqual((s2h.x, s2h.y), (99*2, 88*3)) |
---|
171 | n/a | |
---|
172 | n/a | def test_struct_return_8H(self): |
---|
173 | n/a | class S8I(Structure): |
---|
174 | n/a | _fields_ = [("a", c_int), |
---|
175 | n/a | ("b", c_int), |
---|
176 | n/a | ("c", c_int), |
---|
177 | n/a | ("d", c_int), |
---|
178 | n/a | ("e", c_int), |
---|
179 | n/a | ("f", c_int), |
---|
180 | n/a | ("g", c_int), |
---|
181 | n/a | ("h", c_int)] |
---|
182 | n/a | dll.ret_8i_func.restype = S8I |
---|
183 | n/a | dll.ret_8i_func.argtypes = [S8I] |
---|
184 | n/a | inp = S8I(9, 8, 7, 6, 5, 4, 3, 2) |
---|
185 | n/a | s8i = dll.ret_8i_func(self.wrap(inp)) |
---|
186 | n/a | self.assertEqual((s8i.a, s8i.b, s8i.c, s8i.d, s8i.e, s8i.f, s8i.g, s8i.h), |
---|
187 | n/a | (9*2, 8*3, 7*4, 6*5, 5*6, 4*7, 3*8, 2*9)) |
---|
188 | n/a | |
---|
189 | n/a | def test_recursive_as_param(self): |
---|
190 | n/a | from ctypes import c_int |
---|
191 | n/a | |
---|
192 | n/a | class A(object): |
---|
193 | n/a | pass |
---|
194 | n/a | |
---|
195 | n/a | a = A() |
---|
196 | n/a | a._as_parameter_ = a |
---|
197 | n/a | with self.assertRaises(RecursionError): |
---|
198 | n/a | c_int.from_param(a) |
---|
199 | n/a | |
---|
200 | n/a | |
---|
201 | n/a | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
---|
202 | n/a | |
---|
203 | n/a | class AsParamWrapper(object): |
---|
204 | n/a | def __init__(self, param): |
---|
205 | n/a | self._as_parameter_ = param |
---|
206 | n/a | |
---|
207 | n/a | class AsParamWrapperTestCase(BasicWrapTestCase): |
---|
208 | n/a | wrap = AsParamWrapper |
---|
209 | n/a | |
---|
210 | n/a | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
---|
211 | n/a | |
---|
212 | n/a | class AsParamPropertyWrapper(object): |
---|
213 | n/a | def __init__(self, param): |
---|
214 | n/a | self._param = param |
---|
215 | n/a | |
---|
216 | n/a | def getParameter(self): |
---|
217 | n/a | return self._param |
---|
218 | n/a | _as_parameter_ = property(getParameter) |
---|
219 | n/a | |
---|
220 | n/a | class AsParamPropertyWrapperTestCase(BasicWrapTestCase): |
---|
221 | n/a | wrap = AsParamPropertyWrapper |
---|
222 | n/a | |
---|
223 | n/a | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
---|
224 | n/a | |
---|
225 | n/a | if __name__ == '__main__': |
---|
226 | n/a | unittest.main() |
---|