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> 36 namespace 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())),
143 buffer_size(buffer_size_ != 0 ? buffer_size_ : default_buffer_size),
145 pos_of_read_buffer_end_in_py_file(0),
146 pos_of_write_buffer_end_in_py_file(buffer_size),
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();
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 if (write_buffer)
delete[] write_buffer;
201 int_type
const failure = traits_type::eof();
203 if (status == failure)
return -1;
204 return egptr() - gptr();
209 int_type
const failure = traits_type::eof();
210 if (py_read == bp::object()) {
211 throw std::invalid_argument(
212 "That Python file object has no 'read' attribute");
214 read_buffer = py_read(buffer_size);
215 char* read_buffer_data;
216 bp::ssize_t py_n_read;
217 if (PyBytes_AsStringAndSize(read_buffer.ptr(), &read_buffer_data,
220 throw std::invalid_argument(
221 "The method 'read' of the Python file object " 222 "did not return a string.");
224 off_type n_read = (
off_type)py_n_read;
225 pos_of_read_buffer_end_in_py_file += n_read;
226 setg(read_buffer_data, read_buffer_data, read_buffer_data + n_read);
228 if (n_read == 0)
return failure;
229 return traits_type::to_int_type(read_buffer_data[0]);
234 if (py_write == bp::object()) {
235 throw std::invalid_argument(
236 "That Python file object has no 'write' attribute");
238 farthest_pptr = std::max(farthest_pptr, pptr());
239 off_type n_written = (
off_type)(farthest_pptr - pbase());
240 bp::str chunk(pbase(), farthest_pptr);
242 if (!traits_type::eq_int_type(c, traits_type::eof())) {
243 py_write(traits_type::to_char_type(c));
247 pos_of_write_buffer_end_in_py_file += n_written;
248 setp(pbase(), epptr());
250 farthest_pptr = pptr();
252 return traits_type::eq_int_type(c, traits_type::eof())
253 ? traits_type::not_eof(c)
266 farthest_pptr = std::max(farthest_pptr, pptr());
267 if (farthest_pptr && farthest_pptr > pbase()) {
268 off_type delta = pptr() - farthest_pptr;
270 if (traits_type::eq_int_type(status, traits_type::eof())) result = -1;
271 if (py_seek != bp::object()) py_seek(delta, 1);
272 }
else if (gptr() && gptr() < egptr()) {
273 if (py_seek != bp::object()) py_seek(gptr() - egptr(), 1);
285 virtual pos_type
seekoff(off_type off, std::ios_base::seekdir way,
286 std::ios_base::openmode which = std::ios_base::in |
287 std::ios_base::out) {
295 if (py_seek == bp::object()) {
296 throw std::invalid_argument(
297 "That Python file object has no 'seek' attribute");
301 if (which == std::ios_base::in && !gptr()) {
302 if (traits_type::eq_int_type(
underflow(), traits_type::eof())) {
310 case std::ios_base::beg:
313 case std::ios_base::cur:
316 case std::ios_base::end:
324 boost::optional<off_type> result =
325 seekoff_without_calling_python(off, way, which);
328 if (which == std::ios_base::out)
overflow();
329 if (way == std::ios_base::cur) {
330 if (which == std::ios_base::in)
331 off -= egptr() - gptr();
332 else if (which == std::ios_base::out)
333 off += pptr() - pbase();
335 py_seek(off, whence);
336 result =
off_type(bp::extract<off_type>(py_tell()));
337 if (which == std::ios_base::in)
underflow();
344 std::ios_base::openmode which = std::ios_base::in |
345 std::ios_base::out) {
350 bp::object py_read, py_write, py_seek, py_tell;
352 std::size_t buffer_size;
359 bp::object read_buffer;
366 off_type pos_of_read_buffer_end_in_py_file,
367 pos_of_write_buffer_end_in_py_file;
372 boost::optional<off_type> seekoff_without_calling_python(
373 off_type off, std::ios_base::seekdir way, std::ios_base::openmode which) {
374 boost::optional<off_type>
const failure;
377 off_type buf_begin, buf_end, buf_cur, upper_bound;
378 off_type pos_of_buffer_end_in_py_file;
379 if (which == std::ios_base::in) {
380 pos_of_buffer_end_in_py_file = pos_of_read_buffer_end_in_py_file;
381 buf_begin =
reinterpret_cast<std::streamsize
>(eback());
382 buf_cur =
reinterpret_cast<std::streamsize
>(gptr());
383 buf_end =
reinterpret_cast<std::streamsize
>(egptr());
384 upper_bound = buf_end;
385 }
else if (which == std::ios_base::out) {
386 pos_of_buffer_end_in_py_file = pos_of_write_buffer_end_in_py_file;
387 buf_begin =
reinterpret_cast<std::streamsize
>(pbase());
388 buf_cur =
reinterpret_cast<std::streamsize
>(pptr());
389 buf_end =
reinterpret_cast<std::streamsize
>(epptr());
390 farthest_pptr = std::max(farthest_pptr, pptr());
391 upper_bound =
reinterpret_cast<std::streamsize
>(farthest_pptr) + 1;
398 if (way == std::ios_base::cur) {
399 buf_sought = buf_cur + off;
400 }
else if (way == std::ios_base::beg) {
401 buf_sought = buf_end + (off - pos_of_buffer_end_in_py_file);
402 }
else if (way == std::ios_base::end) {
409 if (buf_sought < buf_begin || buf_sought >= upper_bound)
return failure;
412 if (which == std::ios_base::in)
413 gbump(buf_sought - buf_cur);
414 else if (which == std::ios_base::out)
415 pbump(buf_sought - buf_cur);
416 return pos_of_buffer_end_in_py_file + (buf_sought - buf_end);
423 exceptions(std::ios_base::badbit);
439 exceptions(std::ios_base::badbit);
443 if (this->good()) this->flush();
454 : python_streambuf(python_file_obj, buffer_size) {}
458 ostream(bp::object& python_file_obj, std::size_t buffer_size = 0)
464 if (this->good()) this->flush();
465 }
catch (bp::error_already_set&) {
467 throw std::runtime_error(
468 "Problem closing python ostream.\n" 469 " Known limitation: the error is unrecoverable. Sorry.\n" 470 " Suggestion for programmer: add ostream.flush() before"
streambuf_capsule(bp::object &python_file_obj, std::size_t buffer_size=0)
virtual int sync()
Update the python file to reflect the state of this stream buffer.
ostream(bp::object &python_file_obj, std::size_t buffer_size=0)
static const std::size_t default_buffer_size
The default size of the read and write buffer.
streambuf python_streambuf
#define CHECK_INVARIANT(expr, mess)
streambuf(bp::object &python_file_obj, std::size_t buffer_size_=0)
Construct from a Python file object.
virtual int_type underflow()
C.f. C++ standard section 27.5.2.4.3.
virtual int_type overflow(int_type c=traits_type_eof())
C.f. C++ standard section 27.5.2.4.5.
base_t::off_type off_type
base_t::pos_type pos_type
static int traits_type_eof()
#define TEST_ASSERT(expr)
base_t::int_type int_type
virtual pos_type seekoff(off_type off, std::ios_base::seekdir way, std::ios_base::openmode which=std::ios_base::in|std::ios_base::out)
C.f. C++ standard section 27.5.2.4.2.
virtual pos_type seekpos(pos_type sp, std::ios_base::openmode which=std::ios_base::in|std::ios_base::out)
C.f. C++ standard section 27.5.2.4.2.
virtual std::streamsize showmanyc()
C.f. C++ standard section 27.5.2.4.3.
virtual ~streambuf()
Mundane destructor freeing the allocated resources.
base_t::char_type char_type
A stream buffer getting data from and putting data into a Python file object.
base_t::traits_type traits_type