| 1 |
import ctypes |
| 2 |
import functools |
| 3 |
import gc |
| 4 |
import math |
| 5 |
import sys |
| 6 |
import unittest |
| 7 |
from _ctypes import CTYPES_MAX_ARGCOUNT |
| 8 |
from ctypes import (CDLL, cdll, Structure, CFUNCTYPE, |
| 9 |
ArgumentError, POINTER, sizeof, |
| 10 |
c_byte, c_ubyte, c_char, |
| 11 |
c_short, c_ushort, c_int, c_uint, |
| 12 |
c_long, c_longlong, c_ulonglong, c_ulong, |
| 13 |
c_float, c_double, c_longdouble, py_object) |
| 14 |
from ctypes.util import find_library |
| 15 |
from test import support |
| 16 |
from test.support import import_helper |
| 17 |
_ctypes_test = import_helper.import_module("_ctypes_test") |
| 18 |
|
| 19 |
|
| 20 |
class Callbacks(unittest.TestCase): |
| 21 |
functype = CFUNCTYPE |
| 22 |
|
| 23 |
def callback(self, *args): |
| 24 |
self.got_args = args |
| 25 |
return args[-1] |
| 26 |
|
| 27 |
def check_type(self, typ, arg): |
| 28 |
PROTO = self.functype.__func__(typ, typ) |
| 29 |
result = PROTO(self.callback)(arg) |
| 30 |
if typ == c_float: |
| 31 |
self.assertAlmostEqual(result, arg, places=5) |
| 32 |
else: |
| 33 |
self.assertEqual(self.got_args, (arg,)) |
| 34 |
self.assertEqual(result, arg) |
| 35 |
|
| 36 |
PROTO = self.functype.__func__(typ, c_byte, typ) |
| 37 |
result = PROTO(self.callback)(-3, arg) |
| 38 |
if typ == c_float: |
| 39 |
self.assertAlmostEqual(result, arg, places=5) |
| 40 |
else: |
| 41 |
self.assertEqual(self.got_args, (-3, arg)) |
| 42 |
self.assertEqual(result, arg) |
| 43 |
|
| 44 |
def test_byte(self): |
| 45 |
self.check_type(c_byte, 42) |
| 46 |
self.check_type(c_byte, -42) |
| 47 |
|
| 48 |
def test_ubyte(self): |
| 49 |
self.check_type(c_ubyte, 42) |
| 50 |
|
| 51 |
def test_short(self): |
| 52 |
self.check_type(c_short, 42) |
| 53 |
self.check_type(c_short, -42) |
| 54 |
|
| 55 |
def test_ushort(self): |
| 56 |
self.check_type(c_ushort, 42) |
| 57 |
|
| 58 |
def test_int(self): |
| 59 |
self.check_type(c_int, 42) |
| 60 |
self.check_type(c_int, -42) |
| 61 |
|
| 62 |
def test_uint(self): |
| 63 |
self.check_type(c_uint, 42) |
| 64 |
|
| 65 |
def test_long(self): |
| 66 |
self.check_type(c_long, 42) |
| 67 |
self.check_type(c_long, -42) |
| 68 |
|
| 69 |
def test_ulong(self): |
| 70 |
self.check_type(c_ulong, 42) |
| 71 |
|
| 72 |
def test_longlong(self): |
| 73 |
self.check_type(c_longlong, 42) |
| 74 |
self.check_type(c_longlong, -42) |
| 75 |
|
| 76 |
def test_ulonglong(self): |
| 77 |
self.check_type(c_ulonglong, 42) |
| 78 |
|
| 79 |
def test_float(self): |
| 80 |
# only almost equal: double -> float -> double |
| 81 |
self.check_type(c_float, math.e) |
| 82 |
self.check_type(c_float, -math.e) |
| 83 |
|
| 84 |
def test_double(self): |
| 85 |
self.check_type(c_double, 3.14) |
| 86 |
self.check_type(c_double, -3.14) |
| 87 |
|
| 88 |
def test_longdouble(self): |
| 89 |
self.check_type(c_longdouble, 3.14) |
| 90 |
self.check_type(c_longdouble, -3.14) |
| 91 |
|
| 92 |
def test_char(self): |
| 93 |
self.check_type(c_char, b"x") |
| 94 |
self.check_type(c_char, b"a") |
| 95 |
|
| 96 |
def test_pyobject(self): |
| 97 |
o = () |
| 98 |
for o in (), [], object(): |
| 99 |
initial = sys.getrefcount(o) |
| 100 |
# This call leaks a reference to 'o'... |
| 101 |
self.check_type(py_object, o) |
| 102 |
before = sys.getrefcount(o) |
| 103 |
# ...but this call doesn't leak any more. Where is the refcount? |
| 104 |
self.check_type(py_object, o) |
| 105 |
after = sys.getrefcount(o) |
| 106 |
self.assertEqual((after, o), (before, o)) |
| 107 |
|
| 108 |
def test_unsupported_restype_1(self): |
| 109 |
# Only "fundamental" result types are supported for callback |
| 110 |
# functions, the type must have a non-NULL stginfo->setfunc. |
| 111 |
# POINTER(c_double), for example, is not supported. |
| 112 |
|
| 113 |
prototype = self.functype.__func__(POINTER(c_double)) |
| 114 |
# The type is checked when the prototype is called |
| 115 |
self.assertRaises(TypeError, prototype, lambda: None) |
| 116 |
|
| 117 |
def test_unsupported_restype_2(self): |
| 118 |
prototype = self.functype.__func__(object) |
| 119 |
self.assertRaises(TypeError, prototype, lambda: None) |
| 120 |
|
| 121 |
def test_issue_7959(self): |
| 122 |
proto = self.functype.__func__(None) |
| 123 |
|
| 124 |
class X: |
| 125 |
def func(self): pass |
| 126 |
def __init__(self): |
| 127 |
self.v = proto(self.func) |
| 128 |
|
| 129 |
for i in range(32): |
| 130 |
X() |
| 131 |
gc.collect() |
| 132 |
live = [x for x in gc.get_objects() |
| 133 |
if isinstance(x, X)] |
| 134 |
self.assertEqual(len(live), 0) |
| 135 |
|
| 136 |
def test_issue12483(self): |
| 137 |
class Nasty: |
| 138 |
def __del__(self): |
| 139 |
gc.collect() |
| 140 |
CFUNCTYPE(None)(lambda x=Nasty(): None) |
| 141 |
|
| 142 |
@unittest.skipUnless(hasattr(ctypes, 'WINFUNCTYPE'), |
| 143 |
'ctypes.WINFUNCTYPE is required') |
| 144 |
def test_i38748_stackCorruption(self): |
| 145 |
callback_funcType = ctypes.WINFUNCTYPE(c_long, c_long, c_longlong) |
| 146 |
@callback_funcType |
| 147 |
def callback(a, b): |
| 148 |
c = a + b |
| 149 |
print(f"a={a}, b={b}, c={c}") |
| 150 |
return c |
| 151 |
dll = cdll[_ctypes_test.__file__] |
| 152 |
with support.captured_stdout() as out: |
| 153 |
# With no fix for i38748, the next line will raise OSError and cause the test to fail. |
| 154 |
self.assertEqual(dll._test_i38748_runCallback(callback, 5, 10), 15) |
| 155 |
self.assertEqual(out.getvalue(), "a=5, b=10, c=15\n") |
| 156 |
|
| 157 |
if hasattr(ctypes, 'WINFUNCTYPE'): |
| 158 |
class StdcallCallbacks(Callbacks): |
| 159 |
functype = ctypes.WINFUNCTYPE |
| 160 |
|
| 161 |
|
| 162 |
class SampleCallbacksTestCase(unittest.TestCase): |
| 163 |
|
| 164 |
def test_integrate(self): |
| 165 |
# Derived from some then non-working code, posted by David Foster |
| 166 |
dll = CDLL(_ctypes_test.__file__) |
| 167 |
|
| 168 |
# The function prototype called by 'integrate': double func(double); |
| 169 |
CALLBACK = CFUNCTYPE(c_double, c_double) |
| 170 |
|
| 171 |
# The integrate function itself, exposed from the _ctypes_test dll |
| 172 |
integrate = dll.integrate |
| 173 |
integrate.argtypes = (c_double, c_double, CALLBACK, c_long) |
| 174 |
integrate.restype = c_double |
| 175 |
|
| 176 |
def func(x): |
| 177 |
return x**2 |
| 178 |
|
| 179 |
result = integrate(0.0, 1.0, CALLBACK(func), 10) |
| 180 |
diff = abs(result - 1./3.) |
| 181 |
|
| 182 |
self.assertLess(diff, 0.01, "%s not less than 0.01" % diff) |
| 183 |
|
| 184 |
def test_issue_8959_a(self): |
| 185 |
libc_path = find_library("c") |
| 186 |
if not libc_path: |
| 187 |
self.skipTest('could not find libc') |
| 188 |
libc = CDLL(libc_path) |
| 189 |
|
| 190 |
@CFUNCTYPE(c_int, POINTER(c_int), POINTER(c_int)) |
| 191 |
def cmp_func(a, b): |
| 192 |
return a[0] - b[0] |
| 193 |
|
| 194 |
array = (c_int * 5)(5, 1, 99, 7, 33) |
| 195 |
|
| 196 |
libc.qsort(array, len(array), sizeof(c_int), cmp_func) |
| 197 |
self.assertEqual(array[:], [1, 5, 7, 33, 99]) |
| 198 |
|
| 199 |
@unittest.skipUnless(hasattr(ctypes, 'WINFUNCTYPE'), |
| 200 |
'ctypes.WINFUNCTYPE is required') |
| 201 |
def test_issue_8959_b(self): |
| 202 |
from ctypes.wintypes import BOOL, HWND, LPARAM |
| 203 |
global windowCount |
| 204 |
windowCount = 0 |
| 205 |
|
| 206 |
@ctypes.WINFUNCTYPE(BOOL, HWND, LPARAM) |
| 207 |
def EnumWindowsCallbackFunc(hwnd, lParam): |
| 208 |
global windowCount |
| 209 |
windowCount += 1 |
| 210 |
return True #Allow windows to keep enumerating |
| 211 |
|
| 212 |
user32 = ctypes.windll.user32 |
| 213 |
user32.EnumWindows(EnumWindowsCallbackFunc, 0) |
| 214 |
|
| 215 |
def test_callback_register_int(self): |
| 216 |
# Issue #8275: buggy handling of callback args under Win64 |
| 217 |
# NOTE: should be run on release builds as well |
| 218 |
dll = CDLL(_ctypes_test.__file__) |
| 219 |
CALLBACK = CFUNCTYPE(c_int, c_int, c_int, c_int, c_int, c_int) |
| 220 |
# All this function does is call the callback with its args squared |
| 221 |
func = dll._testfunc_cbk_reg_int |
| 222 |
func.argtypes = (c_int, c_int, c_int, c_int, c_int, CALLBACK) |
| 223 |
func.restype = c_int |
| 224 |
|
| 225 |
def callback(a, b, c, d, e): |
| 226 |
return a + b + c + d + e |
| 227 |
|
| 228 |
result = func(2, 3, 4, 5, 6, CALLBACK(callback)) |
| 229 |
self.assertEqual(result, callback(2*2, 3*3, 4*4, 5*5, 6*6)) |
| 230 |
|
| 231 |
def test_callback_register_double(self): |
| 232 |
# Issue #8275: buggy handling of callback args under Win64 |
| 233 |
# NOTE: should be run on release builds as well |
| 234 |
dll = CDLL(_ctypes_test.__file__) |
| 235 |
CALLBACK = CFUNCTYPE(c_double, c_double, c_double, c_double, |
| 236 |
c_double, c_double) |
| 237 |
# All this function does is call the callback with its args squared |
| 238 |
func = dll._testfunc_cbk_reg_double |
| 239 |
func.argtypes = (c_double, c_double, c_double, |
| 240 |
c_double, c_double, CALLBACK) |
| 241 |
func.restype = c_double |
| 242 |
|
| 243 |
def callback(a, b, c, d, e): |
| 244 |
return a + b + c + d + e |
| 245 |
|
| 246 |
result = func(1.1, 2.2, 3.3, 4.4, 5.5, CALLBACK(callback)) |
| 247 |
self.assertEqual(result, |
| 248 |
callback(1.1*1.1, 2.2*2.2, 3.3*3.3, 4.4*4.4, 5.5*5.5)) |
| 249 |
|
| 250 |
def test_callback_large_struct(self): |
| 251 |
class Check: pass |
| 252 |
|
| 253 |
# This should mirror the structure in Modules/_ctypes/_ctypes_test.c |
| 254 |
class X(Structure): |
| 255 |
_fields_ = [ |
| 256 |
('first', c_ulong), |
| 257 |
('second', c_ulong), |
| 258 |
('third', c_ulong), |
| 259 |
] |
| 260 |
|
| 261 |
def callback(check, s): |
| 262 |
check.first = s.first |
| 263 |
check.second = s.second |
| 264 |
check.third = s.third |
| 265 |
# See issue #29565. |
| 266 |
# The structure should be passed by value, so |
| 267 |
# any changes to it should not be reflected in |
| 268 |
# the value passed |
| 269 |
s.first = s.second = s.third = 0x0badf00d |
| 270 |
|
| 271 |
check = Check() |
| 272 |
s = X() |
| 273 |
s.first = 0xdeadbeef |
| 274 |
s.second = 0xcafebabe |
| 275 |
s.third = 0x0bad1dea |
| 276 |
|
| 277 |
CALLBACK = CFUNCTYPE(None, X) |
| 278 |
dll = CDLL(_ctypes_test.__file__) |
| 279 |
func = dll._testfunc_cbk_large_struct |
| 280 |
func.argtypes = (X, CALLBACK) |
| 281 |
func.restype = None |
| 282 |
# the function just calls the callback with the passed structure |
| 283 |
func(s, CALLBACK(functools.partial(callback, check))) |
| 284 |
self.assertEqual(check.first, s.first) |
| 285 |
self.assertEqual(check.second, s.second) |
| 286 |
self.assertEqual(check.third, s.third) |
| 287 |
self.assertEqual(check.first, 0xdeadbeef) |
| 288 |
self.assertEqual(check.second, 0xcafebabe) |
| 289 |
self.assertEqual(check.third, 0x0bad1dea) |
| 290 |
# See issue #29565. |
| 291 |
# Ensure that the original struct is unchanged. |
| 292 |
self.assertEqual(s.first, check.first) |
| 293 |
self.assertEqual(s.second, check.second) |
| 294 |
self.assertEqual(s.third, check.third) |
| 295 |
|
| 296 |
def test_callback_too_many_args(self): |
| 297 |
def func(*args): |
| 298 |
return len(args) |
| 299 |
|
| 300 |
# valid call with nargs <= CTYPES_MAX_ARGCOUNT |
| 301 |
proto = CFUNCTYPE(c_int, *(c_int,) * CTYPES_MAX_ARGCOUNT) |
| 302 |
cb = proto(func) |
| 303 |
args1 = (1,) * CTYPES_MAX_ARGCOUNT |
| 304 |
self.assertEqual(cb(*args1), CTYPES_MAX_ARGCOUNT) |
| 305 |
|
| 306 |
# invalid call with nargs > CTYPES_MAX_ARGCOUNT |
| 307 |
args2 = (1,) * (CTYPES_MAX_ARGCOUNT + 1) |
| 308 |
with self.assertRaises(ArgumentError): |
| 309 |
cb(*args2) |
| 310 |
|
| 311 |
# error when creating the type with too many arguments |
| 312 |
with self.assertRaises(ArgumentError): |
| 313 |
CFUNCTYPE(c_int, *(c_int,) * (CTYPES_MAX_ARGCOUNT + 1)) |
| 314 |
|
| 315 |
def test_convert_result_error(self): |
| 316 |
def func(): |
| 317 |
return ("tuple",) |
| 318 |
|
| 319 |
proto = CFUNCTYPE(c_int) |
| 320 |
ctypes_func = proto(func) |
| 321 |
with support.catch_unraisable_exception() as cm: |
| 322 |
# don't test the result since it is an uninitialized value |
| 323 |
result = ctypes_func() |
| 324 |
|
| 325 |
self.assertIsInstance(cm.unraisable.exc_value, TypeError) |
| 326 |
self.assertEqual(cm.unraisable.err_msg, |
| 327 |
f"Exception ignored while converting result " |
| 328 |
f"of ctypes callback function {func!r}") |
| 329 |
self.assertIsNone(cm.unraisable.object) |
| 330 |
|
| 331 |
|
| 332 |
if __name__ == '__main__': |
| 333 |
unittest.main() |