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>
35namespace bp = boost::python;
112 typedef std::basic_streambuf<char> base_t;
137 streambuf(bp::object &python_file_obj, std::size_t buffer_size_ = 0)
138 : py_read(getattr(python_file_obj,
"read", bp::object())),
139 py_write(getattr(python_file_obj,
"write", bp::object())),
140 py_seek(getattr(python_file_obj,
"seek", bp::object())),
141 py_tell(getattr(python_file_obj,
"tell", bp::object())),
143 write_buffer(nullptr),
144 pos_of_read_buffer_end_in_py_file(0),
145 pos_of_write_buffer_end_in_py_file(buffer_size),
146 farthest_pptr(nullptr) {
152 if (py_tell != bp::object()) {
154 off_type py_pos = bp::extract<off_type>(py_tell());
155 if (py_seek != bp::object()) {
162 }
catch (bp::error_already_set &) {
163 py_tell = bp::object();
164 py_seek = bp::object();
172 if (py_write != bp::object()) {
174 write_buffer =
new char[buffer_size + 1];
175 write_buffer[buffer_size] =
'\0';
176 setp(write_buffer, write_buffer + buffer_size);
177 farthest_pptr = pptr();
180 setp(
nullptr,
nullptr);
183 if (py_tell != bp::object()) {
184 off_type py_pos = bp::extract<off_type>(py_tell());
185 pos_of_read_buffer_end_in_py_file = py_pos;
186 pos_of_write_buffer_end_in_py_file = py_pos;
192 std::size_t buffer_size_ = 0)
193 :
streambuf(python_file_obj, buffer_size_) {
195 bp::object io_mod = bp::import(
"io");
197 bp::object iobase = io_mod.attr(
"TextIOBase");
204 static bp::object io_mod = bp::object();
205 static bp::object iobase = bp::object();
206 if (!io_mod) io_mod = bp::import(
"io");
207 if (io_mod && !iobase) iobase = io_mod.attr(
"TextIOBase");
212 df_isTextMode = PyObject_IsInstance(python_file_obj.ptr(), iobase.ptr());
216 if (!df_isTextMode) {
218 "Need a text mode file object like StringIO or a file opened "
225 "Need a binary mode file object like BytesIO or a file opened "
230 throw std::invalid_argument(
"bad mode character");
237 delete[] write_buffer;
246 int_type const failure = traits_type::eof();
248 if (status == failure) {
251 return egptr() - gptr();
256 int_type const failure = traits_type::eof();
257 if (py_read == bp::object()) {
258 throw std::invalid_argument(
259 "That Python file object has no 'read' attribute");
261 read_buffer = py_read(buffer_size);
262 char *read_buffer_data;
263 bp::ssize_t py_n_read;
264 if (PyBytes_AsStringAndSize(read_buffer.ptr(), &read_buffer_data,
266 setg(
nullptr,
nullptr,
nullptr);
267 throw std::invalid_argument(
268 "The method 'read' of the Python file object "
269 "did not return a string.");
272 pos_of_read_buffer_end_in_py_file += n_read;
273 setg(read_buffer_data, read_buffer_data, read_buffer_data + n_read);
278 return traits_type::to_int_type(read_buffer_data[0]);
283 if (py_write == bp::object()) {
284 throw std::invalid_argument(
285 "That Python file object has no 'write' attribute");
287 farthest_pptr = std::max(farthest_pptr, pptr());
289 off_type orig_n_written = n_written;
290 const unsigned int STD_ASCII = 0x7F;
291 if (df_isTextMode &&
static_cast<unsigned int>(c) > STD_ASCII) {
295 while (n_written > 0 &&
static_cast<unsigned int>(
296 write_buffer[n_written - 1]) > STD_ASCII) {
300 bp::str chunk(pbase(), pbase() + n_written);
303 if ((!df_isTextMode ||
static_cast<unsigned int>(c) <= STD_ASCII) &&
304 !traits_type::eq_int_type(c, traits_type::eof())) {
305 py_write(traits_type::to_char_type(c));
309 setp(pbase(), epptr());
311 farthest_pptr = pptr();
313 pos_of_write_buffer_end_in_py_file += n_written;
314 if (df_isTextMode &&
static_cast<unsigned int>(c) > STD_ASCII &&
315 !traits_type::eq_int_type(c, traits_type::eof())) {
316 size_t n_to_copy = orig_n_written - n_written;
318 for (
size_t i = 0; i < n_to_copy; ++i) {
319 sputc(write_buffer[n_written + i]);
326 return traits_type::eq_int_type(c, traits_type::eof())
327 ? traits_type::not_eof(c)
340 farthest_pptr = std::max(farthest_pptr, pptr());
341 if (farthest_pptr && farthest_pptr > pbase()) {
342 off_type delta = pptr() - farthest_pptr;
344 if (traits_type::eq_int_type(status, traits_type::eof())) {
347 if (py_seek != bp::object()) {
350 }
else if (gptr() && gptr() < egptr()) {
351 if (py_seek != bp::object()) {
352 py_seek(gptr() - egptr(), 1);
366 std::ios_base::openmode which =
367 std::ios_base::in | std::ios_base::out)
override {
375 if (py_seek == bp::object()) {
376 throw std::invalid_argument(
377 "That Python file object has no 'seek' attribute");
381 if (which == std::ios_base::in && !gptr()) {
382 if (traits_type::eq_int_type(
underflow(), traits_type::eof())) {
390 case std::ios_base::beg:
393 case std::ios_base::cur:
396 case std::ios_base::end:
404 boost::optional<off_type> result =
405 seekoff_without_calling_python(off, way, which);
408 if (which == std::ios_base::out) {
411 if (way == std::ios_base::cur) {
412 if (which == std::ios_base::in) {
413 off -= egptr() - gptr();
414 }
else if (which == std::ios_base::out) {
415 off += pptr() - pbase();
418 py_seek(off, whence);
419 result =
off_type(bp::extract<off_type>(py_tell()));
420 if (which == std::ios_base::in) {
429 std::ios_base::openmode which =
430 std::ios_base::in | std::ios_base::out)
override {
435 bp::object py_read, py_write, py_seek, py_tell;
437 std::size_t buffer_size;
444 bp::object read_buffer;
452 off_type pos_of_read_buffer_end_in_py_file,
453 pos_of_write_buffer_end_in_py_file;
458 boost::optional<off_type> seekoff_without_calling_python(
459 off_type off, std::ios_base::seekdir way, std::ios_base::openmode which) {
460 boost::optional<off_type>
const failure =
off_type(-1);
463 off_type buf_begin, buf_end, buf_cur, upper_bound;
464 off_type pos_of_buffer_end_in_py_file;
465 if (which == std::ios_base::in) {
466 pos_of_buffer_end_in_py_file = pos_of_read_buffer_end_in_py_file;
467 buf_begin =
reinterpret_cast<std::streamsize
>(eback());
468 buf_cur =
reinterpret_cast<std::streamsize
>(gptr());
469 buf_end =
reinterpret_cast<std::streamsize
>(egptr());
470 upper_bound = buf_end;
471 }
else if (which == std::ios_base::out) {
472 pos_of_buffer_end_in_py_file = pos_of_write_buffer_end_in_py_file;
473 buf_begin =
reinterpret_cast<std::streamsize
>(pbase());
474 buf_cur =
reinterpret_cast<std::streamsize
>(pptr());
475 buf_end =
reinterpret_cast<std::streamsize
>(epptr());
476 farthest_pptr = std::max(farthest_pptr, pptr());
477 upper_bound =
reinterpret_cast<std::streamsize
>(farthest_pptr) + 1;
484 if (way == std::ios_base::cur) {
485 buf_sought = buf_cur + off;
486 }
else if (way == std::ios_base::beg) {
487 buf_sought = buf_end + (off - pos_of_buffer_end_in_py_file);
488 }
else if (way == std::ios_base::end) {
495 if (buf_sought < buf_begin || buf_sought >= upper_bound) {
500 if (which == std::ios_base::in) {
501 gbump(buf_sought - buf_cur);
502 }
else if (which == std::ios_base::out) {
503 pbump(buf_sought - buf_cur);
505 return pos_of_buffer_end_in_py_file + (buf_sought - buf_end);
512 exceptions(std::ios_base::badbit);
528 exceptions(std::ios_base::badbit);
533 exceptions(std::ios_base::badbit);
558 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