24 #include "pybind11/pybind11.h" 40 void tryLsstExceptionWarn(
const char *message) {
43 int s = PyErr_WarnEx(PyExc_Warning, message, 1);
63 void raiseLsstException(py::object &pyex) {
65 py::reinterpret_borrow<py::object>(PyImport_ImportModule(
"lsst.pex.exceptions.wrappers"));
67 tryLsstExceptionWarn(
"Failed to import C++ Exception wrapper module.");
70 py::reinterpret_borrow<py::object>(PyObject_GetAttrString(
module.ptr(),
"translate"));
72 tryLsstExceptionWarn(
"Failed to find translation function for C++ Exceptions.");
76 auto instance = py::reinterpret_steal<py::object>(
77 PyObject_CallFunctionObjArgs(
translate.ptr(), pyex.ptr(), NULL));
78 if (!instance.ptr()) {
80 tryLsstExceptionWarn(
"Failed to translate C++ Exception to Python.");
82 auto type = py::reinterpret_borrow<py::object>(PyObject_Type(instance.ptr()));
83 PyErr_SetObject(
type.ptr(), instance.ptr());
91 py::class_<Tracepoint> clsTracepoint(mod,
"Tracepoint");
93 clsTracepoint.def(py::init<char const *, int, char const *, std::string const &>())
99 py::class_<Exception> clsException(mod,
"Exception");
101 clsException.def(py::init<std::string const &>())
111 self.addToStream(stream);
116 s <<
"Exception('" <<
self.what() <<
"')";
120 py::class_<LogicError, Exception> clsLogicError(mod,
"LogicError");
121 clsLogicError.def(py::init<std::string const &>());
123 py::class_<NotFoundError, Exception> clsNotFoundError(mod,
"NotFoundError");
124 clsNotFoundError.def(py::init<std::string const &>());
126 py::class_<RuntimeError, Exception> clsRuntimeError(mod,
"RuntimeError");
127 clsRuntimeError.def(py::init<std::string const &>());
129 py::class_<IoError, RuntimeError> clsIoError(mod,
"IoError");
130 clsIoError.def(py::init<std::string const &>());
132 py::class_<OverflowError, RuntimeError> clsOverflowError(mod,
"OverflowError");
133 clsOverflowError.def(py::init<std::string const &>());
135 py::class_<RangeError, RuntimeError> clsRangeError(mod,
"RangeError");
136 clsRangeError.def(py::init<std::string const &>());
138 py::class_<TypeError, LogicError> clsTypeError(mod,
"TypeError");
139 clsTypeError.def(py::init<std::string const &>());
141 py::class_<UnderflowError, RuntimeError> clsUnderflowError(mod,
"UnderflowError");
142 clsUnderflowError.def(py::init<std::string const &>());
144 py::class_<DomainError, LogicError> clsDomainError(mod,
"DomainError");
145 clsDomainError.def(py::init<std::string const &>());
147 py::class_<InvalidParameterError, LogicError> clsInvalidParameterError(mod,
"InvalidParameterError");
148 clsInvalidParameterError.def(py::init<std::string const &>());
150 py::class_<LengthError, LogicError> clsLengthError(mod,
"LengthError");
151 clsLengthError.def(py::init<std::string const &>());
153 py::class_<OutOfRangeError, LogicError> clsOutOfRangeError(mod,
"OutOfRangeError");
154 clsOutOfRangeError.def(py::init<std::string const &>());
160 py::object current_exception;
161 current_exception = py::cast(e.
clone(), py::return_value_policy::take_ownership);
162 raiseLsstException(current_exception);
virtual char const * getType(void) const noexcept
Return the fully-specified C++ type of a pointer to the exception.
Provides consistent interface for LSST exceptions.
void addMessage(char const *file, int line, char const *func, std::string const &message)
Add a tracepoint and a message to an exception before rethrowing it (access via LSST_EXCEPT_ADD).
virtual char const * what(void) const noexcept
Return a character string summarizing this exception.
virtual std::ostream & addToStream(std::ostream &stream) const
Add a text representation of this exception, including its traceback with messages, to a stream.
A base class for image defects.
virtual Exception * clone(void) const
Return a copy of the exception as an Exception pointer.
Traceback const & getTraceback(void) const noexcept
Retrieve the list of tracepoints associated with an exception.
PYBIND11_MODULE(exceptions, mod)
T rethrow_exception(T... args)