diff options
author | Sergey Magidovich <mgsergio@mapswithme.com> | 2017-04-12 13:56:51 +0300 |
---|---|---|
committer | Sergey Magidovich <mgsergio@mapswithme.com> | 2017-04-12 13:56:51 +0300 |
commit | 32e566da5d4b7282ee463f2e2ad1afb873da9bb7 (patch) | |
tree | adc6d7dc9f8620279ab0cfc303a2322d7e3cc3b6 | |
parent | 2ecff49a9aaed92d42f9510ade1f7006eca80b24 (diff) |
Fix vector_uint8t_to_str to work woth both python 2 and 3.
-rw-r--r-- | pyhelpers/vector_uint8.hpp | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/pyhelpers/vector_uint8.hpp b/pyhelpers/vector_uint8.hpp index 3f74e97445..d10a417c44 100644 --- a/pyhelpers/vector_uint8.hpp +++ b/pyhelpers/vector_uint8.hpp @@ -16,16 +16,21 @@ namespace { using namespace boost::python; -// Converts a vector<uint8_t> to/from Python str. +// Converts a vector<uint8_t> to Python2 str or Python3 bytes. struct vector_uint8t_to_str { static PyObject * convert(std::vector<uint8_t> const & v) { - str s(reinterpret_cast<char const *>(v.data()), v.size()); - return incref(s.ptr()); + auto bytes = PyBytes_FromStringAndSize( + reinterpret_cast<char const *>(v.data()), + v.size()); + Py_INCREF(bytes); + + return bytes; } }; +// Converts a vector<uint8_t> from Python2 str or Python3 bytes. struct vector_uint8t_from_python_str { vector_uint8t_from_python_str() |