mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-25 22:52:01 +00:00
Add buffer_info::as_pybuffer() method
This commit is contained in:
parent
3dd325b772
commit
a7e62e1ca6
@ -231,6 +231,31 @@ struct buffer_info {
|
|||||||
~buffer_info() {
|
~buffer_info() {
|
||||||
if (view) { PyBuffer_Release(view); delete view; }
|
if (view) { PyBuffer_Release(view); delete view; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Py_buffer& as_pybuffer() const {
|
||||||
|
static Py_buffer buf { };
|
||||||
|
// Py_buffer uses signed sizes, strides and shape!..
|
||||||
|
static std::vector<Py_ssize_t> py_strides { };
|
||||||
|
static std::vector<Py_ssize_t> py_shape { };
|
||||||
|
buf.buf = ptr;
|
||||||
|
buf.itemsize = (Py_ssize_t) itemsize;
|
||||||
|
buf.format = const_cast<char *>(format.c_str());
|
||||||
|
buf.ndim = (int) ndim;
|
||||||
|
buf.len = (Py_ssize_t) size;
|
||||||
|
py_strides.clear();
|
||||||
|
py_shape.clear();
|
||||||
|
for (size_t i = 0; i < ndim; ++i) {
|
||||||
|
py_strides.push_back((Py_ssize_t) strides[i]);
|
||||||
|
py_shape.push_back((Py_ssize_t) shape[i]);
|
||||||
|
}
|
||||||
|
buf.strides = py_strides.data();
|
||||||
|
buf.shape = py_shape.data();
|
||||||
|
buf.suboffsets = nullptr;
|
||||||
|
buf.readonly = false;
|
||||||
|
buf.internal = nullptr;
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Py_buffer *view = nullptr;
|
Py_buffer *view = nullptr;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user