diff --git a/include/openPMD/binding/python/Container.H b/include/openPMD/binding/python/Container.H index 350eaed823..a07847e600 100644 --- a/include/openPMD/binding/python/Container.H +++ b/include/openPMD/binding/python/Container.H @@ -34,6 +34,7 @@ #include #include +#include #include #include #include @@ -118,11 +119,12 @@ Class_ finalize_container(Class_ cl) // keep same policy as Container class: missing keys are created cl.def( "__getitem__", - [](Map &m, KeyType const &k) -> MappedType & { return m[k]; }, + [](Map &m, KeyType const &k) -> MappedType { return m[k]; }, // copy + keepalive // All objects in the openPMD object model are handles, so using a copy // is safer and still performant. - py::return_value_policy::copy); + py::return_value_policy::move, + py::keep_alive<0, 1>()); // Assignment provided only if the type is copyable py::detail::map_assignment(cl); diff --git a/include/openPMD/binding/python/Pickle.hpp b/include/openPMD/binding/python/Pickle.hpp index eabe307af9..70a8ec1cdd 100644 --- a/include/openPMD/binding/python/Pickle.hpp +++ b/include/openPMD/binding/python/Pickle.hpp @@ -58,7 +58,7 @@ add_pickle(pybind11::class_ &cl, T_SeriesAccessor &&seriesAccessor) }, // __setstate__ - [&seriesAccessor](py::tuple t) { + [&seriesAccessor](py::tuple const &t) { // our tuple has exactly two elements: filePath & group if (t.size() != 2) throw std::runtime_error("Invalid state!"); diff --git a/src/binding/python/MeshRecordComponent.cpp b/src/binding/python/MeshRecordComponent.cpp index 1a43f0e289..825753402d 100644 --- a/src/binding/python/MeshRecordComponent.cpp +++ b/src/binding/python/MeshRecordComponent.cpp @@ -84,7 +84,10 @@ void init_MeshRecordComponent(py::module &m) add_pickle( cl, [](openPMD::Series &series, std::vector const &group) { uint64_t const n_it = std::stoull(group.at(1)); - return series.iterations[n_it].meshes[group.at(3)][group.at(4)]; + return series.iterations[n_it] + .meshes[group.at(3)] + [group.size() < 5 ? MeshRecordComponent::SCALAR + : group.at(4)]; }); finalize_container(py_mrc_cnt); diff --git a/src/binding/python/RecordComponent.cpp b/src/binding/python/RecordComponent.cpp index 5bceeaee9c..e323127885 100644 --- a/src/binding/python/RecordComponent.cpp +++ b/src/binding/python/RecordComponent.cpp @@ -1124,8 +1124,8 @@ void init_RecordComponent(py::module &m) add_pickle( cl, [](openPMD::Series &series, std::vector const &group) { uint64_t const n_it = std::stoull(group.at(1)); - return series.iterations[n_it] - .particles[group.at(3)][group.at(4)][group.at(5)]; + return series.iterations[n_it].particles[group.at(3)][group.at( + 4)][group.size() < 6 ? RecordComponent::SCALAR : group.at(5)]; }); addRecordComponentSetGet(cl);