Improve reporting of Python exceptions

This commit is contained in:
wmayer 2013-05-04 14:23:53 +02:00
parent 2bbe465229
commit 946bd02df4
12 changed files with 58 additions and 56 deletions

View File

@ -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);
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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;
}

View File

@ -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);

View File

@ -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());
}
// ---------------------------------------------------------

View File

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

View File

@ -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());

View File

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

View File

@ -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();
}
}

View File

@ -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();

View File

@ -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<App::DocumentObject*> 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<std::string> 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;