A stream buffer getting data from and putting data into a Python file object. More...
#include <python_streambuf.h>
Classes | |
| class | istream |
| class | ostream |
Public Types | |
| typedef base_t::char_type | char_type |
| typedef base_t::int_type | int_type |
| typedef base_t::pos_type | pos_type |
| typedef base_t::off_type | off_type |
| typedef base_t::traits_type | traits_type |
Public Member Functions | |
| streambuf (bp::object &python_file_obj, std::size_t buffer_size_=0) | |
| Construct from a Python file object. | |
| virtual | ~streambuf () |
| Mundane destructor freeing the allocated resources. | |
| virtual std::streamsize | showmanyc () |
| C.f. C++ standard section 27.5.2.4.3. | |
| 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. | |
| virtual int | sync () |
| Update the python file to reflect the state of this stream buffer. | |
| 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. | |
Static Public Member Functions | |
| static int | traits_type_eof () |
Static Public Attributes | |
| static const std::size_t | default_buffer_size = 1024 |
| The default size of the read and write buffer. | |
A stream buffer getting data from and putting data into a Python file object.
The aims are as follow:
void read_inputs(std::istream& input) {
...
input >> something >> something_else;
}
and given a piece of Python code which creates a file-like object, to be able to pass this file object to that C++ function, e.g.
import gzip
gzip_file_obj = gzip.GzipFile(...)
read_inputs(gzip_file_obj)
and have the standard stream pull data from and put data into the Python file object.
read_inputs() returns, the Python object is able to continue reading or writing where the C++ code left off.std::fstream.Motivation
Usage
This is 2-step:
using boost_adaptbx::python::streambuf; void read_inputs_wrapper(streambuf& input) { streambuf::istream is(input); read_inputs(is); } def("read_inputs", read_inputs_wrapper);
which has to be written every time one wants a Python binding for such a C++ function.
buffer_size is optional. See also: default_buffer_size
Note: references are to the C++ standard (the numbers between parentheses at the end of references are margin markers).
Definition at line 108 of file python_streambuf.h.
| typedef base_t::char_type boost_adaptbx::python::streambuf::char_type |
Definition at line 118 of file python_streambuf.h.
| typedef base_t::int_type boost_adaptbx::python::streambuf::int_type |
Definition at line 119 of file python_streambuf.h.
| typedef base_t::off_type boost_adaptbx::python::streambuf::off_type |
Definition at line 121 of file python_streambuf.h.
| typedef base_t::pos_type boost_adaptbx::python::streambuf::pos_type |
Definition at line 120 of file python_streambuf.h.
| typedef base_t::traits_type boost_adaptbx::python::streambuf::traits_type |
Definition at line 122 of file python_streambuf.h.
| boost_adaptbx::python::streambuf::streambuf | ( | bp::object & | python_file_obj, | |
| std::size_t | buffer_size_ = 0 | |||
| ) | [inline] |
Construct from a Python file object.
if buffer_size is 0 the current default_buffer_size is used.
Definition at line 137 of file python_streambuf.h.
References TEST_ASSERT.
| virtual boost_adaptbx::python::streambuf::~streambuf | ( | ) | [inline, virtual] |
Mundane destructor freeing the allocated resources.
Definition at line 190 of file python_streambuf.h.
| virtual int_type boost_adaptbx::python::streambuf::overflow | ( | int_type | c = traits_type_eof() |
) | [inline, virtual] |
C.f. C++ standard section 27.5.2.4.5.
Definition at line 231 of file python_streambuf.h.
| virtual pos_type boost_adaptbx::python::streambuf::seekoff | ( | off_type | off, | |
| std::ios_base::seekdir | way, | |||
| std::ios_base::openmode | which = std::ios_base::in | std::ios_base::out | |||
| ) | [inline, virtual] |
C.f. C++ standard section 27.5.2.4.2.
This implementation is optimised to look whether the position is within the buffers, so as to avoid calling Python seek or tell. It is important for many applications that the overhead of calling into Python is avoided as much as possible (e.g. parsers which may do a lot of backtracking)
Definition at line 284 of file python_streambuf.h.
References overflow(), and underflow().
Referenced by seekpos().
| virtual pos_type boost_adaptbx::python::streambuf::seekpos | ( | pos_type | sp, | |
| std::ios_base::openmode | which = std::ios_base::in | std::ios_base::out | |||
| ) | [inline, virtual] |
C.f. C++ standard section 27.5.2.4.2.
Definition at line 342 of file python_streambuf.h.
References seekoff().
| virtual std::streamsize boost_adaptbx::python::streambuf::showmanyc | ( | ) | [inline, virtual] |
C.f. C++ standard section 27.5.2.4.3.
It is essential to override this virtual function for the stream member function readsome to work correctly (c.f. 27.6.1.3, alinea 30)
Definition at line 198 of file python_streambuf.h.
References underflow().
| virtual int boost_adaptbx::python::streambuf::sync | ( | ) | [inline, virtual] |
Update the python file to reflect the state of this stream buffer.
Empty the write buffer into the Python file object and set the seek position of the latter accordingly (C++ standard section 27.5.2.4.2). If there is no write buffer or it is empty, but there is a non-empty read buffer, set the Python file object seek position to the seek position in that read buffer.
Definition at line 261 of file python_streambuf.h.
References overflow().
Referenced by boost_adaptbx::python::streambuf::istream::~istream().
| static int boost_adaptbx::python::streambuf::traits_type_eof | ( | ) | [inline, static] |
Definition at line 126 of file python_streambuf.h.
| virtual int_type boost_adaptbx::python::streambuf::underflow | ( | ) | [inline, virtual] |
C.f. C++ standard section 27.5.2.4.3.
Definition at line 206 of file python_streambuf.h.
Referenced by seekoff(), and showmanyc().
const std::size_t boost_adaptbx::python::streambuf::default_buffer_size = 1024 [static] |
The default size of the read and write buffer.
They are respectively used to buffer data read from and data written to the Python file object. It can be modified from Python.
Definition at line 132 of file python_streambuf.h.
1.7.1