From 7709d6b77dceb925b8f41819bc3e3b1b67c7cfca Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Sun, 19 Jun 2016 14:40:30 +0100 Subject: [PATCH] Add memoryview type --- include/pybind11/pytypes.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 0a0f50878..7a6cf6fbd 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -570,6 +570,19 @@ public: } }; +class memoryview : public object { +public: + memoryview(const buffer_info& info) : memoryview(&info.as_pybuffer()) { } + + memoryview(Py_buffer* view) + : object(PyMemoryView_FromBuffer(view), false) { + if (!m_ptr) + pybind11_fail("Unable to create memoryview from buffer descriptor"); + } + + PYBIND11_OBJECT_DEFAULT(memoryview, object, PyMemoryView_Check) +}; + inline size_t len(handle h) { ssize_t result = PyObject_Length(h.ptr()); if (result < 0)