1 | n/a | """ |
---|
2 | n/a | Here is probably the place to write the docs, since the test-cases |
---|
3 | n/a | show how the type behave. |
---|
4 | n/a | |
---|
5 | n/a | Later... |
---|
6 | n/a | """ |
---|
7 | n/a | |
---|
8 | n/a | from ctypes import * |
---|
9 | n/a | from ctypes.test import need_symbol |
---|
10 | n/a | import sys, unittest |
---|
11 | n/a | |
---|
12 | n/a | try: |
---|
13 | n/a | WINFUNCTYPE |
---|
14 | n/a | except NameError: |
---|
15 | n/a | # fake to enable this test on Linux |
---|
16 | n/a | WINFUNCTYPE = CFUNCTYPE |
---|
17 | n/a | |
---|
18 | n/a | import _ctypes_test |
---|
19 | n/a | dll = CDLL(_ctypes_test.__file__) |
---|
20 | n/a | if sys.platform == "win32": |
---|
21 | n/a | windll = WinDLL(_ctypes_test.__file__) |
---|
22 | n/a | |
---|
23 | n/a | class POINT(Structure): |
---|
24 | n/a | _fields_ = [("x", c_int), ("y", c_int)] |
---|
25 | n/a | class RECT(Structure): |
---|
26 | n/a | _fields_ = [("left", c_int), ("top", c_int), |
---|
27 | n/a | ("right", c_int), ("bottom", c_int)] |
---|
28 | n/a | class FunctionTestCase(unittest.TestCase): |
---|
29 | n/a | |
---|
30 | n/a | def test_mro(self): |
---|
31 | n/a | # in Python 2.3, this raises TypeError: MRO conflict among bases classes, |
---|
32 | n/a | # in Python 2.2 it works. |
---|
33 | n/a | # |
---|
34 | n/a | # But in early versions of _ctypes.c, the result of tp_new |
---|
35 | n/a | # wasn't checked, and it even crashed Python. |
---|
36 | n/a | # Found by Greg Chapman. |
---|
37 | n/a | |
---|
38 | n/a | try: |
---|
39 | n/a | class X(object, Array): |
---|
40 | n/a | _length_ = 5 |
---|
41 | n/a | _type_ = "i" |
---|
42 | n/a | except TypeError: |
---|
43 | n/a | pass |
---|
44 | n/a | |
---|
45 | n/a | |
---|
46 | n/a | from _ctypes import _Pointer |
---|
47 | n/a | try: |
---|
48 | n/a | class X(object, _Pointer): |
---|
49 | n/a | pass |
---|
50 | n/a | except TypeError: |
---|
51 | n/a | pass |
---|
52 | n/a | |
---|
53 | n/a | from _ctypes import _SimpleCData |
---|
54 | n/a | try: |
---|
55 | n/a | class X(object, _SimpleCData): |
---|
56 | n/a | _type_ = "i" |
---|
57 | n/a | except TypeError: |
---|
58 | n/a | pass |
---|
59 | n/a | |
---|
60 | n/a | try: |
---|
61 | n/a | class X(object, Structure): |
---|
62 | n/a | _fields_ = [] |
---|
63 | n/a | except TypeError: |
---|
64 | n/a | pass |
---|
65 | n/a | |
---|
66 | n/a | |
---|
67 | n/a | @need_symbol('c_wchar') |
---|
68 | n/a | def test_wchar_parm(self): |
---|
69 | n/a | f = dll._testfunc_i_bhilfd |
---|
70 | n/a | f.argtypes = [c_byte, c_wchar, c_int, c_long, c_float, c_double] |
---|
71 | n/a | result = f(1, "x", 3, 4, 5.0, 6.0) |
---|
72 | n/a | self.assertEqual(result, 139) |
---|
73 | n/a | self.assertEqual(type(result), int) |
---|
74 | n/a | |
---|
75 | n/a | @need_symbol('c_wchar') |
---|
76 | n/a | def test_wchar_result(self): |
---|
77 | n/a | f = dll._testfunc_i_bhilfd |
---|
78 | n/a | f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double] |
---|
79 | n/a | f.restype = c_wchar |
---|
80 | n/a | result = f(0, 0, 0, 0, 0, 0) |
---|
81 | n/a | self.assertEqual(result, '\x00') |
---|
82 | n/a | |
---|
83 | n/a | def test_voidresult(self): |
---|
84 | n/a | f = dll._testfunc_v |
---|
85 | n/a | f.restype = None |
---|
86 | n/a | f.argtypes = [c_int, c_int, POINTER(c_int)] |
---|
87 | n/a | result = c_int() |
---|
88 | n/a | self.assertEqual(None, f(1, 2, byref(result))) |
---|
89 | n/a | self.assertEqual(result.value, 3) |
---|
90 | n/a | |
---|
91 | n/a | def test_intresult(self): |
---|
92 | n/a | f = dll._testfunc_i_bhilfd |
---|
93 | n/a | f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double] |
---|
94 | n/a | f.restype = c_int |
---|
95 | n/a | result = f(1, 2, 3, 4, 5.0, 6.0) |
---|
96 | n/a | self.assertEqual(result, 21) |
---|
97 | n/a | self.assertEqual(type(result), int) |
---|
98 | n/a | |
---|
99 | n/a | result = f(-1, -2, -3, -4, -5.0, -6.0) |
---|
100 | n/a | self.assertEqual(result, -21) |
---|
101 | n/a | self.assertEqual(type(result), int) |
---|
102 | n/a | |
---|
103 | n/a | # If we declare the function to return a short, |
---|
104 | n/a | # is the high part split off? |
---|
105 | n/a | f.restype = c_short |
---|
106 | n/a | result = f(1, 2, 3, 4, 5.0, 6.0) |
---|
107 | n/a | self.assertEqual(result, 21) |
---|
108 | n/a | self.assertEqual(type(result), int) |
---|
109 | n/a | |
---|
110 | n/a | result = f(1, 2, 3, 0x10004, 5.0, 6.0) |
---|
111 | n/a | self.assertEqual(result, 21) |
---|
112 | n/a | self.assertEqual(type(result), int) |
---|
113 | n/a | |
---|
114 | n/a | # You cannot assign character format codes as restype any longer |
---|
115 | n/a | self.assertRaises(TypeError, setattr, f, "restype", "i") |
---|
116 | n/a | |
---|
117 | n/a | def test_floatresult(self): |
---|
118 | n/a | f = dll._testfunc_f_bhilfd |
---|
119 | n/a | f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double] |
---|
120 | n/a | f.restype = c_float |
---|
121 | n/a | result = f(1, 2, 3, 4, 5.0, 6.0) |
---|
122 | n/a | self.assertEqual(result, 21) |
---|
123 | n/a | self.assertEqual(type(result), float) |
---|
124 | n/a | |
---|
125 | n/a | result = f(-1, -2, -3, -4, -5.0, -6.0) |
---|
126 | n/a | self.assertEqual(result, -21) |
---|
127 | n/a | self.assertEqual(type(result), float) |
---|
128 | n/a | |
---|
129 | n/a | def test_doubleresult(self): |
---|
130 | n/a | f = dll._testfunc_d_bhilfd |
---|
131 | n/a | f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double] |
---|
132 | n/a | f.restype = c_double |
---|
133 | n/a | result = f(1, 2, 3, 4, 5.0, 6.0) |
---|
134 | n/a | self.assertEqual(result, 21) |
---|
135 | n/a | self.assertEqual(type(result), float) |
---|
136 | n/a | |
---|
137 | n/a | result = f(-1, -2, -3, -4, -5.0, -6.0) |
---|
138 | n/a | self.assertEqual(result, -21) |
---|
139 | n/a | self.assertEqual(type(result), float) |
---|
140 | n/a | |
---|
141 | n/a | def test_longdoubleresult(self): |
---|
142 | n/a | f = dll._testfunc_D_bhilfD |
---|
143 | n/a | f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_longdouble] |
---|
144 | n/a | f.restype = c_longdouble |
---|
145 | n/a | result = f(1, 2, 3, 4, 5.0, 6.0) |
---|
146 | n/a | self.assertEqual(result, 21) |
---|
147 | n/a | self.assertEqual(type(result), float) |
---|
148 | n/a | |
---|
149 | n/a | result = f(-1, -2, -3, -4, -5.0, -6.0) |
---|
150 | n/a | self.assertEqual(result, -21) |
---|
151 | n/a | self.assertEqual(type(result), float) |
---|
152 | n/a | |
---|
153 | n/a | @need_symbol('c_longlong') |
---|
154 | n/a | def test_longlongresult(self): |
---|
155 | n/a | f = dll._testfunc_q_bhilfd |
---|
156 | n/a | f.restype = c_longlong |
---|
157 | n/a | f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double] |
---|
158 | n/a | result = f(1, 2, 3, 4, 5.0, 6.0) |
---|
159 | n/a | self.assertEqual(result, 21) |
---|
160 | n/a | |
---|
161 | n/a | f = dll._testfunc_q_bhilfdq |
---|
162 | n/a | f.restype = c_longlong |
---|
163 | n/a | f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double, c_longlong] |
---|
164 | n/a | result = f(1, 2, 3, 4, 5.0, 6.0, 21) |
---|
165 | n/a | self.assertEqual(result, 42) |
---|
166 | n/a | |
---|
167 | n/a | def test_stringresult(self): |
---|
168 | n/a | f = dll._testfunc_p_p |
---|
169 | n/a | f.argtypes = None |
---|
170 | n/a | f.restype = c_char_p |
---|
171 | n/a | result = f(b"123") |
---|
172 | n/a | self.assertEqual(result, b"123") |
---|
173 | n/a | |
---|
174 | n/a | result = f(None) |
---|
175 | n/a | self.assertEqual(result, None) |
---|
176 | n/a | |
---|
177 | n/a | def test_pointers(self): |
---|
178 | n/a | f = dll._testfunc_p_p |
---|
179 | n/a | f.restype = POINTER(c_int) |
---|
180 | n/a | f.argtypes = [POINTER(c_int)] |
---|
181 | n/a | |
---|
182 | n/a | # This only works if the value c_int(42) passed to the |
---|
183 | n/a | # function is still alive while the pointer (the result) is |
---|
184 | n/a | # used. |
---|
185 | n/a | |
---|
186 | n/a | v = c_int(42) |
---|
187 | n/a | |
---|
188 | n/a | self.assertEqual(pointer(v).contents.value, 42) |
---|
189 | n/a | result = f(pointer(v)) |
---|
190 | n/a | self.assertEqual(type(result), POINTER(c_int)) |
---|
191 | n/a | self.assertEqual(result.contents.value, 42) |
---|
192 | n/a | |
---|
193 | n/a | # This on works... |
---|
194 | n/a | result = f(pointer(v)) |
---|
195 | n/a | self.assertEqual(result.contents.value, v.value) |
---|
196 | n/a | |
---|
197 | n/a | p = pointer(c_int(99)) |
---|
198 | n/a | result = f(p) |
---|
199 | n/a | self.assertEqual(result.contents.value, 99) |
---|
200 | n/a | |
---|
201 | n/a | arg = byref(v) |
---|
202 | n/a | result = f(arg) |
---|
203 | n/a | self.assertNotEqual(result.contents, v.value) |
---|
204 | n/a | |
---|
205 | n/a | self.assertRaises(ArgumentError, f, byref(c_short(22))) |
---|
206 | n/a | |
---|
207 | n/a | # It is dangerous, however, because you don't control the lifetime |
---|
208 | n/a | # of the pointer: |
---|
209 | n/a | result = f(byref(c_int(99))) |
---|
210 | n/a | self.assertNotEqual(result.contents, 99) |
---|
211 | n/a | |
---|
212 | n/a | def test_errors(self): |
---|
213 | n/a | f = dll._testfunc_p_p |
---|
214 | n/a | f.restype = c_int |
---|
215 | n/a | |
---|
216 | n/a | class X(Structure): |
---|
217 | n/a | _fields_ = [("y", c_int)] |
---|
218 | n/a | |
---|
219 | n/a | self.assertRaises(TypeError, f, X()) #cannot convert parameter |
---|
220 | n/a | |
---|
221 | n/a | ################################################################ |
---|
222 | n/a | def test_shorts(self): |
---|
223 | n/a | f = dll._testfunc_callback_i_if |
---|
224 | n/a | |
---|
225 | n/a | args = [] |
---|
226 | n/a | expected = [262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048, |
---|
227 | n/a | 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1] |
---|
228 | n/a | |
---|
229 | n/a | def callback(v): |
---|
230 | n/a | args.append(v) |
---|
231 | n/a | return v |
---|
232 | n/a | |
---|
233 | n/a | CallBack = CFUNCTYPE(c_int, c_int) |
---|
234 | n/a | |
---|
235 | n/a | cb = CallBack(callback) |
---|
236 | n/a | f(2**18, cb) |
---|
237 | n/a | self.assertEqual(args, expected) |
---|
238 | n/a | |
---|
239 | n/a | ################################################################ |
---|
240 | n/a | |
---|
241 | n/a | |
---|
242 | n/a | def test_callbacks(self): |
---|
243 | n/a | f = dll._testfunc_callback_i_if |
---|
244 | n/a | f.restype = c_int |
---|
245 | n/a | f.argtypes = None |
---|
246 | n/a | |
---|
247 | n/a | MyCallback = CFUNCTYPE(c_int, c_int) |
---|
248 | n/a | |
---|
249 | n/a | def callback(value): |
---|
250 | n/a | #print "called back with", value |
---|
251 | n/a | return value |
---|
252 | n/a | |
---|
253 | n/a | cb = MyCallback(callback) |
---|
254 | n/a | result = f(-10, cb) |
---|
255 | n/a | self.assertEqual(result, -18) |
---|
256 | n/a | |
---|
257 | n/a | # test with prototype |
---|
258 | n/a | f.argtypes = [c_int, MyCallback] |
---|
259 | n/a | cb = MyCallback(callback) |
---|
260 | n/a | result = f(-10, cb) |
---|
261 | n/a | self.assertEqual(result, -18) |
---|
262 | n/a | |
---|
263 | n/a | AnotherCallback = WINFUNCTYPE(c_int, c_int, c_int, c_int, c_int) |
---|
264 | n/a | |
---|
265 | n/a | # check that the prototype works: we call f with wrong |
---|
266 | n/a | # argument types |
---|
267 | n/a | cb = AnotherCallback(callback) |
---|
268 | n/a | self.assertRaises(ArgumentError, f, -10, cb) |
---|
269 | n/a | |
---|
270 | n/a | |
---|
271 | n/a | def test_callbacks_2(self): |
---|
272 | n/a | # Can also use simple datatypes as argument type specifiers |
---|
273 | n/a | # for the callback function. |
---|
274 | n/a | # In this case the call receives an instance of that type |
---|
275 | n/a | f = dll._testfunc_callback_i_if |
---|
276 | n/a | f.restype = c_int |
---|
277 | n/a | |
---|
278 | n/a | MyCallback = CFUNCTYPE(c_int, c_int) |
---|
279 | n/a | |
---|
280 | n/a | f.argtypes = [c_int, MyCallback] |
---|
281 | n/a | |
---|
282 | n/a | def callback(value): |
---|
283 | n/a | #print "called back with", value |
---|
284 | n/a | self.assertEqual(type(value), int) |
---|
285 | n/a | return value |
---|
286 | n/a | |
---|
287 | n/a | cb = MyCallback(callback) |
---|
288 | n/a | result = f(-10, cb) |
---|
289 | n/a | self.assertEqual(result, -18) |
---|
290 | n/a | |
---|
291 | n/a | @need_symbol('c_longlong') |
---|
292 | n/a | def test_longlong_callbacks(self): |
---|
293 | n/a | |
---|
294 | n/a | f = dll._testfunc_callback_q_qf |
---|
295 | n/a | f.restype = c_longlong |
---|
296 | n/a | |
---|
297 | n/a | MyCallback = CFUNCTYPE(c_longlong, c_longlong) |
---|
298 | n/a | |
---|
299 | n/a | f.argtypes = [c_longlong, MyCallback] |
---|
300 | n/a | |
---|
301 | n/a | def callback(value): |
---|
302 | n/a | self.assertIsInstance(value, int) |
---|
303 | n/a | return value & 0x7FFFFFFF |
---|
304 | n/a | |
---|
305 | n/a | cb = MyCallback(callback) |
---|
306 | n/a | |
---|
307 | n/a | self.assertEqual(13577625587, f(1000000000000, cb)) |
---|
308 | n/a | |
---|
309 | n/a | def test_errors(self): |
---|
310 | n/a | self.assertRaises(AttributeError, getattr, dll, "_xxx_yyy") |
---|
311 | n/a | self.assertRaises(ValueError, c_int.in_dll, dll, "_xxx_yyy") |
---|
312 | n/a | |
---|
313 | n/a | def test_byval(self): |
---|
314 | n/a | |
---|
315 | n/a | # without prototype |
---|
316 | n/a | ptin = POINT(1, 2) |
---|
317 | n/a | ptout = POINT() |
---|
318 | n/a | # EXPORT int _testfunc_byval(point in, point *pout) |
---|
319 | n/a | result = dll._testfunc_byval(ptin, byref(ptout)) |
---|
320 | n/a | got = result, ptout.x, ptout.y |
---|
321 | n/a | expected = 3, 1, 2 |
---|
322 | n/a | self.assertEqual(got, expected) |
---|
323 | n/a | |
---|
324 | n/a | # with prototype |
---|
325 | n/a | ptin = POINT(101, 102) |
---|
326 | n/a | ptout = POINT() |
---|
327 | n/a | dll._testfunc_byval.argtypes = (POINT, POINTER(POINT)) |
---|
328 | n/a | dll._testfunc_byval.restype = c_int |
---|
329 | n/a | result = dll._testfunc_byval(ptin, byref(ptout)) |
---|
330 | n/a | got = result, ptout.x, ptout.y |
---|
331 | n/a | expected = 203, 101, 102 |
---|
332 | n/a | self.assertEqual(got, expected) |
---|
333 | n/a | |
---|
334 | n/a | def test_struct_return_2H(self): |
---|
335 | n/a | class S2H(Structure): |
---|
336 | n/a | _fields_ = [("x", c_short), |
---|
337 | n/a | ("y", c_short)] |
---|
338 | n/a | dll.ret_2h_func.restype = S2H |
---|
339 | n/a | dll.ret_2h_func.argtypes = [S2H] |
---|
340 | n/a | inp = S2H(99, 88) |
---|
341 | n/a | s2h = dll.ret_2h_func(inp) |
---|
342 | n/a | self.assertEqual((s2h.x, s2h.y), (99*2, 88*3)) |
---|
343 | n/a | |
---|
344 | n/a | @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test') |
---|
345 | n/a | def test_struct_return_2H_stdcall(self): |
---|
346 | n/a | class S2H(Structure): |
---|
347 | n/a | _fields_ = [("x", c_short), |
---|
348 | n/a | ("y", c_short)] |
---|
349 | n/a | |
---|
350 | n/a | windll.s_ret_2h_func.restype = S2H |
---|
351 | n/a | windll.s_ret_2h_func.argtypes = [S2H] |
---|
352 | n/a | s2h = windll.s_ret_2h_func(S2H(99, 88)) |
---|
353 | n/a | self.assertEqual((s2h.x, s2h.y), (99*2, 88*3)) |
---|
354 | n/a | |
---|
355 | n/a | def test_struct_return_8H(self): |
---|
356 | n/a | class S8I(Structure): |
---|
357 | n/a | _fields_ = [("a", c_int), |
---|
358 | n/a | ("b", c_int), |
---|
359 | n/a | ("c", c_int), |
---|
360 | n/a | ("d", c_int), |
---|
361 | n/a | ("e", c_int), |
---|
362 | n/a | ("f", c_int), |
---|
363 | n/a | ("g", c_int), |
---|
364 | n/a | ("h", c_int)] |
---|
365 | n/a | dll.ret_8i_func.restype = S8I |
---|
366 | n/a | dll.ret_8i_func.argtypes = [S8I] |
---|
367 | n/a | inp = S8I(9, 8, 7, 6, 5, 4, 3, 2) |
---|
368 | n/a | s8i = dll.ret_8i_func(inp) |
---|
369 | n/a | self.assertEqual((s8i.a, s8i.b, s8i.c, s8i.d, s8i.e, s8i.f, s8i.g, s8i.h), |
---|
370 | n/a | (9*2, 8*3, 7*4, 6*5, 5*6, 4*7, 3*8, 2*9)) |
---|
371 | n/a | |
---|
372 | n/a | @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test') |
---|
373 | n/a | def test_struct_return_8H_stdcall(self): |
---|
374 | n/a | class S8I(Structure): |
---|
375 | n/a | _fields_ = [("a", c_int), |
---|
376 | n/a | ("b", c_int), |
---|
377 | n/a | ("c", c_int), |
---|
378 | n/a | ("d", c_int), |
---|
379 | n/a | ("e", c_int), |
---|
380 | n/a | ("f", c_int), |
---|
381 | n/a | ("g", c_int), |
---|
382 | n/a | ("h", c_int)] |
---|
383 | n/a | windll.s_ret_8i_func.restype = S8I |
---|
384 | n/a | windll.s_ret_8i_func.argtypes = [S8I] |
---|
385 | n/a | inp = S8I(9, 8, 7, 6, 5, 4, 3, 2) |
---|
386 | n/a | s8i = windll.s_ret_8i_func(inp) |
---|
387 | n/a | self.assertEqual( |
---|
388 | n/a | (s8i.a, s8i.b, s8i.c, s8i.d, s8i.e, s8i.f, s8i.g, s8i.h), |
---|
389 | n/a | (9*2, 8*3, 7*4, 6*5, 5*6, 4*7, 3*8, 2*9)) |
---|
390 | n/a | |
---|
391 | n/a | def test_sf1651235(self): |
---|
392 | n/a | # see http://www.python.org/sf/1651235 |
---|
393 | n/a | |
---|
394 | n/a | proto = CFUNCTYPE(c_int, RECT, POINT) |
---|
395 | n/a | def callback(*args): |
---|
396 | n/a | return 0 |
---|
397 | n/a | |
---|
398 | n/a | callback = proto(callback) |
---|
399 | n/a | self.assertRaises(ArgumentError, lambda: callback((1, 2, 3, 4), POINT())) |
---|
400 | n/a | |
---|
401 | n/a | if __name__ == '__main__': |
---|
402 | n/a | unittest.main() |
---|