Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Catching Python exceptions in C

Reply
Thread Tools

Catching Python exceptions in C

 
 
Senthil Kumar
Guest
Posts: n/a
 
      12-08-2008
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 !
 
Reply With Quote
 
 
 
 
Benjamin
Guest
Posts: n/a
 
      12-09-2008
On Dec 8, 12:42*pm, Senthil Kumar <(E-Mail Removed)> 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 !


 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      12-09-2008
En Mon, 08 Dec 2008 16:42:30 -0200, Senthil Kumar <(E-Mail Removed)>
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

 
Reply With Quote
 
Ivan Illarionov
Guest
Posts: n/a
 
      12-09-2008
On Dec 8, 9:42*pm, Senthil Kumar <(E-Mail Removed)> 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.
 
Reply With Quote
 
Ivan Illarionov
Guest
Posts: n/a
 
      12-09-2008
On Dec 9, 12:33*pm, Ivan Illarionov <(E-Mail Removed)> wrote:
> On Dec 8, 9:42*pm, Senthil Kumar <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Catching exceptions from Python 2.4 to 3.x Steven D'Aprano Python 2 11-17-2012 03:53 PM
Catching unhandled exceptions using HttpModules Amil Hanish ASP .Net 0 04-12-2006 11:51 PM
Re: Catching exceptions that are never thrown Adam Maass Java 5 07-22-2003 10:58 PM
Re: Catching exceptions that are never thrown Mike Schilling Java 2 07-16-2003 08:36 PM
Re: catching exceptions from web user controls Marina ASP .Net 2 07-08-2003 04:48 PM



Advertisments