-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1186 from cmastalli/topic/python-exception
- Loading branch information
Showing
6 changed files
with
89 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/////////////////////////////////////////////////////////////////////////////// | ||
// BSD 3-Clause License | ||
// | ||
// Copyright (C) 2023-2023, Heriot-Watt University | ||
// Copyright note valid unless otherwise stated in individual files. | ||
// All rights reserved. | ||
/////////////////////////////////////////////////////////////////////////////// | ||
|
||
#ifndef BINDINGS_PYTHON_CROCODDYL_CORE_EXCEPTION_HPP_ | ||
#define BINDINGS_PYTHON_CROCODDYL_CORE_EXCEPTION_HPP_ | ||
|
||
#include "crocoddyl/core/utils/exception.hpp" | ||
#include "python/crocoddyl/core/core.hpp" | ||
|
||
namespace crocoddyl { | ||
namespace python { | ||
|
||
static PyObject* createExceptionClass(const char* name, | ||
PyObject* base_type = PyExc_Exception) { | ||
const std::string scope_name = | ||
bp::extract<std::string>(bp::scope().attr("__name__")); | ||
const std::string qualified_name = scope_name + "." + name; | ||
PyObject* type = PyErr_NewException(qualified_name.c_str(), base_type, 0); | ||
if (!type) { | ||
bp::throw_error_already_set(); | ||
} | ||
bp::scope().attr(name) = bp::handle<>(bp::borrowed(type)); | ||
return type; | ||
} | ||
|
||
PyObject* ExceptionType = NULL; | ||
void translateException(Exception const& e) { | ||
bp::object exc_t(bp::handle<>(bp::borrowed(ExceptionType))); | ||
exc_t.attr("cause") = | ||
bp::object(e); // add the wrapped exception to the Python exception | ||
exc_t.attr("what") = bp::object(e.what()); // for convenience | ||
PyErr_SetString( | ||
ExceptionType, | ||
e.what()); // the string is used by print(exception) in python | ||
} | ||
|
||
} // namespace python | ||
} // namespace crocoddyl | ||
|
||
#endif // BINDINGS_PYTHON_CROCODDYL_CORE_EXCEPTION_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/////////////////////////////////////////////////////////////////////////////// | ||
// BSD 3-Clause License | ||
// | ||
// Copyright (C) 2023-2023, Heriot-Watt University | ||
// Copyright note valid unless otherwise stated in individual files. | ||
// All rights reserved. | ||
/////////////////////////////////////////////////////////////////////////////// | ||
|
||
#include "python/crocoddyl/core/utils/exception.hpp" | ||
|
||
namespace crocoddyl { | ||
namespace python { | ||
|
||
void exposeException() { | ||
bp::class_<Exception> ExceptionClass( | ||
"Exception", bp::init<std::string, const char *, const char *, int>( | ||
bp::args("self", "msg", "file", "func", "line"), | ||
"Initialize the Crocoddyl's exception.")); | ||
ExceptionClass.add_property("message", &Exception::getMessage) | ||
.add_property("extra_data", &Exception::getExtraData); | ||
|
||
ExceptionType = createExceptionClass("Exception"); | ||
bp::register_exception_translator<Exception>(&translateException); | ||
} | ||
|
||
} // namespace python | ||
} // namespace crocoddyl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters