core-adapters 0.1.1
Third-party adapters for snicholls-core: nanobind helpers, nlohmann/json hooks, the snicholls_core Python package
Loading...
Searching...
No Matches
snicholls::python - nanobind helpers

Helpers for snicholls Python extensions built with nanobind (target snicholls::core_nanobind). More...

Files

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.
 

Classes

class  snicholls::python::byte_view
 A read-only view of a Python bytes-like object's contiguous buffer. More...
 
class  snicholls::python::mutable_byte_view
 A writable view of a Python object's contiguous buffer. More...
 
class  snicholls::python::text_view
 A view of a str's UTF-8, or of a bytes-like buffer taken as UTF-8. More...
 
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...
 
class  snicholls::python::python_function< Signature >
 A Python callable as a C++ callable of a fixed signature, safe to copy, call and destroy on any thread. More...
 
class  snicholls::python::python_function< R(A...)>
 python_function for R(A...): takes the GIL, calls, converts the result to R. More...
 

Typedefs

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.
 

Functions

::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.
 

Detailed Description

Helpers for snicholls Python extensions built with nanobind (target snicholls::core_nanobind).

Typedef Documentation

◆ 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

template<class T >
using snicholls::python::numpy_matrix = typedef ::nanobind::ndarray<::nanobind::numpy, T, ::nanobind::ndim<2>, ::nanobind::c_contig>

A two-dimensional, row-major NumPy array of T, as a result.

Definition at line 100 of file ndarray.hpp.

◆ numpy_vector

template<class T >
using snicholls::python::numpy_vector = typedef ::nanobind::ndarray<::nanobind::numpy, T, ::nanobind::ndim<1> >

A one-dimensional NumPy array of T, as a result.

Definition at line 95 of file ndarray.hpp.

◆ release_gil

using snicholls::python::release_gil = typedef ::nanobind::call_guard<::nanobind::gil_scoped_release>

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

template<class T >
using snicholls::python::vector_inout = typedef ::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.

Definition at line 90 of file ndarray.hpp.

Function Documentation

◆ 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
Argsthe ndarray's parameters; it must be C-contiguous (c_contig) and on the CPU.
Parameters
athe array.
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
Ethe error type.
Parameters
ethe error.
Exceptions
asdescribed; 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
conditionthe precondition.
messagewhat was violated, for the Python message.
Exceptions
precondition_errorif !condition.

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
pthe exception.
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
bthe bytes.
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
sthe characters.
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
Tthe element type.
Parameters
valuesthe elements; moved from.
Returns
the array.
Exceptions
std::bad_alloc.
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
Tthe element type.
Parameters
valuesthe elements in row-major order; moved from.
rowsthe rows.
columnsthe columns.
Returns
the array.
Exceptions
std::length_errorif 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
Vthe value type (void allowed).
Ethe error type.
Parameters
rthe result.
Returns
the value, moved out.
Exceptions
asraise_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
Fa callable taking no arguments.
Parameters
fthe work, which may use Python.
Returns
what f() returns.
Exceptions
whateverf 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
Fa callable taking no arguments; it must not touch Python objects.
Parameters
fthe work.
Returns
what f() returns.
Exceptions
whateverf throws, after the GIL is taken back.
Precondition
the calling thread holds the GIL.

Definition at line 102 of file gil.hpp.