Base: [skip ci] add method to more easily set the Python error indicator from a Base::Exception

This commit is contained in:
wmayer
2020-03-18 17:53:13 +01:00
parent f585ab5d58
commit 49bfbf73f8
5 changed files with 32 additions and 3 deletions

View File

@@ -223,8 +223,8 @@ PyObject* Application::sLoadFile(PyObject * /*self*/, PyObject *args)
Py_Return;
}
catch (const Base::Exception& e) {
PyErr_SetString(PyExc_IOError, e.what());
return 0;
e.setPyException();
return nullptr;
}
catch (const std::exception& e) {
// might be subclass from zipios

View File

@@ -28,6 +28,7 @@
#include "Exception.h"
#include "Console.h"
#include "PyObjectBase.h"
#include <CXX/Objects.hxx>
FC_LOG_LEVEL_INIT("Exception", true, true)
@@ -148,6 +149,21 @@ void Exception::setPyObject( PyObject * pydict)
}
}
PyObject * Exception::getPyExceptionType() const
{
return BaseExceptionFreeCADError;
}
void Exception::setPyException() const
{
PyObject* exc = getPyExceptionType();
if (!exc) {
exc = BaseExceptionFreeCADError;
}
PyErr_SetString(exc, what());
}
// ---------------------------------------------------------
TYPESYSTEM_SOURCE(Base::AbortException,Base::Exception)

View File

@@ -126,7 +126,9 @@ public:
virtual void setPyObject( PyObject * pydict);
/// returns the corresponding python exception type
virtual PyObject * getPyExceptionType() const {return 0;}
virtual PyObject * getPyExceptionType() const;
/// Sets the Python error indicator and an error message
virtual void setPyException() const;
protected:
/* sMessage may be:

View File

@@ -146,6 +146,15 @@ void PyException::ReportException (void) const
}
}
void PyException::setPyException() const
{
std::stringstream str;
str << getStackTrace()
<< getErrorType()
<< ": " << what();
PyErr_SetString(getPyExceptionType(), str.str().c_str());
}
// ---------------------------------------------------------
SystemExitException::SystemExitException()

View File

@@ -113,6 +113,8 @@ public:
const std::string &getErrorType(void) const {return _errorType;}
virtual PyObject *getPyExceptionType(void) const override {return _exceptionType;}
void ReportException (void) const override;
/// Sets the Python error indicator and an error message
virtual void setPyException() const override;
protected:
std::string _stackTrace;