Debian Codegraph @

Lib/test/test_ctypes/test_functions.py

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