28 #ifndef TOMOGRAPHERPY_EXC_H 29 #define TOMOGRAPHERPY_EXC_H 34 #include <tomographerpy/common.h> 40 #ifndef TOMOGRAPHER_PARSED_BY_DOXYGEN 58 namespace tomo_internal {
62 template<
typename Type>
69 m_ptr = PyErr_NewExceptionWithDoc(const_cast<char*>(full_name.
c_str()),
70 const_cast<char*>(docstring.c_str()),
73 if (py::hasattr(scope, name)) {
74 py::pybind11_fail(
"Error during initialization: multiple incompatible " 75 "definitions with name \"" +
std::string(name) +
"\"");
77 scope.attr(name) = *
this;
81 void operator()(
const char *message) {
82 PyErr_SetString(m_ptr, message);
92 template <
typename CppException>
96 PyObject *base = PyExc_Exception,
104 }
catch (
const CppException &e) {
120 PyObject *ptraceback;
125 : ptype(NULL), pvalue(NULL), ptraceback(NULL),
126 msg(
"<catch tpy::PyFetchedException and call restorePyException() to set Python exception>")
128 PyErr_Fetch(&ptype, &pvalue, &ptraceback) ;
134 ptraceback(copy.ptraceback)
138 void restorePyException()
140 PyErr_Restore(ptype, pvalue, ptraceback);
146 const char *
what()
const noexcept {
return msg.
c_str(); }
tomo_internal::ExceptionWithDocstring< CppException > & registerExceptionWithDocstring(py::handle scope, const char *name, PyObject *base=PyExc_Exception, std::string docstring=std::string())
Helper to register in Python a C++ exception with a docstring.
Helper for catching exceptions in a thread and re-raising them.
Base exception class for errors in the tomographer python module.
#define TOMOGRAPHER_DEFINE_MSG_EXCEPTION_BASE(ClassName, ErrPrefix, BaseClass)
Define a simple exception class.
C++ Classes and Utilities for Python Modules.
T rethrow_exception(T... args)