472,353 Members | 1,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

ctypes and setjmp

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

Oct 5 '06 #1
4 1937
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.

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? What do you do
when setjmp() returns != 0? Exit the program? Log a message? How do you
determine which call failed? How do you pass the jmp_buf that you passed to
setjmp() to the api call?

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

Thomas

Oct 6 '06 #2
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

Oct 6 '06 #3
At Friday 6/10/2006 16:14, Thomas Heller wrote:
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.
I didn't know that setjmp/longjmp is actually used by production libraries
for error handling.
Using setjmp to report errors in a general library is, uhm, a bit
crazy at least!
C programmers are certainly crazy people :)
>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? What do you do
when setjmp() returns != 0? Exit the program? Log a message? How do you
determine which call failed? How do you pass the jmp_buf that you passed to
setjmp() to the api call?
The setjmp must be on scope when a longjmp is called. Since there is
no endjmp/canceljmp, this in the practice means that you have to call
setjmp in *every* function that calls another one which may issue a longjmp.
There are other alternatives in plain C (cexcept:
http://cexcept.sourceforge.net/) but that wont help you with ctypes.
>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.

Gabriel Genellina
Softlab SRL

__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Oct 7 '06 #4
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/mi...te-images.html

has some example programs that show setjmp/longjmp in action. Read the whole
thing as his first examples aren't correct :)
Richard

Oct 7 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomas Baruchel | last post by:
Hi, wondering about: func1: setjmp() ; func2(); func2: {FILE *f; f = fopen(); func3(); fclose(f)} func3 : if() longjmp; else return; Note...
4
by: Jrferguson | last post by:
I have a C program that I am trying to port to a Motorola 68k based system. It makes use of setjmp and longjmp which are not supported by my C...
18
by: Peter Smithson | last post by:
Hi, I've read this page - http://devrsrc1.external.hp.com/STK/impacts/i634.html but don't understand it. Here's the text - "Non-standard...
5
by: candy | last post by:
hi all, Consider the following C code: void funct(){ }
20
by: JS | last post by:
When setjmp is called how can the if statement evaluate to true or false when setjmp only returns 0 or non-zero? struct pcb { void...
8
by: Zheng Da | last post by:
I wrote a simple one as follow: typedef struct __myjmp_buf { int efp; int epc; }myjmp_buf; int mysetjmp(myjmp_buf env) {
4
by: Sreekanth | last post by:
Hi All I am trying to write a code in which I have to access an array of jmp_buf so i have declared it as jmp_buf mybuf Now when i am doing...
15
by: rover8898 | last post by:
Hello all, I used setjmp() in a recent of program of mine (it is not completed, so I have not the chance to test it out yet). I am not very...
5
by: Spiros Bousbouras | last post by:
In the following assume that a is int and env is of type jmp_buf a = setjmp(env) ; Is it legal ? It looks reasonable but it seems to violate...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.