ecto
Classes | Functions
ecto::py Namespace Reference

Classes

class  gil
 
struct  gilstatus
 
struct  istream
 
class  nothing_to_lock
 
struct  ostream
 
class  scoped_call_back_to_python
 
class  scoped_gil_release
 
class  streambuf
 
struct  streambuf_capsule
 

Functions

std::string file_and_line_as_string (const char *file, long line)
 A stream buffer getting data from and putting data into a Python file object. More...
 
std::string repr (const boost::python::object &obj)
 
void postregistration (const std::string &, const std::string &, const std::string &)
 

Function Documentation

std::string ecto::py::file_and_line_as_string ( const char *  file,
long  line 
)
inline

A stream buffer getting data from and putting data into a Python file object.

The aims are as follow:

  • Given a C++ function acting on a standard stream, e.g.
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.

  • When Python read_inputs() returns, the Python object is able to continue reading or writing where the C++ code left off.
  • Operations in C++ on mere files should be competitively fast compared to the direct use of std::fstream.

Motivation

  • the standard Python library offer of file-like objects (files, compressed files and archives, network, ...) is far superior to the offer of streams in the C++ standard library and Boost C++ libraries.
  • i/o code involves a fair amount of text processing which is more efficiently prototyped in Python but then one may need to rewrite a time-critical part in C++, in as seamless a manner as possible.

Usage

This is 2-step:

  • a trivial wrapper function
using ecto::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.

  • the Python side
from boost.python import streambuf
read_inputs(streambuf(python_file_obj=obj, buffer_size=1024))

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).

void ecto::py::postregistration ( const std::string &  ,
const std::string &  ,
const std::string &   
)
std::string ecto::py::repr ( const boost::python::object &  obj)