Skip to content
Discussion options

You must be logged in to vote

Yes, a NumPy-free view is possible. The missing piece is an exporter object that implements Python's buffer protocol and retains your owner. memoryview::from_buffer() wraps a raw pointer without an owner argument; its documentation explicitly leaves the buffer lifetime to you.

For example, using double for T and Table for your existing C++ class:

#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) {
…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by henryiii
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants