Catching Python exceptions in C

Discussion in 'Python' started by Senthil Kumar, Dec 8, 2008.

  1. 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 !
     
    Senthil Kumar, Dec 8, 2008
    #1
    1. Advertising

  2. Senthil Kumar

    Benjamin Guest

    On Dec 8, 12:42 pm, Senthil Kumar <> wrote:
    > 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.
    >
    > Thanx in advance !
     
    Benjamin, Dec 9, 2008
    #2
    1. Advertising

  3. En Mon, 08 Dec 2008 16:42:30 -0200, Senthil Kumar <>
    escribió:

    > 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

    --
    Gabriel Genellina
     
    Gabriel Genellina, Dec 9, 2008
    #3
  4. On Dec 8, 9:42 pm, Senthil Kumar <> wrote:
    > 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.
     
    Ivan Illarionov, Dec 9, 2008
    #4
  5. On Dec 9, 12:33 pm, Ivan Illarionov <> wrote:
    > On Dec 8, 9:42 pm, Senthil Kumar <> wrote:
    >
    > > 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.


    And, as others have said, use 'PyRun_String', not 'PyRun_SimpleString'
    Sorry, I missed this in previous reply.
     
    Ivan Illarionov, Dec 9, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marina
    Replies:
    2
    Views:
    494
    Marina
    Jul 8, 2003
  2. Amil Hanish
    Replies:
    0
    Views:
    559
    Amil Hanish
    Apr 13, 2006
  3. Adam Maass
    Replies:
    5
    Views:
    416
    Sudsy
    Jul 22, 2003
  4. Mike Schilling
    Replies:
    2
    Views:
    355
    Mike Schilling
    Jul 16, 2003
  5. Steven D'Aprano

    Catching exceptions from Python 2.4 to 3.x

    Steven D'Aprano, Nov 17, 2012, in forum: Python
    Replies:
    2
    Views:
    220
    Steven D'Aprano
    Nov 17, 2012
Loading...

Share This Page