How to exit Python nicely after error in embedded C code?

Louis Luangkesorn lluang at
Tue Feb 27 09:43:52 EST 2001

(I'm asking this out of complete ignorance, obviously)
In this situation (error occurs in a C function that is called by other C
functions which are called by Python), would having the original C call from
Python in a try...except block catch the exception?  (I'm just learning how to
do this, in both C++ and Python, but I have not done this in real life yet.  It
seems like a nice, cheap way of using exceptions in C, just call everything from
Python :-)


Alex Martelli wrote:

> "Edward C. Jones" <edcjones at> wrote in message
> news:3A9B3B7D.66E31386 at
> > Suppose I have a C function that is called only by other C functions (so
> it
> > doesn't return a PyObject *). If I find an error condition in the the C
> > function, is there function I can call that will exit the C code back into
> > Python so Python can clean up and exit. The function is called by many
> different
> > C function in case of error.
> >
> <sample code deleted>

> In C, you can portably achieve this effect only by having this
> function return or set an error indicator and having every
> caller test it (there is a mechanism called setjmp/longjmp,
> but it may not work as desired everywhere).  In C++, it's
> easy, of course -- that's what exceptions are for.
> Alex

K Louis Luangkesorn
lluang at  PGP:0xF3E2D362
Whatsoever things are true, ... honest, ... just, ... pure, ... lovely, ... of
good report; if there be any virtue, and if there be any praise, think on these
things.  - motto - Northwestern University

More information about the Python-list mailing list