Helpers for snicholls Python extensions built with nanobind (target snicholls::core_nanobind).
More...
|
| file | bytes.hpp |
| | bytes, bytearray, memoryview and str as zero-copy C++ views, and C++ bytes back as Python bytes.
|
| |
| file | errors.hpp |
| | C++ errors to Python exceptions: core's result / traced errors to the snicholls_core.errors hierarchy.
|
| |
| file | gil.hpp |
| | The GIL: release it around C++ work, take it back to call Python, and a Python callable safe from any thread.
|
| |
| file | ndarray.hpp |
| | NumPy arrays in and out of a header-only C++ function, with the GIL released: the names and the template.
|
| |
| file | optional.hpp |
| | Values that may be missing: std::optional both ways, and a returned snicholls::result as value-or-raise.
|
| |
|
| using | snicholls::python::release_gil = ::nanobind::call_guard<::nanobind::gil_scoped_release > |
| | nanobind's call guard that releases the GIL for the duration of a bound call.
|
| |
| template<class T > |
| using | snicholls::python::vector_in = ::nanobind::ndarray< const T, ::nanobind::ndim< 1 >, ::nanobind::c_contig, ::nanobind::device::cpu > |
| | A read-only, C-contiguous, one-dimensional CPU array of T, as a function argument.
|
| |
| template<class T > |
| using | snicholls::python::matrix_in = ::nanobind::ndarray< const T, ::nanobind::ndim< 2 >, ::nanobind::c_contig, ::nanobind::device::cpu > |
| | A read-only, C-contiguous (row-major), two-dimensional CPU array of T, as a function argument.
|
| |
| template<class T > |
| using | snicholls::python::vector_inout = ::nanobind::ndarray< T, ::nanobind::ndim< 1 >, ::nanobind::c_contig, ::nanobind::device::cpu > |
| | A writable, C-contiguous, one-dimensional CPU array of T, for functions that work in place.
|
| |
| template<class T > |
| using | snicholls::python::numpy_vector = ::nanobind::ndarray<::nanobind::numpy, T, ::nanobind::ndim< 1 > > |
| | A one-dimensional NumPy array of T, as a result.
|
| |
| template<class T > |
| using | snicholls::python::numpy_matrix = ::nanobind::ndarray<::nanobind::numpy, T, ::nanobind::ndim< 2 >, ::nanobind::c_contig > |
| | A two-dimensional, row-major NumPy array of T, as a result.
|
| |
|
| ::nanobind::bytes | snicholls::python::to_bytes (std::span< const std::byte > b) |
| | A new Python bytes holding a copy of b.
|
| |
| ::nanobind::bytes | snicholls::python::to_bytes (std::string_view s) |
| | A new Python bytes holding a copy of s.
|
| |
| 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.
|
| |
| template<class F > |
| decltype(auto) | snicholls::python::without_gil (F &&f) |
| | Runs f() with the GIL released and returns its result.
|
| |
| template<class F > |
| decltype(auto) | snicholls::python::with_gil (F &&f) |
| | Runs f() holding the GIL (taking it if this thread does not hold it) and returns its result.
|
| |
| template<class... Args> |
| auto | snicholls::python::as_span (const ::nanobind::ndarray< Args... > &a) noexcept |
| | A span over a C-contiguous array's elements, all dimensions flattened in row-major order.
|
| |
| template<class T > |
| numpy_matrix< T > | snicholls::python::to_numpy (std::vector< T > &&values, std::size_t rows, std::size_t columns) |
| | A NumPy array of shape (rows, columns) owning values' buffer, without copying it.
|
| |
| template<class T > |
| numpy_vector< T > | snicholls::python::to_numpy (std::vector< T > &&values) |
| | A one-dimensional NumPy array owning values' buffer, without copying it.
|
| |
Helpers for snicholls Python extensions built with nanobind (target snicholls::core_nanobind).
◆ matrix_in
template<class T >
| using snicholls::python::matrix_in = typedef ::nanobind::ndarray<const T, ::nanobind::ndim<2>, ::nanobind::c_contig, ::nanobind::device::cpu> |
A read-only, C-contiguous (row-major), two-dimensional CPU array of T, as a function argument.
Definition at line 85 of file ndarray.hpp.
◆ numpy_matrix
A two-dimensional, row-major NumPy array of T, as a result.
Definition at line 100 of file ndarray.hpp.
◆ numpy_vector
A one-dimensional NumPy array of T, as a result.
Definition at line 95 of file ndarray.hpp.
◆ release_gil
nanobind's call guard that releases the GIL for the duration of a bound call.
m.def("f", &f, snicholls::python::release_gil()); - only for functions whose arguments and result are C++ values; see the file's gotchas.
Definition at line 92 of file gil.hpp.
◆ vector_in
template<class T >
| using snicholls::python::vector_in = typedef ::nanobind::ndarray<const T, ::nanobind::ndim<1>, ::nanobind::c_contig, ::nanobind::device::cpu> |
A read-only, C-contiguous, one-dimensional CPU array of T, as a function argument.
Definition at line 80 of file ndarray.hpp.
◆ vector_inout
A writable, C-contiguous, one-dimensional CPU array of T, for functions that work in place.
Definition at line 90 of file ndarray.hpp.
◆ as_span()
template<class... Args>
| auto snicholls::python::as_span |
( |
const ::nanobind::ndarray< Args... > & |
a | ) |
|
|
noexcept |
A span over a C-contiguous array's elements, all dimensions flattened in row-major order.
- Template Parameters
-
| Args | the ndarray's parameters; it must be C-contiguous (c_contig) and on the CPU. |
- Parameters
-
- Returns
- the span:
const elements for a read-only array.
Definition at line 108 of file ndarray.hpp.
◆ raise_error()
template<class E >
| void snicholls::python::raise_error |
( |
const E & |
e | ) |
|
Throws e as the C++ exception that maps to the right Python class.
A traced<X> becomes traced_exception (Python TracedError, with the location, frames and masked fields); an error that is itself a std::exception is thrown as it is (so std::out_of_range is an IndexError); anything else becomes bad_result_access<void> with snicholls::error_text(e) (Python ResultError).
- Template Parameters
-
- Parameters
-
- Exceptions
-
| as | described; std::bad_alloc. |
Definition at line 252 of file errors.hpp.
◆ register_exception_translators()
| void snicholls::python::register_exception_translators |
( |
| ) |
|
|
inline |
Registers the translators for core's errors with nanobind. Call once in each NB_MODULE body.
The standard exceptions keep nanobind's mapping (see the file's table).
Definition at line 229 of file errors.hpp.
◆ require()
| void snicholls::python::require |
( |
bool |
condition, |
|
|
const char * |
message |
|
) |
| |
|
inline |
Throws precondition_error(message) unless condition holds.
- Parameters
-
| condition | the precondition. |
| message | what was violated, for the Python message. |
- Exceptions
-
Definition at line 236 of file errors.hpp.
◆ set_python_error()
| void snicholls::python::set_python_error |
( |
const std::exception_ptr & |
p | ) |
|
|
inlinenoexcept |
Sets the Python error for a C++ exception, by the file's table. For type casters, which return a null handle instead of throwing.
- Parameters
-
- Precondition
- the GIL is held.
Definition at line 286 of file errors.hpp.
◆ to_bytes() [1/2]
| ::nanobind::bytes snicholls::python::to_bytes |
( |
std::span< const std::byte > |
b | ) |
|
|
inline |
A new Python bytes holding a copy of b.
- Parameters
-
- Returns
- the object.
- Exceptions
-
| std::bad_alloc | (via nanobind's python_error for MemoryError). |
- Precondition
- the GIL is held.
Definition at line 224 of file bytes.hpp.
◆ to_bytes() [2/2]
| ::nanobind::bytes snicholls::python::to_bytes |
( |
std::string_view |
s | ) |
|
|
inline |
A new Python bytes holding a copy of s.
- Parameters
-
- Returns
- the object.
- Precondition
- the GIL is held.
Definition at line 234 of file bytes.hpp.
◆ to_numpy() [1/2]
template<class T >
| numpy_vector< T > snicholls::python::to_numpy |
( |
std::vector< T > && |
values | ) |
|
A one-dimensional NumPy array owning values' buffer, without copying it.
- Template Parameters
-
- Parameters
-
| values | the elements; moved from. |
- Returns
- the array.
- Exceptions
-
- Precondition
- the GIL is held.
Definition at line 157 of file ndarray.hpp.
◆ to_numpy() [2/2]
template<class T >
| numpy_matrix< T > snicholls::python::to_numpy |
( |
std::vector< T > && |
values, |
|
|
std::size_t |
rows, |
|
|
std::size_t |
columns |
|
) |
| |
A NumPy array of shape (rows, columns) owning values' buffer, without copying it.
- Template Parameters
-
- Parameters
-
| values | the elements in row-major order; moved from. |
| rows | the rows. |
| columns | the columns. |
- Returns
- the array.
- Exceptions
-
| std::length_error | if rows * columns != values.size(); std::bad_alloc. |
- Precondition
- the GIL is held.
Definition at line 138 of file ndarray.hpp.
◆ unwrap()
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.
- Template Parameters
-
| V | the value type (void allowed). |
| E | the error type. |
- Parameters
-
- Returns
- the value, moved out.
- Exceptions
-
| as | raise_error when r holds an error. |
Definition at line 271 of file errors.hpp.
◆ with_gil()
template<class F >
| decltype(auto) snicholls::python::with_gil |
( |
F && |
f | ) |
|
Runs f() holding the GIL (taking it if this thread does not hold it) and returns its result.
- Template Parameters
-
| F | a callable taking no arguments. |
- Parameters
-
| f | the work, which may use Python. |
- Returns
- what
f() returns.
- Exceptions
-
| whatever | f throws, after the GIL state is restored. |
Definition at line 115 of file gil.hpp.
◆ without_gil()
template<class F >
| decltype(auto) snicholls::python::without_gil |
( |
F && |
f | ) |
|
Runs f() with the GIL released and returns its result.
- Template Parameters
-
| F | a callable taking no arguments; it must not touch Python objects. |
- Parameters
-
- Returns
- what
f() returns.
- Exceptions
-
| whatever | f throws, after the GIL is taken back. |
- Precondition
- the calling thread holds the GIL.
Definition at line 102 of file gil.hpp.