diff --git a/src/App/Application.cpp b/src/App/Application.cpp index 0ddbaa121..518c11c4e 100644 --- a/src/App/Application.cpp +++ b/src/App/Application.cpp @@ -1228,9 +1228,10 @@ void Application::processCmdLineFiles(void) Base::Interpreter().runFile(File.filePath().c_str(), true); } else if (File.hasExtension("py")) { - try{ + try { Base::Interpreter().loadModule(File.fileNamePure().c_str()); - }catch(PyException){ + } + catch(const PyException&) { // if module load not work, just try run the script (run in __main__) Base::Interpreter().runFile(File.filePath().c_str(),true); } diff --git a/src/App/DocumentObserverPython.cpp b/src/App/DocumentObserverPython.cpp index eeac4886c..e844ca7b7 100644 --- a/src/App/DocumentObserverPython.cpp +++ b/src/App/DocumentObserverPython.cpp @@ -101,7 +101,7 @@ void DocumentObserverPython::slotCreatedDocument(const App::Document& Doc) } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("%s\n", e.what()); + e.ReportException(); } } @@ -118,7 +118,7 @@ void DocumentObserverPython::slotDeletedDocument(const App::Document& Doc) } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("%s\n", e.what()); + e.ReportException(); } } @@ -135,7 +135,7 @@ void DocumentObserverPython::slotRelabelDocument(const App::Document& Doc) } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("%s\n", e.what()); + e.ReportException(); } } @@ -152,7 +152,7 @@ void DocumentObserverPython::slotActivateDocument(const App::Document& Doc) } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("%s\n", e.what()); + e.ReportException(); } } @@ -169,7 +169,7 @@ void DocumentObserverPython::slotCreatedObject(const App::DocumentObject& Obj) } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("%s\n", e.what()); + e.ReportException(); } } @@ -186,7 +186,7 @@ void DocumentObserverPython::slotDeletedObject(const App::DocumentObject& Obj) } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("%s\n", e.what()); + e.ReportException(); } } @@ -206,6 +206,6 @@ void DocumentObserverPython::slotChangedObject(const App::DocumentObject& Obj, } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("%s\n", e.what()); + e.ReportException(); } } diff --git a/src/App/FeaturePython.cpp b/src/App/FeaturePython.cpp index a4b1a0e12..9348ace12 100644 --- a/src/App/FeaturePython.cpp +++ b/src/App/FeaturePython.cpp @@ -67,6 +67,7 @@ DocumentObjectExecReturn *FeaturePythonImp::execute() } catch (Py::Exception&) { Base::PyException e; // extract the Python error text + e.ReportException(); std::stringstream str; str << object->Label.getValue() << ": " << e.what(); return new App::DocumentObjectExecReturn(str.str()); @@ -104,8 +105,7 @@ void FeaturePythonImp::onChanged(const Property* prop) } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("FeaturePython::onChanged (%s): %s\n", - object->Label.getValue(), e.what()); + e.ReportException(); } } diff --git a/src/App/PropertyPythonObject.cpp b/src/App/PropertyPythonObject.cpp index 0cdff1ae8..df42a5a73 100644 --- a/src/App/PropertyPythonObject.cpp +++ b/src/App/PropertyPythonObject.cpp @@ -108,7 +108,7 @@ std::string PropertyPythonObject::toString() const } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Warning("PropertyPythonObject::toString: %s\n", e.what()); + e.ReportException(); } return repr; @@ -139,7 +139,7 @@ void PropertyPythonObject::fromString(const std::string& repr) } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Warning("PropertyPythonObject::fromString: %s\n", e.what()); + e.ReportException(); } } @@ -165,7 +165,7 @@ void PropertyPythonObject::loadPickle(const std::string& str) } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Warning("PropertyPythonObject::loadPickle: %s\n", e.what()); + e.ReportException(); } } @@ -283,7 +283,7 @@ void PropertyPythonObject::Save (Base::Writer &writer) const } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Warning("PropertyPythonObject::Save: %s\n", e.what()); + e.ReportException(); } saveObject(writer); @@ -350,7 +350,7 @@ void PropertyPythonObject::Restore(Base::XMLReader &reader) } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Warning("PropertyPythonObject::Restore: %s\n", e.what()); + e.ReportException(); this->object = Py::None(); load_failed = true; } diff --git a/src/Base/Exception.h b/src/Base/Exception.h index 19bd9155c..d2c4a1ecd 100644 --- a/src/Base/Exception.h +++ b/src/Base/Exception.h @@ -48,7 +48,7 @@ public: Exception &operator=(const Exception &inst); virtual const char* what(void) const throw(); - void ReportException (void) const; + virtual void ReportException (void) const; inline void setMessage(const char * sMessage); inline void setMessage(const std::string& sMessage); diff --git a/src/Base/Interpreter.cpp b/src/Base/Interpreter.cpp index b9676bb9b..548265d98 100644 --- a/src/Base/Interpreter.cpp +++ b/src/Base/Interpreter.cpp @@ -71,9 +71,18 @@ PyException::PyException(void) _stackTrace = PP_last_error_trace; /* exception traceback text */ +} +PyException::~PyException() throw() +{ + PyGILStateLocker locker; PyErr_Clear(); // must be called to keep Python interpreter in a valid state (Werner) +} +void PyException::ReportException (void) const +{ + Base::Console().Error("%s%s: %s\n", + _stackTrace.c_str(), _errorType.c_str(), what()); } // --------------------------------------------------------- diff --git a/src/Base/Interpreter.h b/src/Base/Interpreter.h index a055e8aa2..fb61ccb73 100644 --- a/src/Base/Interpreter.h +++ b/src/Base/Interpreter.h @@ -54,11 +54,12 @@ class BaseExport PyException : public Exception public: /// constructor does the whole job PyException(void); - ~PyException() throw() {} + ~PyException() throw(); /// this function returns the stack trace const std::string &getStackTrace(void) const {return _stackTrace;} const std::string &getErrorType(void) const {return _errorType;} + void ReportException (void) const; protected: std::string _stackTrace; diff --git a/src/Gui/Macro.cpp b/src/Gui/Macro.cpp index 47a82ea20..e2bb6c507 100644 --- a/src/Gui/Macro.cpp +++ b/src/Gui/Macro.cpp @@ -240,8 +240,7 @@ void MacroManager::run(MacroType eType,const char *sName) throw; } catch (const Base::PyException& e) { - Base::Console().Error("%s%s: %s\n", - e.getStackTrace().c_str(), e.getErrorType().c_str(), e.what()); + e.ReportException(); } catch (const Base::Exception& e) { qWarning("%s",e.what()); diff --git a/src/Gui/PythonConsole.cpp b/src/Gui/PythonConsole.cpp index 3f8eb7d48..8783922fa 100644 --- a/src/Gui/PythonConsole.cpp +++ b/src/Gui/PythonConsole.cpp @@ -130,7 +130,7 @@ InteractiveInterpreter::InteractiveInterpreter() Base::PyGILStateLocker lock; PyObject* module = PyImport_ImportModule("code"); if (!module) - throw Base::PyException(); + throw Base::PyException(); PyObject* func = PyObject_GetAttrString(module, "InteractiveInterpreter"); PyObject* args = Py_BuildValue("()"); d = new InteractiveInterpreterP; diff --git a/src/Gui/Selection.cpp b/src/Gui/Selection.cpp index a42da3a64..d9ba9ca99 100644 --- a/src/Gui/Selection.cpp +++ b/src/Gui/Selection.cpp @@ -173,7 +173,7 @@ void SelectionObserverPython::addSelection(const SelectionChanges& msg) } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("%s\n", e.what()); + e.ReportException(); } } @@ -192,7 +192,7 @@ void SelectionObserverPython::removeSelection(const SelectionChanges& msg) } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("%s\n", e.what()); + e.ReportException(); } } @@ -209,7 +209,7 @@ void SelectionObserverPython::setSelection(const SelectionChanges& msg) } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("%s\n", e.what()); + e.ReportException(); } } @@ -226,7 +226,7 @@ void SelectionObserverPython::clearSelection(const SelectionChanges& msg) } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("%s\n", e.what()); + e.ReportException(); } } @@ -245,7 +245,7 @@ void SelectionObserverPython::setPreselection(const SelectionChanges& msg) } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("%s\n", e.what()); + e.ReportException(); } } @@ -264,7 +264,7 @@ void SelectionObserverPython::removePreselection(const SelectionChanges& msg) } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("%s\n", e.what()); + e.ReportException(); } } diff --git a/src/Gui/TaskView/TaskDialogPython.cpp b/src/Gui/TaskView/TaskDialogPython.cpp index fbdb964d8..c921eb110 100644 --- a/src/Gui/TaskView/TaskDialogPython.cpp +++ b/src/Gui/TaskView/TaskDialogPython.cpp @@ -238,7 +238,7 @@ bool TaskWatcherPython::shouldShow() } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("TaskWatcherPython::shouldShow: %s\n", e.what()); + e.ReportException(); } if (!this->Filter.empty()) @@ -329,7 +329,7 @@ void TaskDialogPython::open() } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("TaskDialogPython::open: %s\n", e.what()); + e.ReportException(); } } @@ -346,7 +346,7 @@ void TaskDialogPython::clicked(int i) } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("TaskDialogPython::clicked: %s\n", e.what()); + e.ReportException(); } } @@ -363,7 +363,7 @@ bool TaskDialogPython::accept() } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("TaskDialogPython::accept: %s\n", e.what()); + e.ReportException(); } return TaskDialog::accept(); @@ -382,7 +382,7 @@ bool TaskDialogPython::reject() } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("TaskDialogPython::reject: %s\n", e.what()); + e.ReportException(); } return TaskDialog::reject(); @@ -400,7 +400,7 @@ void TaskDialogPython::helpRequested() } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("TaskDialogPython::helpRequested: %s\n", e.what()); + e.ReportException(); } } @@ -418,7 +418,7 @@ QDialogButtonBox::StandardButtons TaskDialogPython::getStandardButtons(void) con } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("TaskDialogPython::getStandardButtons: %s\n", e.what()); + e.ReportException(); } return TaskDialog::getStandardButtons(); @@ -441,7 +441,7 @@ bool TaskDialogPython::isAllowedAlterDocument(void) const } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("TaskDialogPython::isAllowedAlterDocument: %s\n", e.what()); + e.ReportException(); } return TaskDialog::isAllowedAlterDocument(); @@ -460,7 +460,7 @@ bool TaskDialogPython::isAllowedAlterView(void) const } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("TaskDialogPython::isAllowedAlterView: %s\n", e.what()); + e.ReportException(); } return TaskDialog::isAllowedAlterView(); @@ -479,7 +479,7 @@ bool TaskDialogPython::isAllowedAlterSelection(void) const } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("TaskDialogPython::isAllowedAlterSelection: %s\n", e.what()); + e.ReportException(); } return TaskDialog::isAllowedAlterSelection(); @@ -498,7 +498,7 @@ bool TaskDialogPython::needsFullSpace() const } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("TaskDialogPython::needsFullSpace: %s\n", e.what()); + e.ReportException(); } return TaskDialog::needsFullSpace(); diff --git a/src/Gui/ViewProviderPythonFeature.cpp b/src/Gui/ViewProviderPythonFeature.cpp index 059131215..af668ed6a 100644 --- a/src/Gui/ViewProviderPythonFeature.cpp +++ b/src/Gui/ViewProviderPythonFeature.cpp @@ -264,7 +264,7 @@ QIcon ViewProviderPythonFeatureImp::getIcon() const } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("ViewProviderPythonFeature::getIcon: %s\n", e.what()); + e.ReportException(); } return QIcon(); @@ -297,7 +297,7 @@ std::vector ViewProviderPythonFeatureImp::claimChildren(co } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - Base::Console().Error("ViewProviderPythonFeature::claimChildren: %s\n", e.what()); + e.ReportException(); } return children; @@ -342,8 +342,7 @@ bool ViewProviderPythonFeatureImp::setEdit(int ModNum) } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - const char* name = object->getObject()->Label.getValue(); - Base::Console().Error("ViewProviderPythonFeature::setEdit (%s): %s\n", name, e.what()); + e.ReportException(); } return false; @@ -378,8 +377,7 @@ bool ViewProviderPythonFeatureImp::unsetEdit(int ModNum) } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - const char* name = object->getObject()->Label.getValue(); - Base::Console().Error("ViewProviderPythonFeature::unsetEdit (%s): %s\n", name, e.what()); + e.ReportException(); } return false; @@ -414,8 +412,7 @@ void ViewProviderPythonFeatureImp::attach(App::DocumentObject *pcObject) } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - const char* name = object->getObject()->Label.getValue(); - Base::Console().Error("ViewProviderPythonFeature::attach (%s): %s\n", name, e.what()); + e.ReportException(); } } @@ -452,8 +449,7 @@ void ViewProviderPythonFeatureImp::updateData(const App::Property* prop) } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - const char* name = object->getObject()->Label.getValue(); - Base::Console().Error("ViewProviderPythonFeature::updateData (%s): %s\n", name, e.what()); + e.ReportException(); } } @@ -486,8 +482,7 @@ void ViewProviderPythonFeatureImp::onChanged(const App::Property* prop) } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - const char* name = object->getObject()->Label.getValue(); - Base::Console().Error("ViewProviderPythonFeature::onChanged (%s): %s\n", name, e.what()); + e.ReportException(); } } @@ -529,8 +524,7 @@ const char* ViewProviderPythonFeatureImp::getDefaultDisplayMode() const } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - const char* name = object->getObject()->Label.getValue(); - Base::Console().Error("ViewProviderPythonFeature::getDefaultDisplayMode (%s): %s\n", name, e.what()); + e.ReportException(); } return 0; @@ -570,8 +564,7 @@ std::vector ViewProviderPythonFeatureImp::getDisplayModes(void) con } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - const char* name = object->getObject()->Label.getValue(); - Base::Console().Error("ViewProviderPythonFeature::getDisplayModes (%s): %s\n", name, e.what()); + e.ReportException(); } return modes; @@ -596,8 +589,7 @@ std::string ViewProviderPythonFeatureImp::setDisplayMode(const char* ModeName) } catch (Py::Exception&) { Base::PyException e; // extract the Python error text - const char* name = object->getObject()->Label.getValue(); - Base::Console().Error("ViewProviderPythonFeature::setDisplayMode (%s): %s\n", name, e.what()); + e.ReportException(); } return ModeName;