Skip to content

Commit 8f3b359

Browse files
committed
Fix zero-sized storeChunk for Span API in Python
1 parent a258128 commit 8f3b359

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

src/binding/python/RecordComponent.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <pybind11/detail/common.h>
2323
#include <pybind11/numpy.h>
2424
#include <pybind11/pybind11.h>
25+
#include <pybind11/pytypes.h>
2526
#include <pybind11/stl.h>
2627

2728
#include "openPMD/Dataset.hpp"
@@ -529,7 +530,7 @@ struct PythonDynamicMemoryView
529530
, m_datatype(determineDatatype<T>())
530531
{}
531532

532-
[[nodiscard]] pybind11::memoryview currentView() const;
533+
[[nodiscard]] pybind11::object currentView() const;
533534

534535
std::shared_ptr<void> m_dynamicView;
535536
ShapeContainer m_arrayShape;
@@ -542,30 +543,40 @@ namespace
542543
struct GetCurrentView
543544
{
544545
template <typename T>
545-
static pybind11::memoryview call(PythonDynamicMemoryView const &dynamicView)
546+
static pybind11::object call(PythonDynamicMemoryView const &dynamicView)
546547
{
547548
auto span =
548549
static_cast<DynamicMemoryView<T> *>(dynamicView.m_dynamicView.get())
549550
->currentBuffer();
550-
return py::memoryview::from_buffer(
551-
span.data(),
552-
dynamicView.m_arrayShape,
553-
dynamicView.m_strides,
554-
/* readonly = */ false);
551+
if (!span.data())
552+
{
553+
return py::array(
554+
dtype_to_numpy(dynamicView.m_datatype),
555+
dynamicView.m_arrayShape,
556+
dynamicView.m_strides);
557+
}
558+
else
559+
{
560+
return py::memoryview::from_buffer(
561+
span.data(),
562+
dynamicView.m_arrayShape,
563+
dynamicView.m_strides,
564+
/* readonly = */ false);
565+
}
555566
}
556567

557568
static constexpr char const *errorMsg = "DynamicMemoryView";
558569
};
559570

560571
template <>
561-
pybind11::memoryview
572+
pybind11::object
562573
GetCurrentView::call<std::string>(PythonDynamicMemoryView const &)
563574
{
564575
throw std::runtime_error("[DynamicMemoryView] Only PODs allowed.");
565576
}
566577
} // namespace
567578

568-
pybind11::memoryview PythonDynamicMemoryView::currentView() const
579+
pybind11::object PythonDynamicMemoryView::currentView() const
569580
{
570581
return switchNonVectorType<GetCurrentView>(m_datatype, *this);
571582
}

0 commit comments

Comments
 (0)