Repository navigation
How to expose raw memory without numpy #5861
Replies: 1 comment
|
Yes, a NumPy-free view is possible. The missing piece is an exporter object that implements Python's buffer protocol and retains your owner. For example, using #include <pybind11/pybind11.h>
#include <cstddef>
#include <utility>
namespace py = pybind11;
struct ColumnView {
py::object owner;
const double* data;
py::ssize_t size;
};
// Inside PYBIND11_MODULE, before binding get_memview:
py::class_<ColumnView>(m, "_ColumnView", py::buffer_protocol())
.def_buffer([](const ColumnView& v) {
return py::buffer_info(v.data, v.size, true); // readonly
});
// Add this .def to your existing Table binding:
.def("get_memview", [](py::object self, std::size_t i) {
const auto& column = self.cast<const Table&>().columns.at(i);
return py::memoryview(py::cast(ColumnView{
std::move(self), column.data(),
static_cast<py::ssize_t>(column.size())
}));
})The reference chain is v = table.get_memview(0)
s = v[1:]
del v, table
print(s.tolist()) # owner is still retainedAvoid relying solely on Keep the selected vector's size, allocation and element lifetimes stable until every view is released: no resize, erase, clear or replacement. Synchronize any C++ element updates with readers. Keeping This pattern was compiled with pybind11 3.1.0 and Python 3.14.7, without importing NumPy. Checks covered pointer identity, visible C++ element updates, empty columns, read-only behavior, and owner lifetime through slices/casts after releasing the original view. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I have a situation similar to what is described in #2271. In short, I expose a C++ object that has a
std:vector<std::vector<T>> columnsattribute. A member function taking an index returns the selected nested vector as a numpy array:Out of curiosity, I am trying to do it without the numpy dependency. I believe it means my
get_arrayfunction should now return apy::memoryviewinstance:My problem is that I can't find how to pass the
py::cast(*this)reference to tie the lifetime ofthisand the memoryview object. I have reached the point where I doubt memoryviews were design to do what I am trying to do and am wondering what I should use instead or if it is even possible to not use numpy arrays.All reactions