From b5b35c2f3ca0e575506c3cba2b2879d5a16276b6 Mon Sep 17 00:00:00 2001 From: mgsergio Date: Tue, 4 Apr 2017 17:54:02 +0300 Subject: Add Campaign serialization. Add python bindings. --- pyhelpers/vector_list_conversion.hpp | 15 +++++++++------ pyhelpers/vector_uint8.hpp | 21 ++++++++++++++------- 2 files changed, 23 insertions(+), 13 deletions(-) (limited to 'pyhelpers') diff --git a/pyhelpers/vector_list_conversion.hpp b/pyhelpers/vector_list_conversion.hpp index 2aa12a9c61..52ef1f1080 100644 --- a/pyhelpers/vector_list_conversion.hpp +++ b/pyhelpers/vector_list_conversion.hpp @@ -1,6 +1,6 @@ #pragma once -#include "std/vector.hpp" +#include #include #include @@ -8,16 +8,19 @@ namespace { template -vector python_list_to_std_vector(boost::python::object const & iterable) +std::vector python_list_to_std_vector(boost::python::object const & iterable) { - return vector(boost::python::stl_input_iterator(iterable), - boost::python::stl_input_iterator()); + return std::vector(boost::python::stl_input_iterator(iterable), + boost::python::stl_input_iterator()); } +// For this to work one should define +// class_>("YourClassList") +// .def(vector_indexing_suite>()); template -boost::python::list std_vector_to_python_list(vector const & v) +boost::python::list std_vector_to_python_list(std::vector const & v) { - boost::python::object get_iter = boost::python::iterator>(); + boost::python::object get_iter = boost::python::iterator>(); return boost::python::list(get_iter(v)); } } // namespace diff --git a/pyhelpers/vector_uint8.hpp b/pyhelpers/vector_uint8.hpp index f1bebc5ff3..3f74e97445 100644 --- a/pyhelpers/vector_uint8.hpp +++ b/pyhelpers/vector_uint8.hpp @@ -1,6 +1,13 @@ #pragma once -#include "std/vector.hpp" +#include + +// These headers are necessary for cross-python compilation. +// Python3 does not have PyString_* methods. One should use PyBytes_* instead. +// bytesobject.h contains a mapping from PyBytes_* to PyString_*. +// See https://docs.python.org/2/howto/cporting.html for more. +#include "Python.h" +#include "bytesobject.h" #include #include @@ -12,7 +19,7 @@ using namespace boost::python; // Converts a vector to/from Python str. struct vector_uint8t_to_str { - static PyObject * convert(vector const & v) + static PyObject * convert(std::vector const & v) { str s(reinterpret_cast(v.data()), v.size()); return incref(s.ptr()); @@ -23,24 +30,24 @@ struct vector_uint8t_from_python_str { vector_uint8t_from_python_str() { - converter::registry::push_back(&convertible, &construct, type_id>()); + converter::registry::push_back(&convertible, &construct, type_id>()); } static void * convertible(PyObject * obj_ptr) { - if (!PyString_Check(obj_ptr)) + if (!PyBytes_Check(obj_ptr)) return nullptr; return obj_ptr; } static void construct(PyObject * obj_ptr, converter::rvalue_from_python_stage1_data * data) { - const char * value = PyString_AsString(obj_ptr); + const char * value = PyBytes_AsString(obj_ptr); if (value == nullptr) throw_error_already_set(); void * storage = - ((converter::rvalue_from_python_storage> *)data)->storage.bytes; - new (storage) vector(value, value + PyString_Size(obj_ptr)); + ((converter::rvalue_from_python_storage> *)data)->storage.bytes; + new (storage) std::vector(value, value + PyBytes_Size(obj_ptr)); data->convertible = storage; } }; -- cgit v1.2.3