ctypes and setjmp
Richard Jones
richardjones at optushome.com.au
Fri Oct 6 23:19:34 EDT 2006
Gabriel Genellina wrote:
> At Friday 6/10/2006 16:14, Thomas Heller wrote:
>>For ctypes, the only solution I can think of is to invent a new calling
>>convention, which will call setjmp() first internally before calling the
>>libpng api function...
>
> May be reasonable - a non-zero in setjmp would raise a Python
> exception. But perhaps a new calling convention is not needed: as you
> need a way to indicate *which* argument is the jmp_buf, just a new
> argument type would suffice. If you encounter one such thing in the
> function.argtypes list, that means a setjmp is needed before calling
> the actual function.
The ctypes C code which handles invoking the foreign function call must be
responsible for calling setjmp(). You can't call that in Python code - the
C stack is manipulated all over the place for each line of Python code
executed. You have to both call and set the jmp_buf in the same C code call
from Python.
Thus my suggestion of configuring a handler function on the foreign function
wrapper object alongside the argtypes and restype. If one is defined then
ctypes automatically calls setjmp and passes control to that handler
function if setjmp returns non-zero.
This page:
http://www.massey.ac.nz/~mgwalker/misc/create-images.html
has some example programs that show setjmp/longjmp in action. Read the whole
thing as his first examples aren't correct :)
Richard
More information about the Python-list
mailing list