15 #ifndef BOOST_ADAPTBX_PYTHON_STREAMBUF_H 16 #define BOOST_ADAPTBX_PYTHON_STREAMBUF_H 18 #include <boost/python/object.hpp> 19 #include <boost/python/str.hpp> 20 #include <boost/python/extract.hpp> 22 #include <boost/optional.hpp> 23 #include <boost/utility/typed_in_place_factory.hpp> 33 namespace bp = boost::python;
111 typedef std::basic_streambuf<char> base_t;
138 bp::object& python_file_obj,
139 std::size_t buffer_size_=0)
141 py_read (getattr(python_file_obj,
"read", bp::object())),
142 py_write(getattr(python_file_obj,
"write", bp::object())),
143 py_seek (getattr(python_file_obj,
"seek", bp::object())),
144 py_tell (getattr(python_file_obj,
"tell", bp::object())),
145 buffer_size(buffer_size_ != 0 ? buffer_size_ : default_buffer_size),
147 pos_of_read_buffer_end_in_py_file(0),
148 pos_of_write_buffer_end_in_py_file(buffer_size),
156 if (py_tell != bp::object()) {
158 off_type py_pos = bp::extract<off_type>(py_tell());
159 if(py_seek != bp::object()){
167 catch (bp::error_already_set&) {
168 py_tell = bp::object();
169 py_seek = bp::object();
177 if (py_write != bp::object()) {
179 write_buffer =
new char[buffer_size + 1];
180 write_buffer[buffer_size] =
'\0';
181 setp(write_buffer, write_buffer + buffer_size);
182 farthest_pptr = pptr();
189 if (py_tell != bp::object()) {
190 off_type py_pos = bp::extract<off_type>(py_tell());
191 pos_of_read_buffer_end_in_py_file = py_pos;
192 pos_of_write_buffer_end_in_py_file = py_pos;
198 if (write_buffer)
delete[] write_buffer;
206 int_type
const failure = traits_type::eof();
208 if (status == failure)
return -1;
209 return egptr() - gptr();
214 int_type
const failure = traits_type::eof();
215 if (py_read == bp::object()) {
216 throw std::invalid_argument(
217 "That Python file object has no 'read' attribute");
219 read_buffer = py_read(buffer_size);
220 char *read_buffer_data;
221 bp::ssize_t py_n_read;
222 if (PyBytes_AsStringAndSize(read_buffer.ptr(),
223 &read_buffer_data, &py_n_read) == -1) {
225 throw std::invalid_argument(
226 "The method 'read' of the Python file object " 227 "did not return a string.");
229 off_type n_read = (
off_type)py_n_read;
230 pos_of_read_buffer_end_in_py_file += n_read;
231 setg(read_buffer_data, read_buffer_data, read_buffer_data + n_read);
233 if (n_read == 0)
return failure;
234 return traits_type::to_int_type(read_buffer_data[0]);
239 if (py_write == bp::object()) {
240 throw std::invalid_argument(
241 "That Python file object has no 'write' attribute");
243 farthest_pptr = std::max(farthest_pptr, pptr());
244 off_type n_written = (
off_type)(farthest_pptr - pbase());
245 bp::str chunk(pbase(), farthest_pptr);
247 if (!traits_type::eq_int_type(c, traits_type::eof())) {
248 py_write(traits_type::to_char_type(c));
252 pos_of_write_buffer_end_in_py_file += n_written;
253 setp(pbase(), epptr());
255 farthest_pptr = pptr();
257 return traits_type::eq_int_type(
258 c, traits_type::eof()) ? traits_type::not_eof(c) : c;
270 farthest_pptr = std::max(farthest_pptr, pptr());
271 if (farthest_pptr && farthest_pptr > pbase()) {
272 off_type delta = pptr() - farthest_pptr;
274 if (traits_type::eq_int_type(status, traits_type::eof())) result = -1;
275 if (py_seek != bp::object()) py_seek(delta, 1);
277 else if (gptr() && gptr() < egptr()) {
278 if (py_seek != bp::object()) py_seek(gptr() - egptr(), 1);
291 pos_type
seekoff(off_type off, std::ios_base::seekdir way,
292 std::ios_base::openmode which= std::ios_base::in
293 | std::ios_base::out)
302 if (py_seek == bp::object()) {
303 throw std::invalid_argument(
304 "That Python file object has no 'seek' attribute");
308 if (which == std::ios_base::in && !gptr()) {
309 if (traits_type::eq_int_type(
underflow(), traits_type::eof())) {
317 case std::ios_base::beg:
320 case std::ios_base::cur:
323 case std::ios_base::end:
331 boost::optional<off_type> result = seekoff_without_calling_python(
335 if (which == std::ios_base::out)
overflow();
336 if (way == std::ios_base::cur) {
337 if (which == std::ios_base::in) off -= egptr() - gptr();
338 else if (which == std::ios_base::out) off += pptr() - pbase();
340 py_seek(off, whence);
341 result =
off_type(bp::extract<off_type>(py_tell()));
342 if (which == std::ios_base::in)
underflow();
350 std::ios_base::openmode which= std::ios_base::in
351 | std::ios_base::out)
357 bp::object py_read, py_write, py_seek, py_tell;
359 std::size_t buffer_size;
366 bp::object read_buffer;
373 off_type pos_of_read_buffer_end_in_py_file,
374 pos_of_write_buffer_end_in_py_file;
380 boost::optional<off_type> seekoff_without_calling_python(
382 std::ios_base::seekdir way,
383 std::ios_base::openmode which)
385 boost::optional<off_type>
const failure;
388 off_type buf_begin, buf_end, buf_cur, upper_bound;
389 off_type pos_of_buffer_end_in_py_file;
390 if (which == std::ios_base::in) {
391 pos_of_buffer_end_in_py_file = pos_of_read_buffer_end_in_py_file;
392 buf_begin =
reinterpret_cast<std::streamsize
>(eback());
393 buf_cur =
reinterpret_cast<std::streamsize
>(gptr());
394 buf_end =
reinterpret_cast<std::streamsize
>(egptr());
395 upper_bound = buf_end;
397 else if (which == std::ios_base::out) {
398 pos_of_buffer_end_in_py_file = pos_of_write_buffer_end_in_py_file;
399 buf_begin =
reinterpret_cast<std::streamsize
>(pbase());
400 buf_cur =
reinterpret_cast<std::streamsize
>(pptr());
401 buf_end =
reinterpret_cast<std::streamsize
>(epptr());
402 farthest_pptr = std::max(farthest_pptr, pptr());
403 upper_bound =
reinterpret_cast<std::streamsize
>(farthest_pptr) + 1;
411 if (way == std::ios_base::cur) {
412 buf_sought = buf_cur + off;
414 else if (way == std::ios_base::beg) {
415 buf_sought = buf_end + (off - pos_of_buffer_end_in_py_file);
417 else if (way == std::ios_base::end) {
425 if (buf_sought < buf_begin || buf_sought >= upper_bound)
return failure;
428 if (which == std::ios_base::in) gbump(buf_sought - buf_cur);
429 else if (which == std::ios_base::out) pbump(buf_sought - buf_cur);
430 return pos_of_buffer_end_in_py_file + (buf_sought - buf_end);
440 exceptions(std::ios_base::badbit);
451 exceptions(std::ios_base::badbit);
465 bp::object& python_file_obj,
466 std::size_t buffer_size=0)
468 python_streambuf(python_file_obj, buffer_size)
475 bp::object& python_file_obj,
476 std::size_t buffer_size=0)
485 if (this->good()) this->flush();
487 catch (bp::error_already_set&) {
489 throw std::runtime_error(
490 "Problem closing python ostream.\n" 491 " Known limitation: the error is unrecoverable. Sorry.\n" 492 " 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