From 0d351e98e20982384282aab554b144c40d9cbe63 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 11 Aug 2012 10:58:54 +0200 Subject: [PATCH] Proper handling of SystemExitException in Python --- src/Gui/Application.cpp | 22 +++++++++++++++++----- src/Gui/Command.cpp | 3 +++ src/Gui/Macro.cpp | 2 +- src/Gui/MainWindow.cpp | 3 +-- src/Gui/PythonConsole.cpp | 2 +- src/Main/MainGui.cpp | 3 +++ src/Mod/Web/Gui/BrowserView.cpp | 14 ++++++++------ src/Mod/Web/Gui/BrowserView.h | 6 +++--- 8 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/Gui/Application.cpp b/src/Gui/Application.cpp index b7b8a8ea3..53dec5681 100644 --- a/src/Gui/Application.cpp +++ b/src/Gui/Application.cpp @@ -1457,9 +1457,10 @@ namespace Gui { */ class GUIApplication : public GUIApplicationNativeEventAware { + int systemExit; public: - GUIApplication(int & argc, char ** argv) - : GUIApplicationNativeEventAware(argc, argv) + GUIApplication(int & argc, char ** argv, int exitcode) + : GUIApplicationNativeEventAware(argc, argv), systemExit(exitcode) { } @@ -1480,6 +1481,10 @@ public: else return QApplication::notify(receiver, event); } + catch (const Base::SystemExitException&) { + qApp->exit(systemExit); + return true; + } catch (const Base::Exception& e) { Base::Console().Error("Unhandled Base::Exception caught in GUIApplication::notify.\n" "The error message is: %s\n", e.what()); @@ -1543,7 +1548,8 @@ 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(); - GUIApplication mainApp(argc, App::Application::GetARGV()); + int systemExit = 1000; + GUIApplication mainApp(argc, App::Application::GetARGV(), systemExit); // set application icon and window title const std::map& cfg = App::Application::Config(); std::map::const_iterator it; @@ -1716,9 +1722,15 @@ void Application::runApplication(void) Base::Console().Log("Init: Entering event loop\n"); try { - mainApp.exec(); + int ret = mainApp.exec(); + if (ret == systemExit) + throw Base::SystemExitException(); } - catch(...) { + catch (const Base::SystemExitException&) { + Base::Console().Message("System exit\n"); + throw; + } + catch (...) { // catching nasty stuff coming out of the event loop App::Application::destructObserver(); Base::Console().Error("Event loop left through unhandled exception\n"); diff --git a/src/Gui/Command.cpp b/src/Gui/Command.cpp index 40fdb9557..f66adf95e 100644 --- a/src/Gui/Command.cpp +++ b/src/Gui/Command.cpp @@ -281,6 +281,9 @@ void Command::invoke(int i) if (isActive()) activated( i ); } + catch (const Base::SystemExitException&) { + throw; + } catch (Base::PyException &e) { e.ReportException(); Base::Console().Error("Stack Trace: %s\n",e.getStackTrace().c_str()); diff --git a/src/Gui/Macro.cpp b/src/Gui/Macro.cpp index 1ac796478..20a7f49c4 100644 --- a/src/Gui/Macro.cpp +++ b/src/Gui/Macro.cpp @@ -233,7 +233,7 @@ void MacroManager::run(MacroType eType,const char *sName) Base::Interpreter().runFile(sName, this->localEnv); } catch (const Base::SystemExitException&) { - qApp->quit(); + throw; } catch (const Base::PyException& e) { Base::Console().Error("%s%s: %s\n", diff --git a/src/Gui/MainWindow.cpp b/src/Gui/MainWindow.cpp index a13a16c2c..cccdf146b 100644 --- a/src/Gui/MainWindow.cpp +++ b/src/Gui/MainWindow.cpp @@ -1157,8 +1157,7 @@ void MainWindow::delayedStartup() App::Application::processCmdLineFiles(); } catch (const Base::SystemExitException&) { - QApplication::quit(); - return; + throw; } const std::map& cfg = App::Application::Config(); diff --git a/src/Gui/PythonConsole.cpp b/src/Gui/PythonConsole.cpp index 780789b71..eaa7f53cb 100644 --- a/src/Gui/PythonConsole.cpp +++ b/src/Gui/PythonConsole.cpp @@ -709,7 +709,7 @@ void PythonConsole::runSource(const QString& line) } if (ret == QMessageBox::Yes) { PyErr_Clear(); - qApp->quit(); + throw; } else { PyErr_Clear(); diff --git a/src/Main/MainGui.cpp b/src/Main/MainGui.cpp index 5552bd6b4..288bc3df2 100644 --- a/src/Main/MainGui.cpp +++ b/src/Main/MainGui.cpp @@ -300,6 +300,9 @@ int main( int argc, char ** argv ) else App::Application::runApplication(); } + catch (const Base::SystemExitException&) { + exit(0); + } catch (const Base::Exception& e) { Base::Console().Error("%s\n", e.what()); } diff --git a/src/Mod/Web/Gui/BrowserView.cpp b/src/Mod/Web/Gui/BrowserView.cpp index 81c02dd8b..04739a351 100644 --- a/src/Mod/Web/Gui/BrowserView.cpp +++ b/src/Mod/Web/Gui/BrowserView.cpp @@ -114,7 +114,7 @@ BrowserView::BrowserView(QWidget* parent) connect(view, SIGNAL(loadProgress(int)), this, SLOT(onLoadProgress(int))); connect(view, SIGNAL(loadFinished(bool)), - this, SLOT(onLoadFinished())); + this, SLOT(onLoadFinished(bool))); connect(view, SIGNAL(linkClicked(const QUrl &)), this, SLOT(onLinkClicked(const QUrl &))); connect(view->page(), SIGNAL(downloadRequested(const QNetworkRequest &)), @@ -242,12 +242,14 @@ void BrowserView::onLoadProgress(int step) bar->setValue(step); } -void BrowserView::onLoadFinished() +void BrowserView::onLoadFinished(bool ok) { - QProgressBar* bar = Sequencer::instance()->getProgressBar(); - bar->setValue(100); - bar->hide(); - getMainWindow()->statusBar()->showMessage(QString()); + if (ok) { + QProgressBar* bar = Sequencer::instance()->getProgressBar(); + bar->setValue(100); + bar->hide(); + getMainWindow()->statusBar()->showMessage(QString()); + } isLoading = false; } diff --git a/src/Mod/Web/Gui/BrowserView.h b/src/Mod/Web/Gui/BrowserView.h index c32dfd659..553ccd160 100644 --- a/src/Mod/Web/Gui/BrowserView.h +++ b/src/Mod/Web/Gui/BrowserView.h @@ -92,10 +92,10 @@ public: protected Q_SLOTS: void onLoadStarted(); void onLoadProgress(int); - void onLoadFinished(); - void onLinkClicked ( const QUrl & url ) ; + void onLoadFinished(bool); + void onLinkClicked (const QUrl& url); bool chckHostAllowed(const QString& host); - void onDownloadRequested(const QNetworkRequest & request); + void onDownloadRequested(const QNetworkRequest& request); private: WebView* view;