Skip to content

Commit

Permalink
Use py::object to hold py::memoryview
Browse files Browse the repository at this point in the history
As `py::memoryview` must be assigned and we cannot guarantee it will
assign without error, declare `py::object` and place the results of
`py::memoryview` (if successful) into `py::object`. Can reconstruct the
`py::memoryview` later to get the actual object out.
  • Loading branch information
jakirkham committed Jul 31, 2024
1 parent d7adb6a commit 11405d1
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions python/pybind11/cucim_py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,20 +447,20 @@ py::object py_read_region(const CuImage& cuimg,
{
py::gil_scoped_acquire scope_guard;

py::memoryview mv;
bool has_mv = false;
py::object mv_obj(py::none());
try
{
mv = py::memoryview(location);
has_mv = true;
mv_obj = py::memoryview(location);
}
catch (const std::exception& e)
{
}

if (has_mv) // fast copy
if (!mv_obj.is_none()) // fast copy
{
py::buffer_info buf = py::buffer_info(PyMemoryView_GET_BUFFER(mv.ptr()), false);
py::memoryview mv(mv_obj);
py::buffer_info buf(PyMemoryView_GET_BUFFER(mv.ptr()), false);

if (buf.format != py::format_descriptor<int64_t>::format())
{
throw std::invalid_argument("Expected int64 array-like");
Expand Down

0 comments on commit 11405d1

Please sign in to comment.