[C++-sig] Conversion of python files to C++ ostreams

Christopher Bruns cmbruns at stanford.edu
Sat Apr 3 18:56:56 CEST 2010

On Thu, Apr 1, 2010 at 7:11 PM, Ralf W. Grosse-Kunstleve <rwgk at yahoo.com> wrote:
>> Is it necessary to explicitly invoke the streambuf object from python?
> Yes. I could be different and in fact was different in the initial
> implementation (if you look back in the svn history). But there were
> a few subtle problems (buffer size, exception handling) and in the end
> I decided the most obvious and robust approach is to require
> streambuf(sys.stdout) or ostream(sys.stdout).

I finally got streambuf to work with my wrapped classes.  This feels
much better than the approach I was using before.  I added
"[with_custodian_and_ward< 1, 2 >()]" to the streambuf wrapper init
declaration, because I imagined it might be possible for the python
file to be destroyed before the streambuf otherwise.

Now I am faced with wrapping a constructor that takes a
"std::ostream&" as an argument.  I don't know how to write a wrapper
for a constructor in boost.python.  I'm open to suggestions on this.
For example:

// C++ API
struct Foo {
    Foo(ostream& os);

# desired python usage
foo = Foo(streambuf(sys.stdout))

How would I wrap this Foo example?

More information about the Cplusplus-sig mailing list