+ issue: #0002350, handle Python's SystemExit exceptions and do not exit application when running from macro dialog or Python editor

This commit is contained in:
wmayer 2015-12-27 23:00:59 +01:00
parent 0f9d6c14ae
commit 07ba938ff6
3 changed files with 54 additions and 36 deletions

View File

@ -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: ";

View File

@ -40,6 +40,7 @@
#include <App/Application.h>
#include <App/Document.h>
#include <Base/Interpreter.h>
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();
}
}
/**

View File

@ -51,6 +51,7 @@
#include <Base/Interpreter.h>
#include <Base/Parameter.h>
#include <Base/Exception.h>
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()