core-adapters 0.1.3
Third-party adapters for snicholls-core: nanobind helpers, nlohmann/json hooks, the snicholls_core Python package
Loading...
Searching...
No Matches
errors.hpp File Reference

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.
 

Detailed Description

C++ errors to Python exceptions: core's result / traced errors to the snicholls_core.errors hierarchy.

Overview
One mapping for every snicholls extension module, so a Python caller catches the same classes whichever module raised. Call 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.

NB_MODULE(errors_demo_module, m)
{
m.def("price", [](double spot) {
py::require(spot == spot, "spot is NaN"); // PreconditionError (a ValueError)
return py::unwrap(price(spot)); // the value, or TracedError
}, "spot"_a);
m.def("lookup", [](int i) {
if (i > 2)
throw std::out_of_range("no instrument " + std::to_string(i)); // IndexError
return i * 10;
}, "i"_a);
}
void register_exception_translators()
Registers the translators for core's errors with nanobind. Call once in each NB_MODULE body.
Definition errors.hpp:229
Relationships
Guarantees and contracts
  • Registration is idempotent in effect: registering again adds a translator that gives the same answer.
  • 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).
Errors
The point of the header. require(false, ...) throws precondition_error; raise_error(e) throws as described.
Performance
Not on a hot path: only failing calls pay. The import of snicholls_core.errors is done once per class.
Tests and demos
  • Tests: tests/nanobind/errors_test.cpp (the test module), tests/nanobind/errors_test.py.
  • Demo: demos/nanobind/errors_demo.cpp, demos/nanobind/errors_demo.py; scripts/demos/errors_demo.sh.
Dependencies
CMake target snicholls::core_nanobind; core's result.hpp and traced.hpp.
Known issues and gotchas
  • Build every module with SNICHOLLS_RESULT_THROW=1 (core's default with exceptions); with 0, unwrap() of an error terminates the interpreter instead of raising.
  • The classes are looked up at the first error, so snicholls_core must be importable then, not at module import.
Status
Experimental, pre-1.0.

Definition in file errors.hpp.