473,503 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Catching SystemExit in C API code when embedding Python?

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
5 3795
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4968
by: Olli Piepponen | last post by:
Hi, I'm having a little problem catching keystrokes under Windows. I did a little research and found that with mscvrt.getch() one can cath a single key that is pressed. However this doesn't work...
1
1813
by: Tommy Nordgren | last post by:
I want to write an application that embeds and extends (at least) the Python and Perl interpreters. Now i want to find as much as possible about the Python tools used for extending and embedding...
17
19630
by: seberino | last post by:
How can a proprietary software developer protect their Python code? People often ask me about obfuscating Python bytecode. They don't want people to easily decompile their proprietary Python app....
0
1246
by: Steven Bethard | last post by:
I have an optparse-like module and though I have a full unittest-style suite of tests for it, I'd also like to be able to run doctest on the documentation to make sure my examples all work. ...
5
2300
by: Avi Kak | last post by:
Folks, Does regular expression processing in Python allow for executable code to be embedded inside a regular expression? For example, in Perl the following two statements $regex =...
2
2335
by: p.lavarre | last post by:
From: http://docs.python.org/lib/doctest-soapbox.html ... Can I somehow tell doctest that it's time to quit? I ask because not all doctest examples are created equal. Some failures are...
3
11319
by: Will McGugan | last post by:
Hi, Is there any difference between calling sys.exit() and raise SystemExit? Should I prefer one over the other? Regards, Will McGugan -- blog: http://www.willmcgugan.com
2
2306
by: bappai | last post by:
Hello, I am trying to actually call a GUI from my C++ code which would have buttons and therefore can call functions from C++ again, ie extend the C++ code also. I am faced with a peculiar...
3
3265
by: john | last post by:
I wrapped some fortran code using F2PY and need to be able to catch fortran runtime errors to run the following: # "grid" is a wrapped fortran module # no runtime errors incurred when run with...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7011
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5596
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4689
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.