Skip to content

Commit

Permalink
Use PyErr_setObject with a PyDict to set Base::Exception information …
Browse files Browse the repository at this point in the history
…to BaseFreeCADError Python exception
  • Loading branch information
abdullahtahiriyo authored and wwmayer committed May 13, 2017
1 parent ea725a8 commit fbca57b
Showing 1 changed file with 58 additions and 25 deletions.
83 changes: 58 additions & 25 deletions src/Tools/generateTemplates/templateClassPyExport.py
Expand Up @@ -494,12 +494,19 @@ class @self.export.Namespace@Export @self.export.Name@ : public @self.export.Fat
}
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
Expand Down Expand Up @@ -684,12 +691,18 @@ class @self.export.Namespace@Export @self.export.Name@ : public @self.export.Fat
#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
Expand All @@ -715,12 +728,19 @@ class @self.export.Namespace@Export @self.export.Name@ : public @self.export.Fat
#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&)
Expand Down Expand Up @@ -758,12 +778,19 @@ class @self.export.Namespace@Export @self.export.Name@ : public @self.export.Fat
#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
Expand All @@ -789,12 +816,18 @@ class @self.export.Namespace@Export @self.export.Name@ : public @self.export.Fat
#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&)
Expand Down

0 comments on commit fbca57b

Please sign in to comment.