Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.6] bpo-4260: Document that ctypes.xFUNCTYPE are decorators (GH-7924) #8273

Merged
merged 1 commit into from
Jul 13, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion Doc/library/ctypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,22 @@ As we can easily check, our array is sorted now::
1 5 7 33 99
>>>

The function factories can be used as decorator factories, so we may as well
write::

>>> @CFUNCTYPE(c_int, POINTER(c_int), POINTER(c_int))
... 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), py_cmp_func)
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
>>>

.. note::

Make sure you keep references to :func:`CFUNCTYPE` objects as long as they
Expand Down Expand Up @@ -1575,7 +1591,9 @@ Foreign functions can also be created by instantiating function prototypes.
Function prototypes are similar to function prototypes in C; they describe a
function (return type, argument types, calling convention) without defining an
implementation. The factory functions must be called with the desired result
type and the argument types of the function.
type and the argument types of the function, and can be used as decorator
factories, and as such, be applied to functions through the ``@wrapper`` syntax.
See :ref:`ctypes-callback-functions` for examples.


.. function:: CFUNCTYPE(restype, *argtypes, use_errno=False, use_last_error=False)
Expand Down