Debian Codegraph @

extra_tests/ctypes_tests/test_callbacks.py

raw symbols SCIP
1 import pytest
2
3 import math
4 from ctypes import *
5 from .support import BaseCTypesTestChecker
6
7 functypes = [CFUNCTYPE]
8 try:
9 functypes.append(WINFUNCTYPE)
10 except NameError:
11 pass
12
13
14 def callback(*args):
15 callback.got_args = args
16 return args[-1]
17
18 unwrapped_types = {
19 c_float: (float,),
20 c_double: (float,),
21 c_char: (bytes,),
22 c_char_p: (bytes,),
23 c_uint: (int,),
24 c_ulong: (int,),
25 }
26
27 @pytest.mark.parametrize("typ, arg", [
28 (c_byte, 42),
29 (c_byte, -42),
30 (c_ubyte, 42),
31 (c_short, 42),
32 (c_short, -42),
33 (c_ushort, 42),
34 (c_int, 42),
35 (c_int, -42),
36 (c_uint, 42),
37 (c_long, 42),
38 (c_long, -42),
39 (c_ulong, 42),
40 (c_longlong, 42),
41 (c_longlong, -42),
42 (c_ulonglong, 42),
43 (c_float, math.e), # only almost equal: double -> float -> double
44 (c_float, -math.e),
45 (c_double, 3.14),
46 (c_double, -3.14),
47 (c_char, b"x"),
48 (c_char, b"a"),
49 ])
50 @pytest.mark.parametrize('functype', functypes)
51 def test_types(typ, arg, functype):
52 PROTO = functype(typ, typ)
53 cfunc = PROTO(callback)
54 result = cfunc(arg)
55 if typ == c_float:
56 assert abs(result - arg) < 0.000001
57 else:
58 assert callback.got_args == (arg,)
59 assert result == arg
60
61 result2 = cfunc(typ(arg))
62 assert type(result2) in unwrapped_types.get(typ, (int,))
63
64 PROTO = functype(typ, c_byte, typ)
65 result = PROTO(callback)(-3, arg)
66 if typ == c_float:
67 assert abs(result - arg) < 0.000001
68 else:
69 assert callback.got_args == (-3, arg)
70 assert result == arg
71
72 @pytest.mark.parametrize('functype', functypes)
73 def test_unsupported_restype_1(functype):
74 # Only "fundamental" result types are supported for callback
75 # functions, the type must have a non-NULL stgdict->setfunc.
76 # POINTER(c_double), for example, is not supported.
77
78 prototype = functype(POINTER(c_double))
79 # The type is checked when the prototype is called
80 with pytest.raises(TypeError):
81 prototype(lambda: None)
82
83
84 def test_callback_with_struct_argument():
85 class RECT(Structure):
86 _fields_ = [("left", c_int), ("top", c_int),
87 ("right", c_int), ("bottom", c_int)]
88
89 proto = CFUNCTYPE(c_int, RECT)
90
91 def callback(point):
92 point.left *= -1
93 return point.left + point.top + point.right + point.bottom
94
95 cbp = proto(callback)
96 rect = RECT(-1000, 100, 10, 1)
97 res = cbp(rect)
98 assert res == 1111
99 assert rect.left == -1000 # must not have been changed!
100
101 def test_callback_from_c_with_struct_argument(dll):
102 class RECT(Structure):
103 _fields_ = [("left", c_long), ("top", c_long),
104 ("right", c_long), ("bottom", c_long)]
105
106 proto = CFUNCTYPE(c_int, RECT)
107
108 def callback(point):
109 return point.left + point.top + point.right + point.bottom
110
111 cbp = proto(callback)
112 rect = RECT(1000, 100, 10, 1)
113
114 call_callback_with_rect = dll.call_callback_with_rect
115 call_callback_with_rect.restype = c_int
116 call_callback_with_rect.argtypes = [proto, RECT]
117 res = call_callback_with_rect(cbp, rect)
118 assert res == 1111
119
120 def test_callback_unsupported_return_struct():
121 class RECT(Structure):
122 _fields_ = [("left", c_int), ("top", c_int),
123 ("right", c_int), ("bottom", c_int)]
124
125 proto = CFUNCTYPE(RECT, c_int)
126 with pytest.raises(TypeError):
127 proto(lambda r: 0)
128
129
130 def test_qsort(dll):
131 PI = POINTER(c_int)
132 A = c_int*5
133 a = A()
134 for i in range(5):
135 a[i] = 5-i
136
137 assert a[0] == 5 # sanity
138
139 def comp(a, b):
140 a = a.contents.value
141 b = b.contents.value
142 if a < b:
143 return -1
144 elif a > b:
145 return 1
146 else:
147 return 0
148 qs = dll.my_qsort
149 qs.restype = None
150 CMP = CFUNCTYPE(c_int, PI, PI)
151 qs.argtypes = (PI, c_size_t, c_size_t, CMP)
152
153 qs(cast(a, PI), 5, sizeof(c_int), CMP(comp))
154
155 res = list(a)
156
157 assert res == [1,2,3,4,5]
158
159 def test_pyobject_as_opaque(dll):
160 def callback(arg):
161 return arg()
162
163 CTP = CFUNCTYPE(c_int, py_object)
164 cfunc = dll._testfunc_callback_opaque
165 cfunc.argtypes = [CTP, py_object]
166 cfunc.restype = c_int
167 res = cfunc(CTP(callback), lambda : 3)
168 assert res == 3
169
170 def test_callback_void(capsys, dll):
171 def callback():
172 pass
173
174 CTP = CFUNCTYPE(None)
175 cfunc = dll._testfunc_callback_void
176 cfunc.argtypes = [CTP]
177 cfunc.restype = int
178 cfunc(CTP(callback))
179 out, err = capsys.readouterr()
180 assert (out, err) == ("", "")
181
182
183 def test_callback_pyobject():
184 def callback(obj):
185 return obj
186
187 FUNC = CFUNCTYPE(py_object, py_object)
188 cfunc = FUNC(callback)
189 param = c_int(42)
190 assert cfunc(param) is param
191
192 def test_raise_argumenterror():
193 def callback(x):
194 pass
195 FUNC = CFUNCTYPE(None, c_void_p)
196 cfunc = FUNC(callback)
197 param = c_uint(42)
198 with pytest.raises(ArgumentError):
199 cfunc(param)