Skip to content

Commit

Permalink
+ issue: #2350, handle Python's SystemExit exceptions and do not exit…
Browse files Browse the repository at this point in the history
… application when running from macro dialog or Python editor
  • Loading branch information
wwmayer committed Dec 27, 2015
1 parent 0f9d6c1 commit 07ba938
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 35 deletions.
60 changes: 30 additions & 30 deletions src/Base/Interpreter.cpp
Expand Up @@ -265,43 +265,43 @@ void InterpreterSingleton::runFile(const char*pxFileName, bool local)
//std::string encoding = PyUnicode_GetDefaultEncoding();
//PyUnicode_SetDefaultEncoding("utf-8");
//PyUnicode_SetDefaultEncoding(encoding.c_str());
PyObject *module, *dict;
module = PyImport_AddModule("__main__");
dict = PyModule_GetDict(module);
if (local) {
PyObject *module, *dict;
module = PyImport_AddModule("__main__");
dict = PyModule_GetDict(module);
dict = PyDict_Copy(dict);
if (PyDict_GetItemString(dict, "__file__") == NULL) {
PyObject *f = PyString_FromString(pxFileName);
if (f == NULL) {
fclose(fp);
return;
}
if (PyDict_SetItemString(dict, "__file__", f) < 0) {
Py_DECREF(f);
fclose(fp);
return;
}
}
else {
Py_INCREF(dict); // avoid to further distinguish between local and global dict
}

if (PyDict_GetItemString(dict, "__file__") == NULL) {
PyObject *f = PyString_FromString(pxFileName);
if (f == NULL) {
fclose(fp);
Py_DECREF(dict);
return;
}
if (PyDict_SetItemString(dict, "__file__", f) < 0) {
Py_DECREF(f);
fclose(fp);
Py_DECREF(dict);
return;
}
Py_DECREF(f);
}

PyObject *result = PyRun_File(fp, pxFileName, Py_file_input, dict, dict);
fclose(fp);
Py_DECREF(dict);
if (!result) {
if (PyErr_ExceptionMatches(PyExc_SystemExit))
throw SystemExitException();
else
throw PyException();
}
Py_DECREF(result);
}
else {
int ret = PyRun_SimpleFile(fp, pxFileName);
fclose(fp);
if (ret != 0)
PyObject *result = PyRun_File(fp, pxFileName, Py_file_input, dict, dict);
fclose(fp);
Py_DECREF(dict);

if (!result) {
if (PyErr_ExceptionMatches(PyExc_SystemExit))
throw SystemExitException();
else
throw PyException();
}

Py_DECREF(result);
}
else {
std::string err = "Unknown file: ";
Expand Down
17 changes: 13 additions & 4 deletions src/Gui/DlgMacroExecuteImp.cpp
Expand Up @@ -40,6 +40,7 @@

#include <App/Application.h>
#include <App/Document.h>
#include <Base/Interpreter.h>

using namespace Gui;
using namespace Gui::Dialog;
Expand Down Expand Up @@ -118,10 +119,18 @@ void DlgMacroExecuteImp::accept()
QDialog::accept();
QDir dir(this->macroPath);
QFileInfo fi(dir, item->text(0));
Application::Instance->macroManager()->run(Gui::MacroManager::File, fi.filePath().toUtf8());
// after macro run recalculate the document
if (Application::Instance->activeDocument())
Application::Instance->activeDocument()->getDocument()->recompute();
try {
Application::Instance->macroManager()->run(Gui::MacroManager::File, fi.filePath().toUtf8());
// after macro run recalculate the document
if (Application::Instance->activeDocument())
Application::Instance->activeDocument()->getDocument()->recompute();
}
catch (const Base::SystemExitException&) {
// handle SystemExit exceptions
Base::PyGILStateLocker locker;
Base::PyException e;
e.ReportException();
}
}

/**
Expand Down
11 changes: 10 additions & 1 deletion src/Gui/EditorView.cpp
Expand Up @@ -51,6 +51,7 @@

#include <Base/Interpreter.h>
#include <Base/Parameter.h>
#include <Base/Exception.h>

using namespace Gui;
namespace Gui {
Expand Down Expand Up @@ -559,7 +560,15 @@ void PythonEditorView::executeScript()
// always save the macro when it is modified
if (EditorView::onHasMsg("Save"))
EditorView::onMsg("Save", 0);
Application::Instance->macroManager()->run(Gui::MacroManager::File,fileName().toUtf8());
try {
Application::Instance->macroManager()->run(Gui::MacroManager::File,fileName().toUtf8());
}
catch (const Base::SystemExitException&) {
// handle SystemExit exceptions
Base::PyGILStateLocker locker;
Base::PyException e;
e.ReportException();
}
}

void PythonEditorView::startDebug()
Expand Down

0 comments on commit 07ba938

Please sign in to comment.