Skip to content

Commit

Permalink
Base: Imporove Python exception handling
Browse files Browse the repository at this point in the history
If the Python profiler is activated then transporting the dict as exception object is broken where InteractiveInterpreter::runCode() fails to restore it.
To fix the problem use Exception::setPyException() instead inside the macto PY_CATCH and the generated wrapper code.

This also improves exception handling in command line mode.
  • Loading branch information
wwmayer committed Jan 6, 2025
1 parent 26536c9 commit 7114dc6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
8 changes: 3 additions & 5 deletions src/Base/PyObjectBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,10 @@ BaseExport extern PyObject* PyExc_FC_AbortIOException;
#define PY_TRY try

#define __PY_CATCH(R) \
catch(Base::Exception &e) \
catch(const Base::Exception &e) \
{ \
auto pye = e.getPyExceptionType(); \
if(!pye) \
pye = Base::PyExc_FC_GeneralError; \
_Py_ErrorObj(R,pye,e.getPyObject()); \
e.setPyException(); \
R; \
} \
catch(const std::exception &e) \
{ \
Expand Down
27 changes: 9 additions & 18 deletions src/Tools/generateTemplates/templateClassPyExport.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,15 +622,12 @@ class @self.export.Namespace.replace("::","_")@Export @self.export.Name@ : publi
-
return ret;
} // Please sync the following catch implementation with PY_CATCH
catch(Base::Exception &e)
catch(const Base::Exception& e)
{
auto pye = e.getPyExceptionType();
if(!pye)
pye = Base::PyExc_FC_GeneralError;
PyErr_SetObject(pye, e.getPyObject());
e.setPyException();
return nullptr;
}
catch(const std::exception &e)
catch(const std::exception& e)
{
PyErr_SetString(Base::PyExc_FC_GeneralError, e.what());
return nullptr;
Expand Down Expand Up @@ -785,15 +782,12 @@ class @self.export.Namespace.replace("::","_")@Export @self.export.Name@ : publi
PyObject *r = getCustomAttributes(attr);
if(r) return r;
} // Please sync the following catch implementation with PY_CATCH
catch(Base::Exception &e)
catch(const Base::Exception& e)
{
auto pye = e.getPyExceptionType();
if(!pye)
pye = Base::PyExc_FC_GeneralError;
PyErr_SetObject(pye, e.getPyObject());
e.setPyException();
return nullptr;
}
catch(const std::exception &e)
catch(const std::exception& e)
{
PyErr_SetString(Base::PyExc_FC_GeneralError, e.what());
return nullptr;
Expand Down Expand Up @@ -835,15 +829,12 @@ class @self.export.Namespace.replace("::","_")@Export @self.export.Name@ : publi
else if (r == -1)
return -1;
} // Please sync the following catch implementation with PY_CATCH
catch(Base::Exception &e)
catch(const Base::Exception& e)
{
auto pye = e.getPyExceptionType();
if(!pye)
pye = Base::PyExc_FC_GeneralError;
PyErr_SetObject(pye, e.getPyObject());
e.setPyException();
return -1;
}
catch(const std::exception &e)
catch(const std::exception& e)
{
PyErr_SetString(Base::PyExc_FC_GeneralError, e.what());
return -1;
Expand Down

0 comments on commit 7114dc6

Please sign in to comment.