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>
36namespace bp = boost::python;
113 typedef std::basic_streambuf<char> base_t;
138 streambuf(bp::object &python_file_obj, std::size_t buffer_size_ = 0)
139 : py_read(getattr(python_file_obj,
"read", bp::object())),
140 py_write(getattr(python_file_obj,
"write", bp::object())),
141 py_seek(getattr(python_file_obj,
"seek", bp::object())),
142 py_tell(getattr(python_file_obj,
"tell", bp::object())),
144 write_buffer(nullptr),
145 pos_of_read_buffer_end_in_py_file(0),
146 pos_of_write_buffer_end_in_py_file(buffer_size),
147 farthest_pptr(nullptr) {
153 if (py_tell != bp::object()) {
155 off_type py_pos = bp::extract<off_type>(py_tell());
156 if (py_seek != bp::object()) {
163 }
catch (bp::error_already_set &) {
164 py_tell = bp::object();
165 py_seek = bp::object();
173 if (py_write != bp::object()) {
175 write_buffer =
new char[buffer_size + 1];
176 write_buffer[buffer_size] =
'\0';
177 setp(write_buffer, write_buffer + buffer_size);
178 farthest_pptr = pptr();
181 setp(
nullptr,
nullptr);
184 if (py_tell != bp::object()) {
185 off_type py_pos = bp::extract<off_type>(py_tell());
186 pos_of_read_buffer_end_in_py_file = py_pos;
187 pos_of_write_buffer_end_in_py_file = py_pos;
193 std::size_t buffer_size_ = 0)
194 :
streambuf(python_file_obj, buffer_size_) {
196 bp::object io_mod = bp::import(
"io");
198 bp::object iobase = io_mod.attr(
"TextIOBase");
205 static bp::object io_mod = bp::object();
206 static bp::object iobase = bp::object();
207 if (!io_mod) io_mod = bp::import(
"io");
208 if (io_mod && !iobase) iobase = io_mod.attr(
"TextIOBase");
213 df_isTextMode = PyObject_IsInstance(python_file_obj.ptr(), iobase.ptr());
217 if (!df_isTextMode) {
219 "Need a text mode file object like StringIO or a file opened "
226 "Need a binary mode file object like BytesIO or a file opened "
231 throw std::invalid_argument(
"bad mode character");
238 delete[] write_buffer;
247 int_type const failure = traits_type::eof();
249 if (status == failure) {
252 return egptr() - gptr();
257 int_type const failure = traits_type::eof();
258 if (py_read == bp::object()) {
259 throw std::invalid_argument(
260 "That Python file object has no 'read' attribute");
262 read_buffer = py_read(buffer_size);
263 char *read_buffer_data;
264 bp::ssize_t py_n_read;
265 if (PyBytes_AsStringAndSize(read_buffer.ptr(), &read_buffer_data,
267 setg(
nullptr,
nullptr,
nullptr);
268 throw std::invalid_argument(
269 "The method 'read' of the Python file object "
270 "did not return a string.");
273 pos_of_read_buffer_end_in_py_file += n_read;
274 setg(read_buffer_data, read_buffer_data, read_buffer_data + n_read);
279 return traits_type::to_int_type(read_buffer_data[0]);
284 if (py_write == bp::object()) {
285 throw std::invalid_argument(
286 "That Python file object has no 'write' attribute");
288 farthest_pptr = std::max(farthest_pptr, pptr());
290 off_type orig_n_written = n_written;
291 const unsigned int STD_ASCII = 0x7F;
292 if (df_isTextMode &&
static_cast<unsigned int>(c) > STD_ASCII) {
296 while (n_written > 0 &&
static_cast<unsigned int>(
297 write_buffer[n_written - 1]) > STD_ASCII) {
301 bp::str chunk(pbase(), pbase() + n_written);
304 if ((!df_isTextMode ||
static_cast<unsigned int>(c) <= STD_ASCII) &&
305 !traits_type::eq_int_type(c, traits_type::eof())) {
306 py_write(traits_type::to_char_type(c));
310 setp(pbase(), epptr());
312 farthest_pptr = pptr();
314 pos_of_write_buffer_end_in_py_file += n_written;
315 if (df_isTextMode &&
static_cast<unsigned int>(c) > STD_ASCII &&
316 !traits_type::eq_int_type(c, traits_type::eof())) {
317 size_t n_to_copy = orig_n_written - n_written;
319 for (
size_t i = 0; i < n_to_copy; ++i) {
320 sputc(write_buffer[n_written + i]);
327 return traits_type::eq_int_type(c, traits_type::eof())
328 ? traits_type::not_eof(c)
341 farthest_pptr = std::max(farthest_pptr, pptr());
342 if (farthest_pptr && farthest_pptr > pbase()) {
343 off_type delta = pptr() - farthest_pptr;
345 if (traits_type::eq_int_type(status, traits_type::eof())) {
348 if (py_seek != bp::object()) {
351 }
else if (gptr() && gptr() < egptr()) {
352 if (py_seek != bp::object()) {
353 py_seek(gptr() - egptr(), 1);
367 std::ios_base::openmode which =
368 std::ios_base::in | std::ios_base::out)
override {
376 if (py_seek == bp::object()) {
377 throw std::invalid_argument(
378 "That Python file object has no 'seek' attribute");
382 if (which == std::ios_base::in && !gptr()) {
383 if (traits_type::eq_int_type(
underflow(), traits_type::eof())) {
391 case std::ios_base::beg:
394 case std::ios_base::cur:
397 case std::ios_base::end:
405 boost::optional<off_type> result =
406 seekoff_without_calling_python(off, way, which);
409 if (which == std::ios_base::out) {
412 if (way == std::ios_base::cur) {
413 if (which == std::ios_base::in) {
414 off -= egptr() - gptr();
415 }
else if (which == std::ios_base::out) {
416 off += pptr() - pbase();
419 py_seek(off, whence);
420 result =
off_type(bp::extract<off_type>(py_tell()));
421 if (which == std::ios_base::in) {
430 std::ios_base::openmode which =
431 std::ios_base::in | std::ios_base::out)
override {
436 bp::object py_read, py_write, py_seek, py_tell;
438 std::size_t buffer_size;
445 bp::object read_buffer;
453 off_type pos_of_read_buffer_end_in_py_file,
454 pos_of_write_buffer_end_in_py_file;
459 boost::optional<off_type> seekoff_without_calling_python(
460 off_type off, std::ios_base::seekdir way, std::ios_base::openmode which) {
461 boost::optional<off_type>
const failure =
off_type(-1);
464 off_type buf_begin, buf_end, buf_cur, upper_bound;
465 off_type pos_of_buffer_end_in_py_file;
466 if (which == std::ios_base::in) {
467 pos_of_buffer_end_in_py_file = pos_of_read_buffer_end_in_py_file;
468 buf_begin =
reinterpret_cast<std::streamsize
>(eback());
469 buf_cur =
reinterpret_cast<std::streamsize
>(gptr());
470 buf_end =
reinterpret_cast<std::streamsize
>(egptr());
471 upper_bound = buf_end;
472 }
else if (which == std::ios_base::out) {
473 pos_of_buffer_end_in_py_file = pos_of_write_buffer_end_in_py_file;
474 buf_begin =
reinterpret_cast<std::streamsize
>(pbase());
475 buf_cur =
reinterpret_cast<std::streamsize
>(pptr());
476 buf_end =
reinterpret_cast<std::streamsize
>(epptr());
477 farthest_pptr = std::max(farthest_pptr, pptr());
478 upper_bound =
reinterpret_cast<std::streamsize
>(farthest_pptr) + 1;
485 if (way == std::ios_base::cur) {
486 buf_sought = buf_cur + off;
487 }
else if (way == std::ios_base::beg) {
488 buf_sought = buf_end + (off - pos_of_buffer_end_in_py_file);
489 }
else if (way == std::ios_base::end) {
496 if (buf_sought < buf_begin || buf_sought >= upper_bound) {
501 if (which == std::ios_base::in) {
502 gbump(buf_sought - buf_cur);
503 }
else if (which == std::ios_base::out) {
504 pbump(buf_sought - buf_cur);
506 return pos_of_buffer_end_in_py_file + (buf_sought - buf_end);
513 exceptions(std::ios_base::badbit);
529 exceptions(std::ios_base::badbit);
534 exceptions(std::ios_base::badbit);
559 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