GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: bindings/python/crocoddyl/core/utils/exception.hpp Lines: 7 15 46.7 %
Date: 2024-02-13 11:12:33 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



30
      bp::extract<std::string>(bp::scope().attr("__name__"));
22

10
  const std::string qualified_name = scope_name + "." + name;
23
10
  PyObject* type = PyErr_NewException(qualified_name.c_str(), base_type, 0);
24
10
  if (!type) {
25
    bp::throw_error_already_set();
26
  }
27


10
  bp::scope().attr(name) = bp::handle<>(bp::borrowed(type));
28
20
  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_