classification
Title: Document that ctypes.xFUNCTYPE are decorators.
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: LambertDW, adelfino, amaury.forgeotdarc, belopolsky, berker.peksag, georg.brandl, kevinwatters, meador.inge, miss-islington, theller
Priority: normal Keywords: easy, patch

Created on 2008-11-05 05:51 by LambertDW, last changed 2018-07-13 14:05 by berker.peksag. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 7924 merged adelfino, 2018-06-26 02:15
PR 8272 merged miss-islington, 2018-07-13 12:50
PR 8273 merged miss-islington, 2018-07-13 12:51
Messages (8)
msg75515 - (view) Author: David W. Lambert (LambertDW) Date: 2008-11-05 05:51
http://docs.python.org/dev/3.0/library/ctypes.html#callback-functions

ctypes.xFUNCTYPE are another opportunity to advertise decorators.  
Please consider inserting yet another qsort example written as a 
decorator, perhaps as follows.  Or---it could be that I'm slow and the 
average pythonista will figure this out on first read.


@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)
msg75535 - (view) Author: Kevin Watters (kevinwatters) Date: 2008-11-05 23:12
As far as I know, the above code will fail randomly in release mode if 
you have multiple threads running Python, because when ctypes calls out 
to CFUNCTYPE functions, it releases the GIL.

For decorating a python function to give to qsort, maybe you can use 
PYFUNCTYPE?
msg75577 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2008-11-06 18:48
[David Lambert]
> > @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)

[Kevin Watters]
> > As far as I know, the above code will fail randomly in release mode if 
> > you have multiple threads running Python, because when ctypes calls out 
> > to CFUNCTYPE functions, it releases the GIL.
> > 
> > For decorating a python function to give to qsort, maybe you can use 
> > PYFUNCTYPE?

Kevin is wrong - the code is perfect!  Sure does calling a CFUNCTYPE instance
release the GIL, but ctypes aquires the GIL again before executing the python
code in the wrapped py_cmp_func above.

I guess that using PYFUNCTYPE instead would possibly crash because the call to
'qsort' releases the GIL, and the PYFUNCTYPE instance assumes the GIL but does not
acquire it.

But I have no time to work on the docs, sorry.
msg320464 - (view) Author: Andrés Delfino (adelfino) * Date: 2018-06-26 02:16
I suspect we'll be working on this PR a little, but a least it's a start to get this moving.
msg321601 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2018-07-13 12:50
New changeset 379e9d639a52766f79c7a206c5096c8333d1896f by Berker Peksag (Andrés Delfino) in branch 'master':
bpo-4260: Document that ctypes.xFUNCTYPE are decorators (GH-7924)
https://github.com/python/cpython/commit/379e9d639a52766f79c7a206c5096c8333d1896f
msg321610 - (view) Author: miss-islington (miss-islington) Date: 2018-07-13 13:35
New changeset 08c1da71030e0d6019b3493e0ffaf060a6aa5d8e by Miss Islington (bot) in branch '3.7':
bpo-4260: Document that ctypes.xFUNCTYPE are decorators (GH-7924)
https://github.com/python/cpython/commit/08c1da71030e0d6019b3493e0ffaf060a6aa5d8e
msg321612 - (view) Author: miss-islington (miss-islington) Date: 2018-07-13 13:54
New changeset 083a836937b734fcc81b2482d2c52825caff0115 by Miss Islington (bot) in branch '3.6':
bpo-4260: Document that ctypes.xFUNCTYPE are decorators (GH-7924)
https://github.com/python/cpython/commit/083a836937b734fcc81b2482d2c52825caff0115
msg321614 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2018-07-13 14:05
I think we can now close this one. Thanks for the report, David, and thanks for the PR Andreas.

(And thanks for creating and maintaining this awesome module for years, Thomas :))
History
Date User Action Args
2018-07-13 14:05:29berker.peksagsetstatus: open -> closed
versions: + Python 3.6
messages: + msg321614

resolution: fixed
stage: patch review -> resolved
2018-07-13 13:54:23miss-islingtonsetmessages: + msg321612
2018-07-13 13:35:34miss-islingtonsetnosy: + miss-islington
messages: + msg321610
2018-07-13 12:51:35miss-islingtonsetpull_requests: + pull_request7808
2018-07-13 12:50:47miss-islingtonsetpull_requests: + pull_request7807
2018-07-13 12:50:23berker.peksagsetmessages: + msg321601
2018-07-10 21:25:35adelfinosetnosy: + amaury.forgeotdarc, belopolsky, meador.inge
2018-06-26 02:16:41adelfinosetnosy: + adelfino
messages: + msg320464
2018-06-26 02:15:09adelfinosetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request7532
2018-02-25 00:00:02cheryl.sabellasetversions: + Python 3.7, Python 3.8, - Python 3.5, Python 3.6
2016-05-07 14:41:28ppperrysettitle: ctypes.xFUNCTYPE are decorators. -> Document that ctypes.xFUNCTYPE are decorators.
2016-04-19 16:54:02berker.peksagsetkeywords: + easy
nosy: + berker.peksag
stage: needs patch

versions: + Python 3.5, Python 3.6, - Python 3.0
2010-10-29 10:07:21adminsetassignee: georg.brandl -> docs@python
2008-11-06 18:48:31thellersetnosy: + theller
messages: + msg75577
2008-11-05 23:12:11kevinwatterssetnosy: + kevinwatters
messages: + msg75535
2008-11-05 05:51:03LambertDWcreate