diff --git a/src/Base/Interpreter.cpp b/src/Base/Interpreter.cpp index ff883ae01..bd0b1d347 100644 --- a/src/Base/Interpreter.cpp +++ b/src/Base/Interpreter.cpp @@ -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; - } - 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) - throw PyException(); + 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 { std::string err = "Unknown file: "; diff --git a/src/Gui/DlgMacroExecuteImp.cpp b/src/Gui/DlgMacroExecuteImp.cpp index 1c5f75bbe..52bb17163 100644 --- a/src/Gui/DlgMacroExecuteImp.cpp +++ b/src/Gui/DlgMacroExecuteImp.cpp @@ -40,6 +40,7 @@ #include #include +#include using namespace Gui; using namespace Gui::Dialog; @@ -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(); + } } /** diff --git a/src/Gui/EditorView.cpp b/src/Gui/EditorView.cpp index 0aee2fd2b..5b25121bc 100644 --- a/src/Gui/EditorView.cpp +++ b/src/Gui/EditorView.cpp @@ -51,6 +51,7 @@ #include #include +#include using namespace Gui; namespace Gui { @@ -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()