From bc71c6e64ba490abcd38b78013b272a6b76df00f Mon Sep 17 00:00:00 2001 From: Ian Rees Date: Sun, 27 Mar 2016 13:01:04 +1300 Subject: [PATCH] More fixes around Python exit codes --- src/Base/Interpreter.cpp | 2 +- src/Gui/Application.cpp | 11 ++++++----- src/Gui/GuiApplication.cpp | 14 +++++++------- src/Gui/GuiApplication.h | 12 +++++++----- src/Main/MainCmd.cpp | 4 ++-- 5 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/Base/Interpreter.cpp b/src/Base/Interpreter.cpp index 6a0c4847c..d5b2a5152 100644 --- a/src/Base/Interpreter.cpp +++ b/src/Base/Interpreter.cpp @@ -131,7 +131,7 @@ SystemExitException::SystemExitException() } SystemExitException::SystemExitException(const SystemExitException &inst) - : Exception(inst) + : Exception(inst), _exitCode(inst._exitCode) { } diff --git a/src/Gui/Application.cpp b/src/Gui/Application.cpp index cb39dd4c4..790980a28 100644 --- a/src/Gui/Application.cpp +++ b/src/Gui/Application.cpp @@ -1553,8 +1553,7 @@ void Application::runApplication(void) Base::Console().Log("Init: Creating Gui::Application and QApplication\n"); // if application not yet created by the splasher int argc = App::Application::GetARGC(); - int systemExit = 1000; - GUISingleApplication mainApp(argc, App::Application::GetARGV(), systemExit); + GUISingleApplication mainApp(argc, App::Application::GetARGV()); // check if a single or multiple instances can run it = cfg.find("SingleInstance"); @@ -1786,9 +1785,11 @@ void Application::runApplication(void) boost::interprocess::file_lock flock(s.str().c_str()); flock.lock(); - int ret = mainApp.exec(); - if (ret == systemExit) - throw Base::SystemExitException(); + mainApp.exec(); + // Qt can't handle exceptions thrown from event handlers, so we need + // to manually rethrow SystemExitExceptions. + if(mainApp.caughtException.get()) + throw Base::SystemExitException(*mainApp.caughtException.get()); // close the lock file, in case of a crash we can see the existing lock file // on the next restart and try to repair the documents, if needed. diff --git a/src/Gui/GuiApplication.cpp b/src/Gui/GuiApplication.cpp index 74183ee38..f5d7772a7 100644 --- a/src/Gui/GuiApplication.cpp +++ b/src/Gui/GuiApplication.cpp @@ -54,14 +54,13 @@ #include #include -#include #include using namespace Gui; -GUIApplication::GUIApplication(int & argc, char ** argv, int exitcode) - : GUIApplicationNativeEventAware(argc, argv), systemExit(exitcode) +GUIApplication::GUIApplication(int & argc, char ** argv) + : GUIApplicationNativeEventAware(argc, argv) { } @@ -82,8 +81,9 @@ bool GUIApplication::notify (QObject * receiver, QEvent * event) else return QApplication::notify(receiver, event); } - catch (const Base::SystemExitException&) { - qApp->exit(systemExit); + catch (const Base::SystemExitException &e) { + caughtException.reset(new Base::SystemExitException(e)); + qApp->exit(e.getExitCode()); return true; } catch (const Base::Exception& e) { @@ -225,8 +225,8 @@ public: bool running; }; -GUISingleApplication::GUISingleApplication(int & argc, char ** argv, int exitcode) - : GUIApplication(argc, argv, exitcode), +GUISingleApplication::GUISingleApplication(int & argc, char ** argv) + : GUIApplication(argc, argv), d_ptr(new Private(this)) { d_ptr->setupConnection(); diff --git a/src/Gui/GuiApplication.h b/src/Gui/GuiApplication.h index bc7827623..b245ae02f 100644 --- a/src/Gui/GuiApplication.h +++ b/src/Gui/GuiApplication.h @@ -25,7 +25,9 @@ #define GUI_APPLICATION_H #include "GuiApplicationNativeEventAware.h" +#include // For Base::SystemExitException #include +#include class QSessionManager; @@ -39,7 +41,7 @@ class GUIApplication : public GUIApplicationNativeEventAware Q_OBJECT public: - explicit GUIApplication(int & argc, char ** argv, int exitcode); + explicit GUIApplication(int & argc, char ** argv); virtual ~GUIApplication(); /** @@ -49,11 +51,11 @@ public: bool notify (QObject * receiver, QEvent * event); void commitData(QSessionManager &manager); + /// Pointer to exceptions caught in Qt event handler + boost::shared_ptr caughtException; + protected: bool event(QEvent * event); - -private: - int systemExit; }; class GUISingleApplication : public GUIApplication @@ -61,7 +63,7 @@ class GUISingleApplication : public GUIApplication Q_OBJECT public: - explicit GUISingleApplication(int & argc, char ** argv, int exitcode); + explicit GUISingleApplication(int & argc, char ** argv); virtual ~GUISingleApplication(); bool isRunning() const; diff --git a/src/Main/MainCmd.cpp b/src/Main/MainCmd.cpp index 3c3a9376c..397e07c84 100644 --- a/src/Main/MainCmd.cpp +++ b/src/Main/MainCmd.cpp @@ -125,8 +125,8 @@ int main( int argc, char ** argv ) try { Application::runApplication(); } - catch (const Base::SystemExitException&) { - exit(0); + catch (const Base::SystemExitException &e) { + exit(e.getExitCode()); } catch (const Base::Exception& e) { e.ReportException();