diff --git a/src/App/ApplicationPy.cpp b/src/App/ApplicationPy.cpp index 44687ac0ad..e4acb4d607 100644 --- a/src/App/ApplicationPy.cpp +++ b/src/App/ApplicationPy.cpp @@ -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 diff --git a/src/Base/Exception.cpp b/src/Base/Exception.cpp index 33b597880a..048b0baf46 100644 --- a/src/Base/Exception.cpp +++ b/src/Base/Exception.cpp @@ -28,6 +28,7 @@ #include "Exception.h" #include "Console.h" +#include "PyObjectBase.h" #include 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) diff --git a/src/Base/Exception.h b/src/Base/Exception.h index bb9cb05ec1..2f979c5152 100644 --- a/src/Base/Exception.h +++ b/src/Base/Exception.h @@ -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: diff --git a/src/Base/Interpreter.cpp b/src/Base/Interpreter.cpp index 42d6722e09..ad7175014e 100644 --- a/src/Base/Interpreter.cpp +++ b/src/Base/Interpreter.cpp @@ -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() diff --git a/src/Base/Interpreter.h b/src/Base/Interpreter.h index afd232c23f..34b3dc8bd9 100644 --- a/src/Base/Interpreter.h +++ b/src/Base/Interpreter.h @@ -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;