From fbca57bd36c355111155ac0876c6d9b4965f50c5 Mon Sep 17 00:00:00 2001 From: Abdullah Tahiri Date: Sat, 6 May 2017 01:36:45 +0200 Subject: [PATCH] Use PyErr_setObject with a PyDict to set Base::Exception information to BaseFreeCADError Python exception --- .../templateClassPyExport.py | 83 +++++++++++++------ 1 file changed, 58 insertions(+), 25 deletions(-) diff --git a/src/Tools/generateTemplates/templateClassPyExport.py b/src/Tools/generateTemplates/templateClassPyExport.py index 72705888ef..b08a0d23ba 100644 --- a/src/Tools/generateTemplates/templateClassPyExport.py +++ b/src/Tools/generateTemplates/templateClassPyExport.py @@ -494,12 +494,19 @@ PyObject * @self.export.Name@::staticCallback_@i.Name@ (PyObject *self, PyObject } catch(const Base::Exception& e) // catch the FreeCAD exceptions { - std::string str; - str += "FreeCAD exception thrown ("; - str += e.what(); - str += ")"; + PyObject *edict = PyDict_New(); + + PyDict_SetItemString(edict, "sclassname", PyString_FromString(typeid(e).name())); + PyDict_SetItemString(edict, "sErrMsg", PyString_FromString(e.getMessage().c_str())); + PyDict_SetItemString(edict, "sfile", PyString_FromString(e.getFile().c_str())); + PyDict_SetItemString(edict, "iline", PyInt_FromLong(e.getLine())); + PyDict_SetItemString(edict, "sfunction", PyString_FromString(e.getFunction().c_str())); + PyDict_SetItemString(edict, "swhat", PyString_FromString(e.what())); + e.ReportException(); - PyErr_SetString(Base::BaseExceptionFreeCADError,str.c_str()); + PyErr_SetObject(Base::BaseExceptionFreeCADError, edict); + Py_DECREF(edict); + return NULL; } catch(const boost::filesystem::filesystem_error& e) // catch boost filesystem exception @@ -684,12 +691,18 @@ PyObject *@self.export.Name@::_getattr(char *attr) // __getattr__ function: n #ifndef DONT_CATCH_CXX_EXCEPTIONS catch(const Base::Exception& e) // catch the FreeCAD exceptions { - std::string str; - str += "FreeCAD exception thrown ("; - str += e.what(); - str += ")"; + PyObject *edict = PyDict_New(); + + PyDict_SetItemString(edict, "sclassname", PyString_FromString(typeid(e).name())); + PyDict_SetItemString(edict, "sErrMsg", PyString_FromString(e.getMessage().c_str())); + PyDict_SetItemString(edict, "sfile", PyString_FromString(e.getFile().c_str())); + PyDict_SetItemString(edict, "iline", PyInt_FromLong(e.getLine())); + PyDict_SetItemString(edict, "sfunction", PyString_FromString(e.getFunction().c_str())); + PyDict_SetItemString(edict, "swhat", PyString_FromString(e.what())); + e.ReportException(); - PyErr_SetString(Base::BaseExceptionFreeCADError,str.c_str()); + PyErr_SetObject(Base::BaseExceptionFreeCADError, edict); + Py_DECREF(edict); return NULL; } catch(const std::exception& e) // catch other c++ exceptions @@ -715,12 +728,19 @@ PyObject *@self.export.Name@::_getattr(char *attr) // __getattr__ function: n #else // DONT_CATCH_CXX_EXCEPTIONS catch(const Base::Exception& e) // catch the FreeCAD exceptions { - std::string str; - str += "FreeCAD exception thrown ("; - str += e.what(); - str += ")"; + PyObject *edict = PyDict_New(); + + PyDict_SetItemString(edict, "sclassname", PyString_FromString(typeid(e).name())); + PyDict_SetItemString(edict, "sErrMsg", PyString_FromString(e.getMessage().c_str())); + PyDict_SetItemString(edict, "sfile", PyString_FromString(e.getFile().c_str())); + PyDict_SetItemString(edict, "iline", PyInt_FromLong(e.getLine())); + PyDict_SetItemString(edict, "sfunction", PyString_FromString(e.getFunction().c_str())); + PyDict_SetItemString(edict, "swhat", PyString_FromString(e.what())); + e.ReportException(); - PyErr_SetString(Base::BaseExceptionFreeCADError,str.c_str()); + PyErr_SetObject(Base::BaseExceptionFreeCADError, edict); + Py_DECREF(edict); + return NULL; } catch(const Py::Exception&) @@ -758,12 +778,19 @@ int @self.export.Name@::_setattr(char *attr, PyObject *value) // __setattr__ fun #ifndef DONT_CATCH_CXX_EXCEPTIONS catch(const Base::Exception& e) // catch the FreeCAD exceptions { - std::string str; - str += "FreeCAD exception thrown ("; - str += e.what(); - str += ")"; + PyObject *edict = PyDict_New(); + + PyDict_SetItemString(edict, "sclassname", PyString_FromString(typeid(e).name())); + PyDict_SetItemString(edict, "sErrMsg", PyString_FromString(e.getMessage().c_str())); + PyDict_SetItemString(edict, "sfile", PyString_FromString(e.getFile().c_str())); + PyDict_SetItemString(edict, "iline", PyInt_FromLong(e.getLine())); + PyDict_SetItemString(edict, "sfunction", PyString_FromString(e.getFunction().c_str())); + PyDict_SetItemString(edict, "swhat", PyString_FromString(e.what())); + e.ReportException(); - PyErr_SetString(Base::BaseExceptionFreeCADError,str.c_str()); + PyErr_SetObject(Base::BaseExceptionFreeCADError, edict); + Py_DECREF(edict); + return -1; } catch(const std::exception& e) // catch other c++ exceptions @@ -789,12 +816,18 @@ int @self.export.Name@::_setattr(char *attr, PyObject *value) // __setattr__ fun #else // DONT_CATCH_CXX_EXCEPTIONS catch(const Base::Exception& e) // catch the FreeCAD exceptions { - std::string str; - str += "FreeCAD exception thrown ("; - str += e.what(); - str += ")"; + PyObject *edict = PyDict_New(); + + PyDict_SetItemString(edict, "sclassname", PyString_FromString(typeid(e).name())); + PyDict_SetItemString(edict, "sErrMsg", PyString_FromString(e.getMessage().c_str())); + PyDict_SetItemString(edict, "sfile", PyString_FromString(e.getFile().c_str())); + PyDict_SetItemString(edict, "iline", PyInt_FromLong(e.getLine())); + PyDict_SetItemString(edict, "sfunction", PyString_FromString(e.getFunction().c_str())); + PyDict_SetItemString(edict, "swhat", PyString_FromString(e.what())); + e.ReportException(); - PyErr_SetString(Base::BaseExceptionFreeCADError,str.c_str()); + PyErr_SetObject(Base::BaseExceptionFreeCADError, edict); + Py_DECREF(edict); return -1; } catch(const Py::Exception&)