ctypes and setjmp

Richard Jones richardjones at optushome.com.au
Fri Oct 6 19:34:45 EDT 2006


Thomas Heller wrote:

> Richard Jones schrieb:
>> Currently ctypes can't play well with any C code that requires use of
>> setjmp as part of its API.
>> 
>> libpng is one of those libraries.
>> 
>> Can anyone think of a reasonable solution to this? Perhaps ctypes might
>> be patched to offer setjmp support in foreign function definitions?
>> 
>> 
>>     Richard
>> 
> I didn't know that setjmp/longjmp is actually used by production libraries
> for error handling.

To be honest, I didn't even know what setjmp/longjmp *were* until I hit its
use in libpng, and I've been programming C for over a decade now ;)

> How is this pattern used in C?  Do you call setjump() before each api
> call, or do you call setjump once, and then do all the api calls?

I believe you call setjmp just before calling the API function in question.


> What do you do when setjmp() returns != 0?

I'd say that in ctypes land you'd have to provide a function to call in that
situation. ctypes would call that function and then continue on. It's up to
that function to raise an exception or otherwise flag the error. So in
rough terms, you'd have ctypes do:

  if (error_handler != NULL) r = 0;
  else r = setjmp();
  if (r == 0) api_call();
  if (r == 1) error_handler();


> 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...

Yeah, that's what I figured.


     Richard




More information about the Python-list mailing list