16#ifndef BOOST_ADAPTBX_PYTHON_STREAMBUF_H
17#define BOOST_ADAPTBX_PYTHON_STREAMBUF_H
19#include <boost/python/object.hpp>
20#include <boost/python/str.hpp>
21#include <boost/python/extract.hpp>
23#include <boost/optional.hpp>
24#include <boost/utility/typed_in_place_factory.hpp>
37namespace bp = boost::python;
114 typedef std::basic_streambuf<char> base_t;
139 streambuf(bp::object& python_file_obj, std::size_t buffer_size_ = 0)
140 : py_read(getattr(python_file_obj,
"read", bp::object())),
141 py_write(getattr(python_file_obj,
"write", bp::object())),
142 py_seek(getattr(python_file_obj,
"seek", bp::object())),
143 py_tell(getattr(python_file_obj,
"tell", bp::object())),
145 write_buffer(nullptr),
146 pos_of_read_buffer_end_in_py_file(0),
147 pos_of_write_buffer_end_in_py_file(buffer_size),
148 farthest_pptr(nullptr) {
154 if (py_tell != bp::object()) {
156 off_type py_pos = bp::extract<off_type>(py_tell());
157 if (py_seek != bp::object()) {
164 }
catch (bp::error_already_set&) {
165 py_tell = bp::object();
166 py_seek = bp::object();
174 if (py_write != bp::object()) {
176 write_buffer =
new char[buffer_size + 1];
177 write_buffer[buffer_size] =
'\0';
178 setp(write_buffer, write_buffer + buffer_size);
179 farthest_pptr = pptr();
182 setp(
nullptr,
nullptr);
185 if (py_tell != bp::object()) {
186 off_type py_pos = bp::extract<off_type>(py_tell());
187 pos_of_read_buffer_end_in_py_file = py_pos;
188 pos_of_write_buffer_end_in_py_file = py_pos;
194 std::size_t buffer_size_ = 0)
195 :
streambuf(python_file_obj, buffer_size_) {
197 bp::object io_mod = bp::import(
"io");
199 bp::object iobase = io_mod.attr(
"TextIOBase");
206 static bp::object io_mod = bp::object();
207 static bp::object iobase = bp::object();
208 if (!io_mod) io_mod = bp::import(
"io");
209 if (io_mod && !iobase) iobase = io_mod.attr(
"TextIOBase");
214 df_isTextMode = PyObject_IsInstance(python_file_obj.ptr(), iobase.ptr());
218 if (!df_isTextMode) {
220 "Need a text mode file object like StringIO or a file opened "
227 "Need a binary mode file object like BytesIO or a file opened "
232 throw std::invalid_argument(
"bad mode character");
239 delete[] write_buffer;
248 int_type const failure = traits_type::eof();
250 if (status == failure) {
253 return egptr() - gptr();
258 int_type const failure = traits_type::eof();
259 if (py_read == bp::object()) {
260 throw std::invalid_argument(
261 "That Python file object has no 'read' attribute");
263 read_buffer = py_read(buffer_size);
264 char* read_buffer_data;
265 bp::ssize_t py_n_read;
266 if (PyBytes_AsStringAndSize(read_buffer.ptr(), &read_buffer_data,
268 setg(
nullptr,
nullptr,
nullptr);
269 throw std::invalid_argument(
270 "The method 'read' of the Python file object "
271 "did not return a string.");
274 pos_of_read_buffer_end_in_py_file += n_read;
275 setg(read_buffer_data, read_buffer_data, read_buffer_data + n_read);
280 return traits_type::to_int_type(read_buffer_data[0]);
285 if (py_write == bp::object()) {
286 throw std::invalid_argument(
287 "That Python file object has no 'write' attribute");
289 farthest_pptr = std::max(farthest_pptr, pptr());
291 off_type orig_n_written = n_written;
292 const unsigned int STD_ASCII = 0x7F;
293 if (df_isTextMode &&
static_cast<unsigned int>(c) > STD_ASCII) {
297 while (n_written > 0 &&
static_cast<unsigned int>(
298 write_buffer[n_written - 1]) > STD_ASCII) {
302 bp::str chunk(pbase(), pbase() + n_written);
305 if ((!df_isTextMode ||
static_cast<unsigned int>(c) <= STD_ASCII) &&
306 !traits_type::eq_int_type(c, traits_type::eof())) {
307 py_write(traits_type::to_char_type(c));
311 setp(pbase(), epptr());
313 farthest_pptr = pptr();
315 pos_of_write_buffer_end_in_py_file += n_written;
316 if (df_isTextMode &&
static_cast<unsigned int>(c) > STD_ASCII &&
317 !traits_type::eq_int_type(c, traits_type::eof())) {
318 size_t n_to_copy = orig_n_written - n_written;
320 for (
size_t i = 0; i < n_to_copy; ++i) {
321 sputc(write_buffer[n_written + i]);
328 return traits_type::eq_int_type(c, traits_type::eof())
329 ? traits_type::not_eof(c)
342 farthest_pptr = std::max(farthest_pptr, pptr());
343 if (farthest_pptr && farthest_pptr > pbase()) {
344 off_type delta = pptr() - farthest_pptr;
346 if (traits_type::eq_int_type(status, traits_type::eof())) {
349 if (py_seek != bp::object()) {
352 }
else if (gptr() && gptr() < egptr()) {
353 if (py_seek != bp::object()) {
354 py_seek(gptr() - egptr(), 1);
368 std::ios_base::openmode which =
369 std::ios_base::in | std::ios_base::out)
override {
377 if (py_seek == bp::object()) {
378 throw std::invalid_argument(
379 "That Python file object has no 'seek' attribute");
383 if (which == std::ios_base::in && !gptr()) {
384 if (traits_type::eq_int_type(
underflow(), traits_type::eof())) {
392 case std::ios_base::beg:
395 case std::ios_base::cur:
398 case std::ios_base::end:
406 boost::optional<off_type> result =
407 seekoff_without_calling_python(off, way, which);
410 if (which == std::ios_base::out) {
413 if (way == std::ios_base::cur) {
414 if (which == std::ios_base::in) {
415 off -= egptr() - gptr();
416 }
else if (which == std::ios_base::out) {
417 off += pptr() - pbase();
420 py_seek(off, whence);
421 result =
off_type(bp::extract<off_type>(py_tell()));
422 if (which == std::ios_base::in) {
431 std::ios_base::openmode which =
432 std::ios_base::in | std::ios_base::out)
override {
437 bp::object py_read, py_write, py_seek, py_tell;
439 std::size_t buffer_size;
446 bp::object read_buffer;
454 off_type pos_of_read_buffer_end_in_py_file,
455 pos_of_write_buffer_end_in_py_file;
460 boost::optional<off_type> seekoff_without_calling_python(
461 off_type off, std::ios_base::seekdir way, std::ios_base::openmode which) {
462 boost::optional<off_type>
const failure;
465 off_type buf_begin, buf_end, buf_cur, upper_bound;
466 off_type pos_of_buffer_end_in_py_file;
467 if (which == std::ios_base::in) {
468 pos_of_buffer_end_in_py_file = pos_of_read_buffer_end_in_py_file;
469 buf_begin =
reinterpret_cast<std::streamsize
>(eback());
470 buf_cur =
reinterpret_cast<std::streamsize
>(gptr());
471 buf_end =
reinterpret_cast<std::streamsize
>(egptr());
472 upper_bound = buf_end;
473 }
else if (which == std::ios_base::out) {
474 pos_of_buffer_end_in_py_file = pos_of_write_buffer_end_in_py_file;
475 buf_begin =
reinterpret_cast<std::streamsize
>(pbase());
476 buf_cur =
reinterpret_cast<std::streamsize
>(pptr());
477 buf_end =
reinterpret_cast<std::streamsize
>(epptr());
478 farthest_pptr = std::max(farthest_pptr, pptr());
479 upper_bound =
reinterpret_cast<std::streamsize
>(farthest_pptr) + 1;
486 if (way == std::ios_base::cur) {
487 buf_sought = buf_cur + off;
488 }
else if (way == std::ios_base::beg) {
489 buf_sought = buf_end + (off - pos_of_buffer_end_in_py_file);
490 }
else if (way == std::ios_base::end) {
497 if (buf_sought < buf_begin || buf_sought >= upper_bound) {
502 if (which == std::ios_base::in) {
503 gbump(buf_sought - buf_cur);
504 }
else if (which == std::ios_base::out) {
505 pbump(buf_sought - buf_cur);
507 return pos_of_buffer_end_in_py_file + (buf_sought - buf_end);
514 exceptions(std::ios_base::badbit);
530 exceptions(std::ios_base::badbit);
551 ostream(bp::object& python_file_obj, std::size_t buffer_size = 0)
#define TEST_ASSERT(expr)
#define CHECK_INVARIANT(expr, mess)
Class to allow us to throw a ValueError from C++ and have it make it back to Python.
A stream buffer getting data from and putting data into a Python file object.
base_t::char_type char_type
~streambuf() override
Mundane destructor freeing the allocated resources.
base_t::off_type off_type
static const std::size_t default_buffer_size
The default size of the read and write buffer.
pos_type seekpos(pos_type sp, std::ios_base::openmode which=std::ios_base::in|std::ios_base::out) override
C.f. C++ standard section 27.5.2.4.2.
base_t::pos_type pos_type
static int traits_type_eof()
std::streamsize showmanyc() override
C.f. C++ standard section 27.5.2.4.3.
pos_type seekoff(off_type off, std::ios_base::seekdir way, std::ios_base::openmode which=std::ios_base::in|std::ios_base::out) override
C.f. C++ standard section 27.5.2.4.2.
base_t::int_type int_type
streambuf(bp::object &python_file_obj, char mode, std::size_t buffer_size_=0)
constructor to enforce a mode (binary or text)
int sync() override
Update the python file to reflect the state of this stream buffer.
base_t::traits_type traits_type
int_type overflow(int_type c=traits_type_eof()) override
C.f. C++ standard section 27.5.2.4.5.
int_type underflow() override
C.f. C++ standard section 27.5.2.4.3.
streambuf(bp::object &python_file_obj, std::size_t buffer_size_=0)
Construct from a Python file object.
ostream(bp::object &python_file_obj, std::size_t buffer_size=0)
~ostream() noexcept override
streambuf_capsule(bp::object &python_file_obj, std::size_t buffer_size=0)
streambuf python_streambuf