|
8 | 8 | from _ctypes import CTYPES_MAX_ARGCOUNT |
9 | 9 | from ctypes import (CDLL, cdll, Structure, Union, CFUNCTYPE, |
10 | 10 | ArgumentError, POINTER, sizeof, |
11 | | - c_byte, c_ubyte, c_char, c_char_p, |
12 | | - c_short, c_ushort, c_int, c_uint, |
| 11 | + c_byte, c_ubyte, c_char, c_char_p, c_void_p, |
| 12 | + c_short, c_ushort, c_int, c_uint, c_size_t, |
13 | 13 | c_long, c_longlong, c_ulonglong, c_ulong, |
14 | 14 | c_float, c_double, c_longdouble, py_object) |
15 | 15 | from ctypes.util import find_library, struct, wrap_dll_function |
@@ -197,6 +197,81 @@ def cmp_func(a, b): |
197 | 197 | libc.qsort(array, len(array), sizeof(c_int), cmp_func) |
198 | 198 | self.assertEqual(array[:], [1, 5, 7, 33, 99]) |
199 | 199 |
|
| 200 | + def test_mismatched_callback_prototype(self): |
| 201 | + # gh-125788: CFUNCTYPE and WINFUNCTYPE prototypes are distinct |
| 202 | + # classes that share one type name, so a mismatch used to raise |
| 203 | + # "expected CFunctionType instance instead of CFunctionType". |
| 204 | + # Reproducer from the issue: qsort's comparator prototype does |
| 205 | + # not match the callback that is passed. Where c_int is an alias |
| 206 | + # of c_long, those two prototypes are the same object; a shorter |
| 207 | + # argument list keeps them distinct. |
| 208 | + def signature(proto): |
| 209 | + return (f"(restype={proto._restype_!r}, " |
| 210 | + f"argtypes={proto._argtypes_!r})") |
| 211 | + |
| 212 | + def message(expected, actual): |
| 213 | + return ( |
| 214 | + f"expected {expected.__name__} instance with signature " |
| 215 | + f"{signature(expected)} instead of " |
| 216 | + f"{actual.__name__} instance with signature " |
| 217 | + f"{signature(actual)}" |
| 218 | + ) |
| 219 | + |
| 220 | + def cmp(*args): |
| 221 | + return 0 |
| 222 | + |
| 223 | + cmp_int = CFUNCTYPE(c_int, c_void_p, c_void_p) |
| 224 | + cmp_long = CFUNCTYPE(c_long, c_void_p, c_void_p) |
| 225 | + if cmp_int is cmp_long: |
| 226 | + cmp_long = CFUNCTYPE(c_long, c_void_p) |
| 227 | + |
| 228 | + long_cb = cmp_long(cmp) |
| 229 | + mismatch = message(cmp_int, cmp_long) |
| 230 | + with self.assertRaises(TypeError) as cm: |
| 231 | + cmp_int.from_param(long_cb) |
| 232 | + self.assertEqual(str(cm.exception), mismatch) |
| 233 | + |
| 234 | + # A matching callback is still accepted unchanged. |
| 235 | + good = cmp_int(cmp) |
| 236 | + self.assertIs(cmp_int.from_param(good), good) |
| 237 | + |
| 238 | + # Values that are not callbacks keep the generic message. |
| 239 | + with self.assertRaises(TypeError) as cm: |
| 240 | + cmp_int.from_param(object()) |
| 241 | + self.assertEqual(str(cm.exception), |
| 242 | + "expected CFunctionType instance instead of object") |
| 243 | + |
| 244 | + # Argument types differ, so each side of the message is checked |
| 245 | + # independently of the result type. |
| 246 | + cmp_chars = CFUNCTYPE(c_int, c_char_p) |
| 247 | + with self.assertRaises(TypeError) as cm: |
| 248 | + cmp_int.from_param(cmp_chars(cmp)) |
| 249 | + self.assertEqual(str(cm.exception), message(cmp_int, cmp_chars)) |
| 250 | + |
| 251 | + if hasattr(ctypes, 'WINFUNCTYPE'): |
| 252 | + win_int = ctypes.WINFUNCTYPE(c_int, c_void_p, c_void_p) |
| 253 | + win_long = ctypes.WINFUNCTYPE(c_long, c_void_p, c_void_p) |
| 254 | + if win_int is win_long: |
| 255 | + win_long = ctypes.WINFUNCTYPE(c_long, c_void_p) |
| 256 | + with self.assertRaises(TypeError) as cm: |
| 257 | + win_int.from_param(win_long(cmp)) |
| 258 | + self.assertEqual(str(cm.exception), message(win_int, win_long)) |
| 259 | + |
| 260 | + libc_path = find_library("c") |
| 261 | + if not libc_path: |
| 262 | + return |
| 263 | + libc = CDLL(libc_path) |
| 264 | + try: |
| 265 | + qsort = libc.qsort |
| 266 | + except (AttributeError, OSError, ValueError): |
| 267 | + return |
| 268 | + qsort.restype = None |
| 269 | + qsort.argtypes = [c_void_p, c_size_t, c_size_t, cmp_int] |
| 270 | + with self.assertRaises(ArgumentError) as cm: |
| 271 | + qsort(None, 0, sizeof(c_int), long_cb) |
| 272 | + self.assertEqual(str(cm.exception), |
| 273 | + f"argument 4: TypeError: {mismatch}") |
| 274 | + |
200 | 275 | @unittest.skipUnless(hasattr(ctypes, 'WINFUNCTYPE'), |
201 | 276 | 'ctypes.WINFUNCTYPE is required') |
202 | 277 | def test_issue_8959_b(self): |
|
0 commit comments