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

Alex Martelli aleaxit at yahoo.com
Tue Feb 27 02:03:27 EST 2001


"Edward C. Jones" <edcjones at erols.com> wrote in message
news:3A9B3B7D.66E31386 at erols.com...
> 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.
>
> int error_callback(int status, char * funcname) {
>     if (status != 0) {
>         printf("Error occured in %s\n", funcname);
>         /* Is this part of what I want: */
>         PyErr_SetString(PyExc_Exception, "Error in C function");
>         /* ... what goes here ... */
>     }
>     return 0;
> }

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






More information about the Python-list mailing list