+ handle Python's SystemExit exception when running script or macro

git-svn-id: https://free-cad.svn.sourceforge.net/svnroot/free-cad/trunk@5398 e8eeb9e2-ec13-0410-a4a9-efa5cf37419d
This commit is contained in:
wmayer 2012-01-11 18:39:27 +00:00
parent 054384b756
commit 048528b886
3 changed files with 23 additions and 8 deletions

View File

@ -1190,6 +1190,10 @@ void Application::processCmdLineFiles(void)
}
}
}
catch (const Base::SystemExitException&) {
Base::PyGILStateLocker locker;
Base::Interpreter().systemExit();
}
catch (const Base::Exception& e) {
Console().Error("Exception while processing file: %s [%s]\n", File.filePath().c_str(), e.what());
}

View File

@ -242,8 +242,12 @@ void InterpreterSingleton::runFile(const char*pxFileName, bool local)
PyObject *result = PyRun_File(fp, pxFileName, Py_file_input, dict, dict);
fclose(fp);
Py_DECREF(dict);
if (!result)
throw PyException();
if (!result) {
if (PyErr_ExceptionMatches(PyExc_SystemExit))
throw SystemExitException();
else
throw PyException();
}
Py_DECREF(result);
}
else {
@ -271,8 +275,12 @@ bool InterpreterSingleton::loadModule(const char* psModName)
PyGILStateLocker locker;
module = PP_Load_Module(psModName);
if (!module)
throw PyException();
if (!module) {
if (PyErr_ExceptionMatches(PyExc_SystemExit))
throw SystemExitException();
else
throw PyException();
}
return true;
}

View File

@ -216,15 +216,18 @@ namespace Gui {
void MacroManager::run(MacroType eType,const char *sName)
{
try
{
try {
PythonRedirector std_out("stdout",new OutputStdout);
PythonRedirector std_err("stderr",new OutputStderr);
//The given path name is expected to be Utf-8
Base::Interpreter().runFile(sName, true);
}
catch (const Base::Exception& e)
{
catch (const Base::SystemExitException&) {
Base::PyGILStateLocker lock;
PyErr_Clear();
Base::Interpreter().systemExit();
}
catch (const Base::Exception& e) {
qWarning("%s",e.what());
}
}