473,769 Members | 5,834 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2075
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.argtyp es 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.argtyp es 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
2296
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 that FILE *fis a local variable in func2.
4
2126
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 compiler. I understand the general principle behind setjmp/longjmp, but I am somewhat lost in the detail of how to actually implement this in assembler. (My compiler supports in-line assembler which I hope will prove usefull). I will be very gratefull for any help or pointers with this problem. I am...
18
2449
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 usage of setjmp() and longjmp() could result in compatibility problems. The contents of the jmp_buf buffer are specific
5
1874
by: candy | last post by:
hi all, Consider the following C code: void funct(){ }
20
2321
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 *(*start_routine) (void *); void *arg; jmp_buf state; int stack; };
8
2419
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
7563
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 a longjmp like
15
2432
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 profocient in C coding as are some of my co-workers. They (my co-workers) say (with vehement ardor ;) ) that the usage of setjmp() emplyoyed in function"C" that was called from function "B" that was called from function "A" that was called form the main(), will cause havoc in the stack. And it makes...
5
1616
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 what is mentioned under "Environmental limits" of 7.13.1.1 of n869 so my guess is that it's not legal. If it's not legal could someone give me some insight
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9849
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6661
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5433
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3948
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.