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
bytes.hpp
Go to the documentation of this file.
1//
2// bytes.hpp
3// core-adapters
4//
5// Copyright 2010-2026 Saxon Herschel Nicholls
6//
7
59
60#ifndef snicholls_core_adapters_nanobind_bytes_hpp
61#define snicholls_core_adapters_nanobind_bytes_hpp
62
63#include <nanobind/nanobind.h>
64
65#include <cstddef>
66#include <span>
67#include <string_view>
68#include <utility>
69
70namespace snicholls::python {
71
72namespace detail {
73
74// A held buffer (or a str's UTF-8), released with the GIL taken.
75class held_buffer
76{
77public:
78 held_buffer() noexcept = default;
79 held_buffer(const held_buffer&) = delete;
80 held_buffer& operator=(const held_buffer&) = delete;
81 held_buffer(held_buffer&& o) noexcept : buffer_(o.buffer_), held_(std::exchange(o.held_, false)),
82 text_(std::exchange(o.text_, nullptr)), data_(o.data_), size_(o.size_)
83 {
84 }
85 held_buffer& operator=(held_buffer&& o) noexcept
86 {
87 if (this != &o) {
88 release();
89 buffer_ = o.buffer_;
90 held_ = std::exchange(o.held_, false);
91 text_ = std::exchange(o.text_, nullptr);
92 data_ = o.data_;
93 size_ = o.size_;
94 }
95 return *this;
96 }
97 ~held_buffer() { release(); }
98
99 // A contiguous buffer; writable when asked. False, with no Python error left, when src has none.
100 bool acquire_buffer(PyObject* src, bool writable) noexcept
101 {
102 release();
103 if (PyUnicode_Check(src))
104 return false;
105 if (PyObject_GetBuffer(src, &buffer_, writable ? (PyBUF_SIMPLE | PyBUF_WRITABLE) : PyBUF_SIMPLE) != 0) {
106 PyErr_Clear();
107 return false;
108 }
109 held_ = true;
110 data_ = buffer_.buf;
111 size_ = static_cast<std::size_t>(buffer_.len);
112 return true;
113 }
114
115 // A str's UTF-8 (kept alive by a reference to the str), or a contiguous buffer.
116 bool acquire_text(PyObject* src) noexcept
117 {
118 release();
119 if (!PyUnicode_Check(src))
120 return acquire_buffer(src, false);
121 Py_ssize_t n = 0;
122 const char* s = PyUnicode_AsUTF8AndSize(src, &n);
123 if (s == nullptr) {
124 PyErr_Clear();
125 return false;
126 }
127 Py_INCREF(src);
128 text_ = src;
129 data_ = const_cast<char*>(s);
130 size_ = static_cast<std::size_t>(n);
131 return true;
132 }
133
134 [[nodiscard]] void* data() const noexcept { return data_; }
135 [[nodiscard]] std::size_t size() const noexcept { return size_; }
136
137private:
138 void release() noexcept
139 {
140 if (!held_ && text_ == nullptr)
141 return;
142 const PyGILState_STATE state = PyGILState_Ensure();
143 if (held_)
144 PyBuffer_Release(&buffer_);
145 Py_XDECREF(text_);
146 PyGILState_Release(state);
147 held_ = false;
148 text_ = nullptr;
149 data_ = nullptr;
150 size_ = 0;
151 }
152
153 Py_buffer buffer_{};
154 bool held_ = false;
155 PyObject* text_ = nullptr;
156 void* data_ = nullptr;
157 std::size_t size_ = 0;
158};
159
160} // namespace detail
161
165{
166public:
168 [[nodiscard]] std::span<const std::byte> bytes() const noexcept
169 {
170 return {static_cast<const std::byte*>(held_.data()), held_.size()};
171 }
173 [[nodiscard]] std::string_view chars() const noexcept { return {static_cast<const char*>(held_.data()), held_.size()}; }
175 [[nodiscard]] std::size_t size() const noexcept { return held_.size(); }
176
178 bool acquire(PyObject* src) noexcept { return held_.acquire_buffer(src, false); }
179
180private:
181 detail::held_buffer held_;
182};
183
187{
188public:
190 [[nodiscard]] std::span<std::byte> bytes() const noexcept { return {static_cast<std::byte*>(held_.data()), held_.size()}; }
192 [[nodiscard]] std::size_t size() const noexcept { return held_.size(); }
193
195 bool acquire(PyObject* src) noexcept { return held_.acquire_buffer(src, true); }
196
197private:
198 detail::held_buffer held_;
199};
200
204{
205public:
207 [[nodiscard]] std::string_view view() const noexcept { return {static_cast<const char*>(held_.data()), held_.size()}; }
209 [[nodiscard]] std::size_t size() const noexcept { return held_.size(); }
210
212 bool acquire(PyObject* src) noexcept { return held_.acquire_text(src); }
213
214private:
215 detail::held_buffer held_;
216};
217
224[[nodiscard]] inline ::nanobind::bytes to_bytes(std::span<const std::byte> b)
225{
226 return ::nanobind::bytes(b.data(), b.size());
227}
228
234[[nodiscard]] inline ::nanobind::bytes to_bytes(std::string_view s) { return ::nanobind::bytes(s.data(), s.size()); }
235
236} // namespace snicholls::python
237
238namespace nanobind::detail {
239
241template <class View, auto Descr>
242struct snicholls_view_caster
243{
244 NB_TYPE_CASTER(View, Descr)
245
246 bool from_python(handle src, uint32_t, cleanup_list*) noexcept { return value.acquire(src.ptr()); }
247
248 static handle from_cpp(const View&, rv_policy, cleanup_list*) noexcept
249 {
250 PyErr_SetString(PyExc_TypeError, "snicholls::python: a byte or text view is an argument type only");
251 return {};
252 }
253};
254
255template <>
256struct type_caster<snicholls::python::byte_view>
257 : snicholls_view_caster<snicholls::python::byte_view, const_name("collections.abc.Buffer")>
258{
259};
260
261template <>
262struct type_caster<snicholls::python::mutable_byte_view>
263 : snicholls_view_caster<snicholls::python::mutable_byte_view, const_name("collections.abc.Buffer")>
264{
265};
266
267template <>
268struct type_caster<snicholls::python::text_view>
269 : snicholls_view_caster<snicholls::python::text_view, const_name("str | collections.abc.Buffer")>
270{
271};
273
274} // namespace nanobind::detail
275
276#endif /* snicholls_core_adapters_nanobind_bytes_hpp */
A read-only view of a Python bytes-like object's contiguous buffer.
Definition bytes.hpp:165
bool acquire(PyObject *src) noexcept
Takes src's buffer (the type caster's step).
Definition bytes.hpp:178
std::string_view chars() const noexcept
The bytes as characters.
Definition bytes.hpp:173
std::span< const std::byte > bytes() const noexcept
The bytes.
Definition bytes.hpp:168
std::size_t size() const noexcept
The length in bytes.
Definition bytes.hpp:175
A writable view of a Python object's contiguous buffer.
Definition bytes.hpp:187
bool acquire(PyObject *src) noexcept
Takes src's writable buffer (the type caster's step).
Definition bytes.hpp:195
std::size_t size() const noexcept
The length in bytes.
Definition bytes.hpp:192
std::span< std::byte > bytes() const noexcept
The bytes.
Definition bytes.hpp:190
A view of a str's UTF-8, or of a bytes-like buffer taken as UTF-8.
Definition bytes.hpp:204
bool acquire(PyObject *src) noexcept
Takes src's UTF-8 or buffer (the type caster's step).
Definition bytes.hpp:212
std::string_view view() const noexcept
The text.
Definition bytes.hpp:207
std::size_t size() const noexcept
The length in bytes of UTF-8.
Definition bytes.hpp:209
::nanobind::bytes to_bytes(std::span< const std::byte > b)
A new Python bytes holding a copy of b.
Definition bytes.hpp:224