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