This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author theller
Recipients LambertDW, georg.brandl, kevinwatters, theller
Date 2008-11-06.18:48:30
SpamBayes Score 5.500407e-09
Marked as misclassified No
Message-id <49133BFD.9070101@ctypes.org>
In-reply-to <1225926731.76.0.649556386111.issue4260@psf.upfronthosting.co.za>
Content
[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.
History
Date User Action Args
2008-11-06 18:48:31thellersetrecipients: + theller, georg.brandl, kevinwatters, LambertDW
2008-11-06 18:48:30thellerlinkissue4260 messages
2008-11-06 18:48:30thellercreate