Catching Python exceptions in C

Gabriel Genellina gagsl-py2 at
Tue Dec 9 07:51:42 CET 2008

En Mon, 08 Dec 2008 16:42:30 -0200, Senthil Kumar <senthilkas at>

> 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

More information about the Python-list mailing list