From bac47ee55fbce4ec74c8e1785c1c995eb4e4e44c Mon Sep 17 00:00:00 2001 From: Alain Miniussi Date: Thu, 18 Jun 2020 16:58:59 +0200 Subject: [PATCH] have pick API work in both python 2 and 3 --- include/boost/mpi/python/serialize.hpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/include/boost/mpi/python/serialize.hpp b/include/boost/mpi/python/serialize.hpp index a4b1c6f..0ae1277 100644 --- a/include/boost/mpi/python/serialize.hpp +++ b/include/boost/mpi/python/serialize.hpp @@ -400,8 +400,15 @@ save_impl(Archiver& ar, const boost::python::object& obj, mpl::false_ /*has_direct_serialization*/) { boost::python::object buf = boost::python::pickle::dumps(obj); +#if PY_MAJOR_VERSION >= 3 + Py_ssize_t py_len; + char* string; + PyBytes_AsStringAndSize(buf.ptr(), &string, &py_len); + int len = py_len; // int, as in load_impl +#else int len = boost::python::extract(buf.attr("__len__")()); - const char* string = boost::python::extract(buf); + char const* string = boost::python::extract(buf); +#endif ar << len << boost::serialization::make_array(string, len); } @@ -441,10 +448,14 @@ load_impl(Archiver& ar, boost::python::object& obj, { int len; ar >> len; - boost::scoped_array string(new char[len]); ar >> boost::serialization::make_array(string.get(), len); +#if PY_MAJOR_VERSION >= 3 + char *data = string.get(); + boost::python::object buf(boost::python::handle<>(PyBytes_FromStringAndSize(data, len))); +#else boost::python::str buf(string.get(), len); +#endif obj = boost::python::pickle::loads(buf); } @@ -487,7 +498,6 @@ save(Archiver& ar, const boost::python::object& obj, { typedef Archiver OArchiver; typedef typename input_archiver::type IArchiver; - detail::save_impl(ar, obj, version, has_direct_serialization()); } @@ -499,7 +509,6 @@ load(Archiver& ar, boost::python::object& obj, { typedef Archiver IArchiver; typedef typename output_archiver::type OArchiver; - detail::load_impl(ar, obj, version, has_direct_serialization()); }