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

bytes, bytearray, memoryview and str as zero-copy C++ views, and C++ bytes back as Python bytes. More...

#include <nanobind/nanobind.h>
#include <cstddef>
#include <span>
#include <string_view>
#include <utility>

Go to the source code of this file.

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

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.
 

Detailed Description

bytes, bytearray, memoryview and str as zero-copy C++ views, and C++ bytes back as Python bytes.

Overview
Core reads bytes as std::span<const std::byte> and text as std::string_view. These argument types give a binding exactly that, with no copy, from whatever a Python caller has:
C++ argument accepts gives
snicholls::python::byte_view bytes, bytearray, memoryview, array.array, a C-contiguous NumPy array - anything with a contiguous buffer std::span<const std::byte>
snicholls::python::mutable_byte_view a writable contiguous buffer (bytearray, writable memoryview, NumPy) std::span<std::byte>
snicholls::python::text_view str (its UTF-8), and the bytes-like types above (taken as UTF-8 as they are) std::string_view

str is deliberately not a byte_view: text has no bytes until an encoding is chosen, so a caller writes s.encode(). Back to Python, to_bytes(span) / to_bytes(string_view) copy into a new bytes; a std::string or std::string_view result is a str as usual (<nanobind/stl/string.h>).

// Any bytes-like object, no copy: a core function over bytes sees a std::span<const std::byte>
m.def("fnv1a_64", [](const snicholls::python::byte_view& b) { return snicholls::fnv1a_64(b.chars()); }, "data"_a);
// Text: a str's UTF-8 (or bytes as they are), as a std::string_view
m.def("shout", [](const snicholls::python::text_view& t) { return std::string(t.view()) + "!"; }, "text"_a);
// Writable buffers, changed in place
for (std::byte& x : b.bytes())
x = std::byte{0};
}, "buffer"_a);
// Bytes back to Python
m.def("header", [](std::uint32_t length) {
const std::byte h[4] = {std::byte(length >> 24), std::byte(length >> 16), std::byte(length >> 8), std::byte(length)};
return snicholls::python::to_bytes(std::span<const std::byte>(h));
}, "length"_a);
A read-only view of a Python bytes-like object's contiguous buffer.
Definition bytes.hpp:165
std::string_view chars() const noexcept
The bytes as characters.
Definition bytes.hpp:173
A writable view of a Python object's contiguous buffer.
Definition bytes.hpp:187
A view of a str's UTF-8, or of a bytes-like buffer taken as UTF-8.
Definition bytes.hpp:204
std::string_view view() const noexcept
The text.
Definition bytes.hpp:207
::nanobind::bytes to_bytes(std::span< const std::byte > b)
A new Python bytes holding a copy of b.
Definition bytes.hpp:224
Relationships
Guarantees and contracts
  • The views hold the buffer (or the str) for their own lifetime, so the memory stays valid and, for a bytearray, its size stays fixed while a view exists (Python raises BufferError on a resize).
  • The views may be destroyed with or without the GIL: they take it to release the buffer.
  • Move-only.
Errors
An argument of another type, or a non-contiguous buffer, fails overload resolution (TypeError); a read-only buffer fails for mutable_byte_view. to_bytes throws std::bad_alloc (MemoryError).
Performance
O(1) for the views (one buffer request); to_bytes copies once.
Tests and demos
  • Tests: tests/nanobind/bytes_test.cpp (the test module), tests/nanobind/bytes_test.py.
  • Demo: demos/nanobind/bytes_demo.cpp, demos/nanobind/bytes_demo.py; scripts/demos/bytes_demo.sh.
Dependencies
CMake target snicholls::core_nanobind. The buffer protocol is in the stable ABI from Python 3.11, so these work in abi3 modules.
Known issues and gotchas
  • Take the views by value or const& only in the function signature; never store one beyond the call.
  • A text_view of bytes is not checked for valid UTF-8, exactly as std::string_view would not be.
Status
Experimental, pre-1.0.

Definition in file bytes.hpp.