Directory: | ./ |
---|---|
File: | bindings/python/crocoddyl/core/utils/exception.hpp |
Date: | 2025-01-30 11:01:55 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 8 | 16 | 50.0% |
Branches: | 14 | 48 | 29.2% |
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 | 10 | static PyObject* createExceptionClass(const char* name, | |
19 | PyObject* base_type = PyExc_Exception) { | ||
20 | const std::string scope_name = | ||
21 |
6/12✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 10 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 10 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 10 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 10 times.
✗ Branch 17 not taken.
|
20 | bp::extract<std::string>(bp::scope().attr("__name__")); |
22 |
2/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
|
10 | const std::string qualified_name = scope_name + "." + name; |
23 |
1/2✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
|
10 | PyObject* type = PyErr_NewException(qualified_name.c_str(), base_type, 0); |
24 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (!type) { |
25 | ✗ | bp::throw_error_already_set(); | |
26 | } | ||
27 |
4/8✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 10 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 10 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 10 times.
✗ Branch 12 not taken.
|
10 | bp::scope().attr(name) = bp::handle<>(bp::borrowed(type)); |
28 | 10 | return type; | |
29 | 10 | } | |
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 |