Base: [skip ci] add method to more easily set the Python error indicator from a Base::Exception
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user