Catching Python exceptions in C


S

Senthil Kumar

Hi Pythoneers !
Can somebody give a quick solution?
I am trying to raise exceptions in python and trying to handle it in
C.
I am able to raise exceptions successfully. However could not catch
those in C.
I am using the following function to run the python from C:
Pyrun_SimpleString().
After the exception is raised, I am checking PyErr_Occurred(). It
always returns NULL, and I cannot catch the exception.
Pls help me a way out.

Thanx in advance !
 
Ad

Advertisements

B

Benjamin

Hi Pythoneers !
Can somebody give a quick solution?
I am trying to raise exceptions in python and trying to handle it in
C.
I am able to raise exceptions successfully. However could not catch
those in C.
I am using the following function to run the python from C:
Pyrun_SimpleString().
After the exception is raised, I am checking PyErr_Occurred(). It
always returns NULL, and I cannot catch the exception.
Pls help me a way out.

If a function returns NULL, you can use PyErr_ExceptionMatches to
check if it is the exception you want to catch.
 
G

Gabriel Genellina

I am trying to raise exceptions in python and trying to handle it in
C.
I am able to raise exceptions successfully. However could not catch
those in C.
I am using the following function to run the python from C:
Pyrun_SimpleString().
After the exception is raised, I am checking PyErr_Occurred(). It
always returns NULL, and I cannot catch the exception.
Pls help me a way out.

From the documentation:
"...Returns 0 on success or -1 if an exception was raised. If there was an
error, there is no way to get the exception information."

Try using PyRun_String instead, or write your own based on pythonrun.c
 
I

Ivan Illarionov

Hi Pythoneers !
Can somebody give a quick solution?
I am trying to raise exceptions in python and trying to handle it in
C.
I am able to raise exceptions successfully. However could not catch
those in C.
I am using the following function to run the python from C:
Pyrun_SimpleString().
After the exception is raised, I am checking PyErr_Occurred(). It
always returns NULL, and I cannot catch the exception.
Pls help me a way out.

Thanx in advance !

I use something like this:

if (PyErr_Occurred()) {
if (PyErr_ExceptionMatches(ExceptionName)) {
/* do something about ExceptionName */
} else if (PyErr_ExceptionMatches(AnotherExceptionName)) {
/* if we want to get the exception string */
PyObject *errtype, *errvalue, *traceback;
PyErr_Fetch(&errtype, &errvalue, &traceback);
if(errvalue != NULL) {
PyObject *s = PyObject_Str(errvalue);
/* Now 'PyString_AS_STRING(s)'
contains C string of error message
do something with it
*/
Py_DECREF(s);
}
Py_XDECREF(errvalue);
Py_XDECREF(errtype);
Py_XDECREF(traceback);
}
}

Goof luck.
 
Ad

Advertisements

I

Ivan Illarionov

I use something like this:

if (PyErr_Occurred()) {
    if (PyErr_ExceptionMatches(ExceptionName)) {
        /* do something about ExceptionName */
    } else if (PyErr_ExceptionMatches(AnotherExceptionName)) {
    /* if we want to get the exception string */
        PyObject *errtype, *errvalue, *traceback;
        PyErr_Fetch(&errtype, &errvalue, &traceback);
        if(errvalue != NULL) {
             PyObject *s = PyObject_Str(errvalue);
             /*    Now 'PyString_AS_STRING(s)'
              contains C string of error message
              do something with it
             */
             Py_DECREF(s);
        }
        Py_XDECREF(errvalue);
        Py_XDECREF(errtype);
        Py_XDECREF(traceback);
    }

}

Goof luck.

And, as others have said, use 'PyRun_String', not 'PyRun_SimpleString'
Sorry, I missed this in previous reply.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top