From 048528b886587e30fc857aec7b6cefb75f5f8502 Mon Sep 17 00:00:00 2001 From: wmayer Date: Wed, 11 Jan 2012 18:39:27 +0000 Subject: [PATCH] + 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 --- src/App/Application.cpp | 4 ++++ src/Base/Interpreter.cpp | 16 ++++++++++++---- src/Gui/Macro.cpp | 11 +++++++---- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/App/Application.cpp b/src/App/Application.cpp index 8c43039e5..4a7d79407 100644 --- a/src/App/Application.cpp +++ b/src/App/Application.cpp @@ -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()); } diff --git a/src/Base/Interpreter.cpp b/src/Base/Interpreter.cpp index e54da64af..a1bd56a9a 100644 --- a/src/Base/Interpreter.cpp +++ b/src/Base/Interpreter.cpp @@ -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; } diff --git a/src/Gui/Macro.cpp b/src/Gui/Macro.cpp index 067f44fa1..f1b505fc2 100644 --- a/src/Gui/Macro.cpp +++ b/src/Gui/Macro.cpp @@ -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()); } }