
Hi Christopher and Early,
...I thought there is some standard Boost.Python stream somewhere anyway
and didn't expect this kind of solution :)
The code became longer than before (at least when calculating the
definition of the sink class also) - but much more elegant :)
Before:
std::ostringstream oss;
oss << "Hello, Python world!" << std::endl;
std::string cs = oss.str();
PySys_WriteStdout(cs.c_str());
Now:
boost::iostreams::stream total 8
-rw-r--r-- 1 dietrich dietrich 131 2009-05-14 22:56 Jamfile
-rw-r--r-- 1 dietrich dietrich 0 2009-05-14 22:56 project-root.jam
-rw-r--r-- 1 dietrich dietrich 982 2009-05-14 22:56 pysys_stdout.cpp bjam [...] bin/gcc-4.3.3/debug/pysys_stdout Hello, Python world! On Wed, 2009-05-13 at 01:11 -0700, Christopher Currie wrote: On Mon, May 11, 2009 at 2:17 PM, Early Ehlinger Basically, you need to create a streambuf class that calls
PySys_WriteStdout() inside your_streambuf_class::sync(). It's probably fairly straightforward to do this using Boost.Iostreams,
by creating a model of Sink: include #include <algorithm> // min
#include <iosfwd> // streamsize
#include class pysys_stdout_sink {
public:
typedef char char_type;
typedef boost::iostreams::sink_tag category; std::streamsize write( const char* s, std::streamsize n ) {
// PySys_WriteStdout truncates to 1000 chars
static const std::streamsize MAXSIZE = 1000; std::streamsize written = std::min( n, MAXSIZE );
PySys_WriteStdout( (boost::format("%%.%1%s") %
written).str().c_str(), s ); return written;
}
}; boost::iostreams::stream int main()
{
Py_Initialize();
pysys_stdout << "Hello, Python world!\n";
} HTH,
Christopher
_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users