1 | n/a | import functools |
---|
2 | n/a | import unittest |
---|
3 | n/a | from ctypes import * |
---|
4 | n/a | from ctypes.test import need_symbol |
---|
5 | n/a | import _ctypes_test |
---|
6 | n/a | |
---|
7 | n/a | class Callbacks(unittest.TestCase): |
---|
8 | n/a | functype = CFUNCTYPE |
---|
9 | n/a | |
---|
10 | n/a | ## def tearDown(self): |
---|
11 | n/a | ## import gc |
---|
12 | n/a | ## gc.collect() |
---|
13 | n/a | |
---|
14 | n/a | def callback(self, *args): |
---|
15 | n/a | self.got_args = args |
---|
16 | n/a | return args[-1] |
---|
17 | n/a | |
---|
18 | n/a | def check_type(self, typ, arg): |
---|
19 | n/a | PROTO = self.functype.__func__(typ, typ) |
---|
20 | n/a | result = PROTO(self.callback)(arg) |
---|
21 | n/a | if typ == c_float: |
---|
22 | n/a | self.assertAlmostEqual(result, arg, places=5) |
---|
23 | n/a | else: |
---|
24 | n/a | self.assertEqual(self.got_args, (arg,)) |
---|
25 | n/a | self.assertEqual(result, arg) |
---|
26 | n/a | |
---|
27 | n/a | PROTO = self.functype.__func__(typ, c_byte, typ) |
---|
28 | n/a | result = PROTO(self.callback)(-3, arg) |
---|
29 | n/a | if typ == c_float: |
---|
30 | n/a | self.assertAlmostEqual(result, arg, places=5) |
---|
31 | n/a | else: |
---|
32 | n/a | self.assertEqual(self.got_args, (-3, arg)) |
---|
33 | n/a | self.assertEqual(result, arg) |
---|
34 | n/a | |
---|
35 | n/a | ################ |
---|
36 | n/a | |
---|
37 | n/a | def test_byte(self): |
---|
38 | n/a | self.check_type(c_byte, 42) |
---|
39 | n/a | self.check_type(c_byte, -42) |
---|
40 | n/a | |
---|
41 | n/a | def test_ubyte(self): |
---|
42 | n/a | self.check_type(c_ubyte, 42) |
---|
43 | n/a | |
---|
44 | n/a | def test_short(self): |
---|
45 | n/a | self.check_type(c_short, 42) |
---|
46 | n/a | self.check_type(c_short, -42) |
---|
47 | n/a | |
---|
48 | n/a | def test_ushort(self): |
---|
49 | n/a | self.check_type(c_ushort, 42) |
---|
50 | n/a | |
---|
51 | n/a | def test_int(self): |
---|
52 | n/a | self.check_type(c_int, 42) |
---|
53 | n/a | self.check_type(c_int, -42) |
---|
54 | n/a | |
---|
55 | n/a | def test_uint(self): |
---|
56 | n/a | self.check_type(c_uint, 42) |
---|
57 | n/a | |
---|
58 | n/a | def test_long(self): |
---|
59 | n/a | self.check_type(c_long, 42) |
---|
60 | n/a | self.check_type(c_long, -42) |
---|
61 | n/a | |
---|
62 | n/a | def test_ulong(self): |
---|
63 | n/a | self.check_type(c_ulong, 42) |
---|
64 | n/a | |
---|
65 | n/a | def test_longlong(self): |
---|
66 | n/a | self.check_type(c_longlong, 42) |
---|
67 | n/a | self.check_type(c_longlong, -42) |
---|
68 | n/a | |
---|
69 | n/a | def test_ulonglong(self): |
---|
70 | n/a | self.check_type(c_ulonglong, 42) |
---|
71 | n/a | |
---|
72 | n/a | def test_float(self): |
---|
73 | n/a | # only almost equal: double -> float -> double |
---|
74 | n/a | import math |
---|
75 | n/a | self.check_type(c_float, math.e) |
---|
76 | n/a | self.check_type(c_float, -math.e) |
---|
77 | n/a | |
---|
78 | n/a | def test_double(self): |
---|
79 | n/a | self.check_type(c_double, 3.14) |
---|
80 | n/a | self.check_type(c_double, -3.14) |
---|
81 | n/a | |
---|
82 | n/a | def test_longdouble(self): |
---|
83 | n/a | self.check_type(c_longdouble, 3.14) |
---|
84 | n/a | self.check_type(c_longdouble, -3.14) |
---|
85 | n/a | |
---|
86 | n/a | def test_char(self): |
---|
87 | n/a | self.check_type(c_char, b"x") |
---|
88 | n/a | self.check_type(c_char, b"a") |
---|
89 | n/a | |
---|
90 | n/a | # disabled: would now (correctly) raise a RuntimeWarning about |
---|
91 | n/a | # a memory leak. A callback function cannot return a non-integral |
---|
92 | n/a | # C type without causing a memory leak. |
---|
93 | n/a | @unittest.skip('test disabled') |
---|
94 | n/a | def test_char_p(self): |
---|
95 | n/a | self.check_type(c_char_p, "abc") |
---|
96 | n/a | self.check_type(c_char_p, "def") |
---|
97 | n/a | |
---|
98 | n/a | def test_pyobject(self): |
---|
99 | n/a | o = () |
---|
100 | n/a | from sys import getrefcount as grc |
---|
101 | n/a | for o in (), [], object(): |
---|
102 | n/a | initial = grc(o) |
---|
103 | n/a | # This call leaks a reference to 'o'... |
---|
104 | n/a | self.check_type(py_object, o) |
---|
105 | n/a | before = grc(o) |
---|
106 | n/a | # ...but this call doesn't leak any more. Where is the refcount? |
---|
107 | n/a | self.check_type(py_object, o) |
---|
108 | n/a | after = grc(o) |
---|
109 | n/a | self.assertEqual((after, o), (before, o)) |
---|
110 | n/a | |
---|
111 | n/a | def test_unsupported_restype_1(self): |
---|
112 | n/a | # Only "fundamental" result types are supported for callback |
---|
113 | n/a | # functions, the type must have a non-NULL stgdict->setfunc. |
---|
114 | n/a | # POINTER(c_double), for example, is not supported. |
---|
115 | n/a | |
---|
116 | n/a | prototype = self.functype.__func__(POINTER(c_double)) |
---|
117 | n/a | # The type is checked when the prototype is called |
---|
118 | n/a | self.assertRaises(TypeError, prototype, lambda: None) |
---|
119 | n/a | |
---|
120 | n/a | def test_unsupported_restype_2(self): |
---|
121 | n/a | prototype = self.functype.__func__(object) |
---|
122 | n/a | self.assertRaises(TypeError, prototype, lambda: None) |
---|
123 | n/a | |
---|
124 | n/a | def test_issue_7959(self): |
---|
125 | n/a | proto = self.functype.__func__(None) |
---|
126 | n/a | |
---|
127 | n/a | class X(object): |
---|
128 | n/a | def func(self): pass |
---|
129 | n/a | def __init__(self): |
---|
130 | n/a | self.v = proto(self.func) |
---|
131 | n/a | |
---|
132 | n/a | import gc |
---|
133 | n/a | for i in range(32): |
---|
134 | n/a | X() |
---|
135 | n/a | gc.collect() |
---|
136 | n/a | live = [x for x in gc.get_objects() |
---|
137 | n/a | if isinstance(x, X)] |
---|
138 | n/a | self.assertEqual(len(live), 0) |
---|
139 | n/a | |
---|
140 | n/a | def test_issue12483(self): |
---|
141 | n/a | import gc |
---|
142 | n/a | class Nasty: |
---|
143 | n/a | def __del__(self): |
---|
144 | n/a | gc.collect() |
---|
145 | n/a | CFUNCTYPE(None)(lambda x=Nasty(): None) |
---|
146 | n/a | |
---|
147 | n/a | |
---|
148 | n/a | @need_symbol('WINFUNCTYPE') |
---|
149 | n/a | class StdcallCallbacks(Callbacks): |
---|
150 | n/a | try: |
---|
151 | n/a | functype = WINFUNCTYPE |
---|
152 | n/a | except NameError: |
---|
153 | n/a | pass |
---|
154 | n/a | |
---|
155 | n/a | ################################################################ |
---|
156 | n/a | |
---|
157 | n/a | class SampleCallbacksTestCase(unittest.TestCase): |
---|
158 | n/a | |
---|
159 | n/a | def test_integrate(self): |
---|
160 | n/a | # Derived from some then non-working code, posted by David Foster |
---|
161 | n/a | dll = CDLL(_ctypes_test.__file__) |
---|
162 | n/a | |
---|
163 | n/a | # The function prototype called by 'integrate': double func(double); |
---|
164 | n/a | CALLBACK = CFUNCTYPE(c_double, c_double) |
---|
165 | n/a | |
---|
166 | n/a | # The integrate function itself, exposed from the _ctypes_test dll |
---|
167 | n/a | integrate = dll.integrate |
---|
168 | n/a | integrate.argtypes = (c_double, c_double, CALLBACK, c_long) |
---|
169 | n/a | integrate.restype = c_double |
---|
170 | n/a | |
---|
171 | n/a | def func(x): |
---|
172 | n/a | return x**2 |
---|
173 | n/a | |
---|
174 | n/a | result = integrate(0.0, 1.0, CALLBACK(func), 10) |
---|
175 | n/a | diff = abs(result - 1./3.) |
---|
176 | n/a | |
---|
177 | n/a | self.assertLess(diff, 0.01, "%s not less than 0.01" % diff) |
---|
178 | n/a | |
---|
179 | n/a | def test_issue_8959_a(self): |
---|
180 | n/a | from ctypes.util import find_library |
---|
181 | n/a | libc_path = find_library("c") |
---|
182 | n/a | if not libc_path: |
---|
183 | n/a | self.skipTest('could not find libc') |
---|
184 | n/a | libc = CDLL(libc_path) |
---|
185 | n/a | |
---|
186 | n/a | @CFUNCTYPE(c_int, POINTER(c_int), POINTER(c_int)) |
---|
187 | n/a | def cmp_func(a, b): |
---|
188 | n/a | return a[0] - b[0] |
---|
189 | n/a | |
---|
190 | n/a | array = (c_int * 5)(5, 1, 99, 7, 33) |
---|
191 | n/a | |
---|
192 | n/a | libc.qsort(array, len(array), sizeof(c_int), cmp_func) |
---|
193 | n/a | self.assertEqual(array[:], [1, 5, 7, 33, 99]) |
---|
194 | n/a | |
---|
195 | n/a | @need_symbol('WINFUNCTYPE') |
---|
196 | n/a | def test_issue_8959_b(self): |
---|
197 | n/a | from ctypes.wintypes import BOOL, HWND, LPARAM |
---|
198 | n/a | global windowCount |
---|
199 | n/a | windowCount = 0 |
---|
200 | n/a | |
---|
201 | n/a | @WINFUNCTYPE(BOOL, HWND, LPARAM) |
---|
202 | n/a | def EnumWindowsCallbackFunc(hwnd, lParam): |
---|
203 | n/a | global windowCount |
---|
204 | n/a | windowCount += 1 |
---|
205 | n/a | return True #Allow windows to keep enumerating |
---|
206 | n/a | |
---|
207 | n/a | windll.user32.EnumWindows(EnumWindowsCallbackFunc, 0) |
---|
208 | n/a | |
---|
209 | n/a | def test_callback_register_int(self): |
---|
210 | n/a | # Issue #8275: buggy handling of callback args under Win64 |
---|
211 | n/a | # NOTE: should be run on release builds as well |
---|
212 | n/a | dll = CDLL(_ctypes_test.__file__) |
---|
213 | n/a | CALLBACK = CFUNCTYPE(c_int, c_int, c_int, c_int, c_int, c_int) |
---|
214 | n/a | # All this function does is call the callback with its args squared |
---|
215 | n/a | func = dll._testfunc_cbk_reg_int |
---|
216 | n/a | func.argtypes = (c_int, c_int, c_int, c_int, c_int, CALLBACK) |
---|
217 | n/a | func.restype = c_int |
---|
218 | n/a | |
---|
219 | n/a | def callback(a, b, c, d, e): |
---|
220 | n/a | return a + b + c + d + e |
---|
221 | n/a | |
---|
222 | n/a | result = func(2, 3, 4, 5, 6, CALLBACK(callback)) |
---|
223 | n/a | self.assertEqual(result, callback(2*2, 3*3, 4*4, 5*5, 6*6)) |
---|
224 | n/a | |
---|
225 | n/a | def test_callback_register_double(self): |
---|
226 | n/a | # Issue #8275: buggy handling of callback args under Win64 |
---|
227 | n/a | # NOTE: should be run on release builds as well |
---|
228 | n/a | dll = CDLL(_ctypes_test.__file__) |
---|
229 | n/a | CALLBACK = CFUNCTYPE(c_double, c_double, c_double, c_double, |
---|
230 | n/a | c_double, c_double) |
---|
231 | n/a | # All this function does is call the callback with its args squared |
---|
232 | n/a | func = dll._testfunc_cbk_reg_double |
---|
233 | n/a | func.argtypes = (c_double, c_double, c_double, |
---|
234 | n/a | c_double, c_double, CALLBACK) |
---|
235 | n/a | func.restype = c_double |
---|
236 | n/a | |
---|
237 | n/a | def callback(a, b, c, d, e): |
---|
238 | n/a | return a + b + c + d + e |
---|
239 | n/a | |
---|
240 | n/a | result = func(1.1, 2.2, 3.3, 4.4, 5.5, CALLBACK(callback)) |
---|
241 | n/a | self.assertEqual(result, |
---|
242 | n/a | callback(1.1*1.1, 2.2*2.2, 3.3*3.3, 4.4*4.4, 5.5*5.5)) |
---|
243 | n/a | |
---|
244 | n/a | def test_callback_large_struct(self): |
---|
245 | n/a | class Check: pass |
---|
246 | n/a | |
---|
247 | n/a | class X(Structure): |
---|
248 | n/a | _fields_ = [ |
---|
249 | n/a | ('first', c_ulong), |
---|
250 | n/a | ('second', c_ulong), |
---|
251 | n/a | ('third', c_ulong), |
---|
252 | n/a | ] |
---|
253 | n/a | |
---|
254 | n/a | def callback(check, s): |
---|
255 | n/a | check.first = s.first |
---|
256 | n/a | check.second = s.second |
---|
257 | n/a | check.third = s.third |
---|
258 | n/a | |
---|
259 | n/a | check = Check() |
---|
260 | n/a | s = X() |
---|
261 | n/a | s.first = 0xdeadbeef |
---|
262 | n/a | s.second = 0xcafebabe |
---|
263 | n/a | s.third = 0x0bad1dea |
---|
264 | n/a | |
---|
265 | n/a | CALLBACK = CFUNCTYPE(None, X) |
---|
266 | n/a | dll = CDLL(_ctypes_test.__file__) |
---|
267 | n/a | func = dll._testfunc_cbk_large_struct |
---|
268 | n/a | func.argtypes = (X, CALLBACK) |
---|
269 | n/a | func.restype = None |
---|
270 | n/a | # the function just calls the callback with the passed structure |
---|
271 | n/a | func(s, CALLBACK(functools.partial(callback, check))) |
---|
272 | n/a | self.assertEqual(check.first, s.first) |
---|
273 | n/a | self.assertEqual(check.second, s.second) |
---|
274 | n/a | self.assertEqual(check.third, s.third) |
---|
275 | n/a | self.assertEqual(check.first, 0xdeadbeef) |
---|
276 | n/a | self.assertEqual(check.second, 0xcafebabe) |
---|
277 | n/a | self.assertEqual(check.third, 0x0bad1dea) |
---|
278 | n/a | |
---|
279 | n/a | ################################################################ |
---|
280 | n/a | |
---|
281 | n/a | if __name__ == '__main__': |
---|
282 | n/a | unittest.main() |
---|