|
core-adapters 0.1.1
Third-party adapters for snicholls-core: nanobind helpers, nlohmann/json hooks, the snicholls_core Python package
|
C++ errors to Python exceptions: core's result / traced errors to the snicholls_core.errors hierarchy.
More...
#include <nanobind/nanobind.h>#include <snicholls/core/result.hpp>#include <snicholls/core/traced.hpp>#include <exception>#include <new>#include <stdexcept>#include <string>#include <type_traits>#include <utility>Go to the source code of this file.
Classes | |
| class | snicholls::python::precondition_error |
A violated precondition of a bound function: snicholls_core.errors.PreconditionError (a ValueError). More... | |
| class | snicholls::python::traced_exception |
A snicholls::traced<E> error on its way to Python: snicholls_core.errors.TracedError. More... | |
Functions | |
| void | snicholls::python::register_exception_translators () |
Registers the translators for core's errors with nanobind. Call once in each NB_MODULE body. | |
| void | snicholls::python::require (bool condition, const char *message) |
Throws precondition_error(message) unless condition holds. | |
| template<class E > | |
| void | snicholls::python::raise_error (const E &e) |
Throws e as the C++ exception that maps to the right Python class. | |
| template<class V , class E > | |
| decltype(auto) | snicholls::python::unwrap (result< V, E > &&r) |
The value of a result, or raise_error of its error. | |
| void | snicholls::python::set_python_error (const std::exception_ptr &p) noexcept |
| Sets the Python error for a C++ exception, by the file's table. For type casters, which return a null handle instead of throwing. | |
C++ errors to Python exceptions: core's result / traced errors to the snicholls_core.errors hierarchy.
snicholls::python::register_exception_translators() once in each module's NB_MODULE body. Then:| C++ | Python |
|---|---|
snicholls::python::traced_exception (from a snicholls::traced<E> error) | snicholls_core.errors.TracedError |
snicholls::bad_result_access<E> (any E: unwrap() / expect() of an error) | snicholls_core.errors.ResultError |
snicholls::python::precondition_error (require(...)) | snicholls_core.errors.PreconditionError (also a ValueError) |
std::bad_alloc | MemoryError |
std::invalid_argument, std::domain_error, std::length_error | ValueError |
std::out_of_range | IndexError |
std::overflow_error | OverflowError |
any other std::exception | RuntimeError |
The last five rows are nanobind's own defaults, kept and tested. The Python classes live in pure Python (snicholls_core/errors.py) and are looked up by import, so every module shares them; a module used without the snicholls_core package installed falls back to the builtin in the second column's base (ValueError, RuntimeError).
For values: raise_error(e) throws the right C++ exception for any core error type, and unwrap(r) returns the value of a snicholls::result<V, E> or raises; optional.hpp makes a returned result do this by itself.
snicholls::result, snicholls::bad_result_access, snicholls::traced, snicholls::error_report (core).optional.hpp (the result type caster), gil.hpp.traced_exception carries the error report, with fields already masked by core; a secret never reaches Python.set_python_error never throws (it reports a failure to build the exception as that Python error instead).require(false, ...) throws precondition_error; raise_error(e) throws as described.snicholls_core.errors is done once per class.scripts/demos/errors_demo.sh.snicholls::core_nanobind; core's result.hpp and traced.hpp.SNICHOLLS_RESULT_THROW=1 (core's default with exceptions); with 0, unwrap() of an error terminates the interpreter instead of raising.snicholls_core must be importable then, not at module import.Definition in file errors.hpp.