1 #include "pybind11/pybind11.h"
8 using namespace lsst::pex::exceptions;
10 namespace py = pybind11;
14 namespace exceptions {
17 void tryLsstExceptionWarn(
const char *message) {
20 int s = PyErr_WarnEx(PyExc_Warning, message, 1);
36 void raiseLsstException(py::object &pyex) {
38 py::reinterpret_borrow<py::object>(PyImport_ImportModule(
"lsst.pex.exceptions.wrappers"));
40 tryLsstExceptionWarn(
"Failed to import C++ Exception wrapper module.");
43 py::reinterpret_borrow<py::object>(PyObject_GetAttrString(module.ptr(),
"translate"));
45 tryLsstExceptionWarn(
"Failed to find translation function for C++ Exceptions.");
49 auto instance = py::reinterpret_steal<py::object>(
50 PyObject_CallFunctionObjArgs(
translate.ptr(), pyex.ptr(), NULL));
51 if (!instance.ptr()) {
53 tryLsstExceptionWarn(
"Failed to translate C++ Exception to Python.");
55 auto type = py::reinterpret_borrow<py::object>(PyObject_Type(instance.ptr()));
56 PyErr_SetObject(type.ptr(), instance.ptr());
64 py::module mod(
"exceptions");
66 py::class_<Tracepoint> clsTracepoint(mod,
"Tracepoint");
68 clsTracepoint.def(py::init<char const *, int, char const *, std::string const &>())
74 py::class_<Exception> clsException(mod,
"Exception");
76 clsException.def(py::init<std::string const &>())
85 std::ostringstream stream;
86 self.addToStream(stream);
89 .def(
"__repr__", [](
Exception &
self) -> std::string {
91 s <<
"Exception('" <<
self.what() <<
"')";
95 py::class_<LogicError, Exception> clsLogicError(mod,
"LogicError");
96 clsLogicError.def(py::init<std::string const &>());
98 py::class_<NotFoundError, Exception> clsNotFoundError(mod,
"NotFoundError");
99 clsNotFoundError.def(py::init<std::string const &>());
101 py::class_<RuntimeError, Exception> clsRuntimeError(mod,
"RuntimeError");
102 clsRuntimeError.def(py::init<std::string const &>());
104 py::class_<IoError, RuntimeError> clsIoError(mod,
"IoError");
105 clsIoError.def(py::init<std::string const &>());
107 py::class_<MemoryError, RuntimeError> clsMemoryError(mod,
"MemoryError");
108 clsMemoryError.def(py::init<std::string const &>());
110 py::class_<OverflowError, RuntimeError> clsOverflowError(mod,
"OverflowError");
111 clsOverflowError.def(py::init<std::string const &>());
113 py::class_<RangeError, RuntimeError> clsRangeError(mod,
"RangeError");
114 clsRangeError.def(py::init<std::string const &>());
116 py::class_<TimeoutError, RuntimeError> clsTimeoutError(mod,
"TimeoutError");
117 clsTimeoutError.def(py::init<std::string const &>());
119 py::class_<TypeError, RuntimeError> clsTypeError(mod,
"TypeError");
120 clsTypeError.def(py::init<std::string const &>());
122 py::class_<UnderflowError, RuntimeError> clsUnderflowError(mod,
"UnderflowError");
123 clsUnderflowError.def(py::init<std::string const &>());
125 py::class_<DomainError, LogicError> clsDomainError(mod,
"DomainError");
126 clsDomainError.def(py::init<std::string const &>());
128 py::class_<InvalidParameterError, LogicError> clsInvalidParameterError(mod,
"InvalidParameterError");
129 clsInvalidParameterError.def(py::init<std::string const &>());
131 py::class_<LengthError, LogicError> clsLengthError(mod,
"LengthError");
132 clsLengthError.def(py::init<std::string const &>());
134 py::class_<OutOfRangeError, LogicError> clsOutOfRangeError(mod,
"OutOfRangeError");
135 clsOutOfRangeError.def(py::init<std::string const &>());
137 py::register_exception_translator([](std::exception_ptr p) {
139 if (p) std::rethrow_exception(p);
141 py::object current_exception;
142 current_exception = py::cast(e.
clone(), py::return_value_policy::take_ownership);
143 raiseLsstException(current_exception);
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
Return a character string summarizing this exception.
PYBIND11_PLUGIN(exceptions)
virtual std::ostream & addToStream(std::ostream &stream) const
Add a text representation of this exception, including its traceback with messages, to a stream.
Traceback const & getTraceback(void) const
Retrieve the list of tracepoints associated with an exception.
virtual Exception * clone(void) const
Return a copy of the exception as an Exception*.
virtual char const * getType(void) const
Return the fully-specified C++ type of a pointer to the exception.