diff options
author | mgsergio <mgsergio@yandex.ru> | 2017-04-04 17:54:02 +0300 |
---|---|---|
committer | Sergey Yershov <syershov@maps.me> | 2017-04-04 17:54:02 +0300 |
commit | b5b35c2f3ca0e575506c3cba2b2879d5a16276b6 (patch) | |
tree | 8783b05e2490ed83b9ac346c4cea53ef8bd1986d /pyhelpers/vector_uint8.hpp | |
parent | cec1817da3845ae94665dfe294d824766802972d (diff) |
Add Campaign serialization. Add python bindings.
Diffstat (limited to 'pyhelpers/vector_uint8.hpp')
-rw-r--r-- | pyhelpers/vector_uint8.hpp | 21 |
1 files changed, 14 insertions, 7 deletions
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 <vector> + +// 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 <boost/python.hpp> #include <boost/python/suite/indexing/map_indexing_suite.hpp> @@ -12,7 +19,7 @@ using namespace boost::python; // Converts a vector<uint8_t> to/from Python str. struct vector_uint8t_to_str { - static PyObject * convert(vector<uint8_t> const & v) + static PyObject * convert(std::vector<uint8_t> const & v) { str s(reinterpret_cast<char const *>(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<vector<uint8_t>>()); + converter::registry::push_back(&convertible, &construct, type_id<std::vector<uint8_t>>()); } 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<vector<uint8_t>> *)data)->storage.bytes; - new (storage) vector<uint8_t>(value, value + PyString_Size(obj_ptr)); + ((converter::rvalue_from_python_storage<std::vector<uint8_t>> *)data)->storage.bytes; + new (storage) std::vector<uint8_t>(value, value + PyBytes_Size(obj_ptr)); data->convertible = storage; } }; |