| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /////////////////////////////////////////////////////////////////////////////// | ||
| 2 | // BSD 3-Clause License | ||
| 3 | // | ||
| 4 | // Copyright (C) 2023-2023, Heriot-Watt University | ||
| 5 | // Copyright note valid unless otherwise stated in individual files. | ||
| 6 | // All rights reserved. | ||
| 7 | /////////////////////////////////////////////////////////////////////////////// | ||
| 8 | |||
| 9 | #ifndef BINDINGS_PYTHON_CROCODDYL_CORE_EXCEPTION_HPP_ | ||
| 10 | #define BINDINGS_PYTHON_CROCODDYL_CORE_EXCEPTION_HPP_ | ||
| 11 | |||
| 12 | #include "crocoddyl/core/utils/exception.hpp" | ||
| 13 | #include "python/crocoddyl/core/core.hpp" | ||
| 14 | |||
| 15 | namespace crocoddyl { | ||
| 16 | namespace python { | ||
| 17 | |||
| 18 | ✗ | static PyObject* createExceptionClass(const char* name, | |
| 19 | PyObject* base_type = PyExc_Exception) { | ||
| 20 | const std::string scope_name = | ||
| 21 | ✗ | bp::extract<std::string>(bp::scope().attr("__name__")); | |
| 22 | ✗ | const std::string qualified_name = scope_name + "." + name; | |
| 23 | ✗ | PyObject* type = PyErr_NewException(qualified_name.c_str(), base_type, 0); | |
| 24 | ✗ | if (!type) { | |
| 25 | ✗ | bp::throw_error_already_set(); | |
| 26 | } | ||
| 27 | ✗ | bp::scope().attr(name) = bp::handle<>(bp::borrowed(type)); | |
| 28 | ✗ | return type; | |
| 29 | ✗ | } | |
| 30 | |||
| 31 | PyObject* ExceptionType = NULL; | ||
| 32 | ✗ | void translateException(Exception const& e) { | |
| 33 | ✗ | bp::object exc_t(bp::handle<>(bp::borrowed(ExceptionType))); | |
| 34 | ✗ | exc_t.attr("cause") = | |
| 35 | ✗ | bp::object(e); // add the wrapped exception to the Python exception | |
| 36 | ✗ | exc_t.attr("what") = bp::object(e.what()); // for convenience | |
| 37 | ✗ | PyErr_SetString( | |
| 38 | ExceptionType, | ||
| 39 | ✗ | e.what()); // the string is used by print(exception) in python | |
| 40 | ✗ | } | |
| 41 | |||
| 42 | } // namespace python | ||
| 43 | } // namespace crocoddyl | ||
| 44 | |||
| 45 | #endif // BINDINGS_PYTHON_CROCODDYL_CORE_EXCEPTION_HPP_ | ||
| 46 |