+ 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:
parent
0f9d6c14ae
commit
07ba938ff6
|
@ -265,43 +265,43 @@ void InterpreterSingleton::runFile(const char*pxFileName, bool local)
|
||||||
//std::string encoding = PyUnicode_GetDefaultEncoding();
|
//std::string encoding = PyUnicode_GetDefaultEncoding();
|
||||||
//PyUnicode_SetDefaultEncoding("utf-8");
|
//PyUnicode_SetDefaultEncoding("utf-8");
|
||||||
//PyUnicode_SetDefaultEncoding(encoding.c_str());
|
//PyUnicode_SetDefaultEncoding(encoding.c_str());
|
||||||
|
PyObject *module, *dict;
|
||||||
|
module = PyImport_AddModule("__main__");
|
||||||
|
dict = PyModule_GetDict(module);
|
||||||
if (local) {
|
if (local) {
|
||||||
PyObject *module, *dict;
|
|
||||||
module = PyImport_AddModule("__main__");
|
|
||||||
dict = PyModule_GetDict(module);
|
|
||||||
dict = PyDict_Copy(dict);
|
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 {
|
else {
|
||||||
int ret = PyRun_SimpleFile(fp, pxFileName);
|
Py_INCREF(dict); // avoid to further distinguish between local and global dict
|
||||||
fclose(fp);
|
|
||||||
if (ret != 0)
|
|
||||||
throw PyException();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
else {
|
||||||
std::string err = "Unknown file: ";
|
std::string err = "Unknown file: ";
|
||||||
|
|
|
@ -40,6 +40,7 @@
|
||||||
|
|
||||||
#include <App/Application.h>
|
#include <App/Application.h>
|
||||||
#include <App/Document.h>
|
#include <App/Document.h>
|
||||||
|
#include <Base/Interpreter.h>
|
||||||
|
|
||||||
using namespace Gui;
|
using namespace Gui;
|
||||||
using namespace Gui::Dialog;
|
using namespace Gui::Dialog;
|
||||||
|
@ -118,10 +119,18 @@ void DlgMacroExecuteImp::accept()
|
||||||
QDialog::accept();
|
QDialog::accept();
|
||||||
QDir dir(this->macroPath);
|
QDir dir(this->macroPath);
|
||||||
QFileInfo fi(dir, item->text(0));
|
QFileInfo fi(dir, item->text(0));
|
||||||
Application::Instance->macroManager()->run(Gui::MacroManager::File, fi.filePath().toUtf8());
|
try {
|
||||||
// after macro run recalculate the document
|
Application::Instance->macroManager()->run(Gui::MacroManager::File, fi.filePath().toUtf8());
|
||||||
if (Application::Instance->activeDocument())
|
// after macro run recalculate the document
|
||||||
Application::Instance->activeDocument()->getDocument()->recompute();
|
if (Application::Instance->activeDocument())
|
||||||
|
Application::Instance->activeDocument()->getDocument()->recompute();
|
||||||
|
}
|
||||||
|
catch (const Base::SystemExitException&) {
|
||||||
|
// handle SystemExit exceptions
|
||||||
|
Base::PyGILStateLocker locker;
|
||||||
|
Base::PyException e;
|
||||||
|
e.ReportException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -51,6 +51,7 @@
|
||||||
|
|
||||||
#include <Base/Interpreter.h>
|
#include <Base/Interpreter.h>
|
||||||
#include <Base/Parameter.h>
|
#include <Base/Parameter.h>
|
||||||
|
#include <Base/Exception.h>
|
||||||
|
|
||||||
using namespace Gui;
|
using namespace Gui;
|
||||||
namespace Gui {
|
namespace Gui {
|
||||||
|
@ -559,7 +560,15 @@ void PythonEditorView::executeScript()
|
||||||
// always save the macro when it is modified
|
// always save the macro when it is modified
|
||||||
if (EditorView::onHasMsg("Save"))
|
if (EditorView::onHasMsg("Save"))
|
||||||
EditorView::onMsg("Save", 0);
|
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()
|
void PythonEditorView::startDebug()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user