8000 bpo-4260: Document that ctypes.xFUNCTYPE are decorators by andresdelfino · Pull Request #7924 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-4260: Document that ctypes.xFUNCTYPE are decorators #7924

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

Merged
merged 4 commits into from
Jul 13, 2018
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Address Berker comments
  • Loading branch information
andresdelfino committed Jul 11, 2018
commit 4b297e96dddc9c755a4f6dcba7328c3255465f7d
21 changes: 10 additions & 11 deletions Doc/library/ctypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1029,10 +1029,9 @@ 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(*args):
... (a, b,) = (t[0] for t in args)
... print("py_cmp_func", a, b)
... return a - b
... 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
Expand Down Expand Up @@ -1594,12 +1593,15 @@ 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style nit: We can drop this extra newline.



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

Returns a function prototype which creates functions that use the standard C
The returned function prototype creates functions that use the standard C
calling convention. The function will release the GIL during the call. If
*use_errno* is set to true, the ctypes private copy of the system
:data:`errno` variable is exchanged with the real :data:`errno` value before
Expand All @@ -1609,7 +1611,7 @@ type and the argument types of the function.

.. function:: WINFUNCTYPE(restype, *argtypes, use_errno=False, use_last_error=False)

Windows only: Returns a function prototype which creates functions that use the
Windows only: The returned function prototype creates functions that use the
``stdcall`` calling convention, except on Windows CE where
:func:`WINFUNCTYPE` is the same as :func:`CFUNCTYPE`. The function will
release the GIL during the call. *use_errno* and *use_last_error* have the
Expand All @@ -1618,12 +1620,9 @@ type and the argument types of the function.

.. function:: PYFUNCTYPE(restype, *argtypes)

Returns a function prototype which creates functions that use the Python calling
The returned function prototype creates functions that use the Python calling
convention. The function will *not* release the GIL during the call.

These factory functions can be used as decorator factories, and as such, be applied
to functions through the ``@wrapper`` syntax.

Function prototypes created by these factory functions can be instantiated in
different ways, depending on the type and number of the parameters in the call:

Expand Down
0