Base: Improve Python exception handling

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.
This commit is contained in:
wmayer
2025-01-06 15:14:02 +01:00
committed by Benjamin Nauck
parent e0027daf3b
commit 2c60e83f25
2 changed files with 12 additions and 23 deletions

View File

@@ -446,12 +446,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) \
{ \

View File

@@ -669,15 +669,12 @@ PyObject * @self.export.Name@::staticCallback_@i.Name@ (PyObject *self, PyObject
-
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;
@@ -832,15 +829,12 @@ PyObject *@self.export.Name@::_getattr(const char *attr) // __getattr__ functi
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;
@@ -882,15 +876,12 @@ int @self.export.Name@::_setattr(const char *attr, PyObject *value) // __setattr
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;