From f9cf5cf779781d69be4401fc0b914c2a0f1e23fa Mon Sep 17 00:00:00 2001 From: jakirkham Date: Wed, 31 Jul 2024 02:51:12 -0700 Subject: [PATCH] Switch to `memoryview` --- python/pybind11/cucim_py.cpp | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/python/pybind11/cucim_py.cpp b/python/pybind11/cucim_py.cpp index b49853e56..6a8a6a3c6 100644 --- a/python/pybind11/cucim_py.cpp +++ b/python/pybind11/cucim_py.cpp @@ -19,8 +19,10 @@ #include #include +#include #include #include +#include #include #include @@ -444,10 +446,31 @@ py::object py_read_region(const CuImage& cuimg, { py::gil_scoped_acquire scope_guard; - auto arr = pybind11::array_t::ensure(location); - if (arr) // fast copy + py::memoryview mv; + try // fast copy { - py::buffer_info buf = arr.request(); + mv = py::memoryview(location); + } + catch (const std::exception& e) + { + mv = nullptr; + } + + if (mv) // fast copy + { + py::buffer_info buf = buffer_info(PyMemoryView_GET_BUFFER(mv.ptr()), false); + if (buf) + { + if (buf.format != 'q') + { + throw std::invalid_argument("Expected int64 array-like"); + } + if (PyBuffer_IsContiguous(buf.view(), 'C')) + { + throw std::invalid_argument("Expected C-contiguous array-like"); + } + } + int64_t* data_array = static_cast(buf.ptr); ssize_t data_size = buf.size; locations.reserve(data_size);