Skip to content

Commit

Permalink
BUG: File I/O needs bytes in Python 3
Browse files Browse the repository at this point in the history
The wrapper that allows us to use Python file-like objects as C++
streams was specific to Python 2 inasmuch as it wrapped data in a
str() before sending it to the write() method.

As of Boost 1.67, Boost.Python doesn't have a convenient wrapper for
bytes the way it does str.  We instantiate a bytes() object around the
raw data ourselves.

Some of the issues involved are discussed in this pull request:

boostorg/python#54
  • Loading branch information
atwilso committed Dec 20, 2018
1 parent 05d4a65 commit 1702f12
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion tracktable/PythonWrapping/PythonFileLikeObjectStreams.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,17 @@ class PythonWriteSink
// can do is dump the data out, claim it was all written and
// hope for the best.

#if PY_MAJOR_VERSION == 2
boost::python::str data(buffer, n);

boost::python::object write_result(this->Writer(data));
boost::python::extract<std::streamsize> bytes_written(write_result);

#else
boost::python::object data(boost::python::handle<>(PyBytes_FromStringAndSize(buffer, n)));
boost::python::object write_result(this->Writer(data));
boost::python::extract<std::streamsize> bytes_written(write_result);
#endif

return (bytes_written.check() ? bytes_written() : n);
}

Expand Down

0 comments on commit 1702f12

Please sign in to comment.