Closed
Description
It would be nice to have a pybind11::bytearray
type for directly manipulating bytearray
objects, similar to pybind11::bytes
. This way a wrapper function could allocate and return a native mutable Python buffer. A toy example:
#include <pybind11/pybind11.h>
namespace py = pybind11;
extern size_t decompressed_size(const void *in, size_t in_size);
extern int decompress(const void *in, size_t in_size, void *out, size_t out_size);
PYBIND11_MODULE(mycompression, m) {
m.def("decompress_mutable", [](py::buffer compressed_data) {
py::buffer_info in_inf = compressed_data.request();
py::bytearray out(decompressed_size(in_inf.ptr, in_inf.len));
(void) decompress(in_inf.ptr, in_inf.size, out.data(), out.size());
return out;
});
}
This example borrows the idea of exposing data()
and size()
methods on the py::bytearray
type which was suggested for py::bytes
in #2517. These could use PyByteArray_AsString
and PyByteArray_GetSize
with appropriate error checking under the hood.
If deemed worthwhile, this should be simple to implement using the PyByteArray_*
family of functions. These functions mirror the PyBytes_*
family currently in use for pybind11::bytes
, the definition of which is only some 30 lines of code.
Metadata
Metadata
Assignees
Labels
No labels