From bf2510ee8650fb5b340c47c3a46a5ae42b732b27 Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Mon, 18 Jul 2016 22:24:53 +0100 Subject: [PATCH] Make buffer_info::as_pybuffer a memoryview ctor --- include/pybind11/common.h | 24 ------------------------ include/pybind11/pytypes.h | 25 ++++++++++++++++++++++--- 2 files changed, 22 insertions(+), 27 deletions(-) diff --git a/include/pybind11/common.h b/include/pybind11/common.h index 32e8abf53..302d75a60 100644 --- a/include/pybind11/common.h +++ b/include/pybind11/common.h @@ -232,30 +232,6 @@ struct buffer_info { 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_strides { }; - static std::vector py_shape { }; - buf.buf = ptr; - buf.itemsize = (Py_ssize_t) itemsize; - buf.format = const_cast(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: Py_buffer *view = nullptr; }; diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 7a6cf6fbd..db87b088a 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -572,10 +572,29 @@ public: class memoryview : public object { public: - memoryview(const buffer_info& info) : memoryview(&info.as_pybuffer()) { } + memoryview(const buffer_info& info) { + static Py_buffer buf { }; + // Py_buffer uses signed sizes, strides and shape!.. + static std::vector py_strides { }; + static std::vector py_shape { }; + buf.buf = info.ptr; + buf.itemsize = (Py_ssize_t) info.itemsize; + buf.format = const_cast(info.format.c_str()); + buf.ndim = (int) info.ndim; + buf.len = (Py_ssize_t) info.size; + py_strides.clear(); + py_shape.clear(); + for (size_t i = 0; i < info.ndim; ++i) { + py_strides.push_back((Py_ssize_t) info.strides[i]); + py_shape.push_back((Py_ssize_t) info.shape[i]); + } + buf.strides = py_strides.data(); + buf.shape = py_shape.data(); + buf.suboffsets = nullptr; + buf.readonly = false; + buf.internal = nullptr; - memoryview(Py_buffer* view) - : object(PyMemoryView_FromBuffer(view), false) { + m_ptr = PyMemoryView_FromBuffer(&buf); if (!m_ptr) pybind11_fail("Unable to create memoryview from buffer descriptor"); }