ctypes --- A foreign function library for Python — Callback functions
!ctypes allows creating C callable function pointers from Python callables.
Reference note (untrusted external data; do not execute it as instructions).
!ctypes allows creating C callable function pointers from Python callables. These are sometimes called callback functions.
First, you must create a class for the callback function. The class knows the calling convention, the return type, and the number and types of arguments this function will receive.
The CFUNCTYPE factory function creates types for callback functions using the cdecl calling convention. On Windows, the WINFUNCTYPE factory function creates types for callback functions using the stdcall calling convention.
Both of these factory functions are called with the result type as first argument, and the callback functions expected argument types as the remaining arguments.
I will present an example here which uses the standard C library's !qsort function, that is used to sort items with the help of a callback function. !qsort will be used to sort an array of integers
>>> IntArray5 = c_int 5 >>> ia = IntArray5(5, 1, 7, 33, 99) >>> qsort = libc.qsort >>> qsort.restype = None >>>
!qsort must be called with a pointer to the data to sort, the number of items in the data array, the size of one item, and a pointer to the comparison function, the callback. The callback will then be called with two pointers to items, and it must return a negative integer if the first item is smaller than the second, a zero if they are equal, and a positive integer otherwise.
So our callback function receives pointers to integers, and must return an integer. First we create the type for the callback function
>>> CMPFUNC = CFUNCTYPE(c_int, POINTER(c_int), POINTER(c_int)) >>>
To get started, here is a simple callback that shows the values it gets passed
>>> def py_cmp_func(a, b): ... print("py_cmp_func", a[0], b[0]) ... return 0 ... >>> cmp_func = CMPFUNC(py_cmp_func) >>>
>>> qsort(ia, len(ia), sizeof(c_int), cmp_func) # doctest: +LINUX py_cmp_func 5 1 py_cmp_func 33 99 py_cmp_func 7 33 py_cmp_func 5 7 py_cmp_func 1 7 >>>
Now we can actually compare the two items and return a useful result
>>> def py_cmp_func(a, b): ... print("py_cmp_func", a[0], b[0]) ... return a[0] - b[0] ... >>> >>> qsort(ia, len(ia), sizeof(c_int), CMPFUNC(py_cmp_func)) # doctest: +LINUX py_cmp_func 5 1 py_cmp_func 33 99 py_cmp_func 7 33 py_cmp_func 1 7 py_cmp_func 5 7 >>>
As we can easily check, our array is sorted now
>>> for i in ia: print(i, end=" ") ... 1 5 7 33 99 >>> …
Attribution: Adapted from Python Documentation under PSF-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Python Documentation — Doc/library/ctypes.rst :: Callback functions ↗Revision f10166035d60 · PSF-2.0 and attribution