By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,282 Members | 1,169 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,282 IT Pros & Developers. It's quick & easy.

Catching SystemExit in C API code when embedding Python?

P: n/a
Hi all!

I am embedding Python into a GUI application in a way that the GUI is
scriptable using Python.

Now I have come to a problem that when the user puts a "sys.exit(0)"
into his script to end the script, not only the script is terminated,
but also the GUI application itself. This is not the intended behaviour.

As in Python itself you can catch SystemExit, I think this should be
the way to go. But how do I catch this exception from within the C API?

Thanks in advance for any hints.

--
Stefan Bellon
Aug 2 '07 #1
Share this Question
Share on Google+
5 Replies

P: n/a
Stefan Bellon wrote:
Hi all!

I am embedding Python into a GUI application in a way that the GUI is
scriptable using Python.

Now I have come to a problem that when the user puts a "sys.exit(0)"
into his script to end the script, not only the script is terminated,
but also the GUI application itself. This is not the intended behaviour.

As in Python itself you can catch SystemExit, I think this should be
the way to go. But how do I catch this exception from within the C API?

Thanks in advance for any hints.
Have a look at the following doc page for handling exceptions with the C
api:

http://docs.python.org/api/exceptionHandling.html

Also, here is some sample code that will catch system exit exceptions:

//Call python code
....
//Check for system exit exception
if(PyErr_Occurred()) {
if(PyErr_ExceptionMatches(PyExc_SystemExit)) {
//handle system exit
PyErr_Clear();
} else {
PyErr_Print();
}
}
Aug 2 '07 #2

P: n/a
First of all, I'm sorry to followup my own posting, but I can add a few
things ...

On Thu, 02 Aug, Stefan Bellon wrote:
As in Python itself you can catch SystemExit, I think this should be
the way to go. But how do I catch this exception from within the C
API?
I now installed an exception hook in sys.excepthook on the C side. And
indeed, it gets called whenever an exception is raised ... but not when
SystemExit is raised. And indeed, in PyErr_PrintEx in pythonrun.c I
found the following:

hook = PySys_GetObject("excepthook");
if (hook) {
PyObject *args = PyTuple_Pack(3,
exception, v ? v : Py_None, tb ? tb : Py_None);
PyObject *result = PyEval_CallObject(hook, args);
if (result == NULL) {
PyObject *exception2, *v2, *tb2;
if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
handle_system_exit();
}

But ... then my original question becomes even stronger: How do I
"catch" a SystemExit when embedding Python and not wanting that a
script with sys.exit just terminates the whole application?

--
Stefan Bellon
Aug 2 '07 #3

P: n/a
On Thu, 02 Aug, Farshid Lashkari wrote:
Also, here is some sample code that will catch system exit exceptions:

//Call python code
...
//Check for system exit exception
if(PyErr_Occurred()) {
if(PyErr_ExceptionMatches(PyExc_SystemExit)) {
//handle system exit
PyErr_Clear();
} else {
PyErr_Print();
}
}
Thanks for your hints ...

The interesting part is "Call python code". In my example this is done
with PyRun_SimpleString which does not return if an exception is not
handled but raised "out of" the interpreter. So I am unsure of what you
mean with "Call python code".

When installing an excepthook (see my other posting), then I can indeed
catch all exceptions ... except for SystemExit which is the one I'm
after.

--
Stefan Bellon
Aug 2 '07 #4

P: n/a
Stefan Bellon wrote:
Thanks for your hints ...

The interesting part is "Call python code". In my example this is done
with PyRun_SimpleString which does not return if an exception is not
handled but raised "out of" the interpreter. So I am unsure of what you
mean with "Call python code".

When installing an excepthook (see my other posting), then I can indeed
catch all exceptions ... except for SystemExit which is the one I'm
after.
You cannot use PyRun_SimpleString, since it will automatically print and
clear the error. You will need to use PyRun_String instead.
Aug 2 '07 #5

P: n/a
On Thu, 02 Aug, Farshid Lashkari wrote:
You cannot use PyRun_SimpleString, since it will automatically print
and clear the error. You will need to use PyRun_String instead.
Thanks, that helps a lot!

--
Stefan Bellon
Aug 2 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.