-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1025,6 +1025,23 @@ 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(*args): | ||
... (a, b,) = (t[0] for t in args) | ||
... print("py_cmp_func", a, b) | ||
... return a - b | ||
... | ||
>>> 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 | ||
|
@@ -1582,7 +1599,7 @@ type and the argument types of the function. | |
|
||
.. function:: CFUNCTYPE(restype, *argtypes, use_errno=False, use_last_error=False) | ||
|
||
The returned function prototype creates functions that use the standard C | ||
Returns a function prototype which creates functions that use the standard C | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't make changes irrelevant to bpo-4260. |
||
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 | ||
|
@@ -1592,7 +1609,7 @@ type and the argument types of the function. | |
|
||
.. function:: WINFUNCTYPE(restype, *argtypes, use_errno=False, use_last_error=False) | ||
|
||
Windows only: The returned function prototype creates functions that use the | ||
Windows only: Returns a function prototype which creates functions that use the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't make changes irrelevant to bpo-4260. |
||
``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 | ||
|
@@ -1601,9 +1618,12 @@ type and the argument types of the function. | |
|
||
.. function:: PYFUNCTYPE(restype, *argtypes) | ||
|
||
The returned function prototype creates functions that use the Python calling | ||
Returns a function prototype which creates functions that use the Python calling | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't make changes irrelevant to bpo-4260. |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can move this information to the first paragraph of the "Function prototypes" section to make it more discoverable. Also, we can add a link to the examples from that paragraph. |
||
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: | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why didn't you reuse the
py_cmp_func
function in line 1008? IMO, the following snippet should work:There is even a test for that same function:
cpython/Lib/ctypes/test/test_callbacks.py
Lines 186 to 188 in b91a3a0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I followed the proposal from David W. Lambert in the issue, but you are right, I tested the decorator with the function from line 1008 and works as well.