[C++-sig] Conversion of python files to C++ ostreams
Ralf W. Grosse-Kunstleve
rwgk at yahoo.com
Fri Apr 2 04:11:44 CEST 2010
> I like that the streambuf class appears to work with gzip files and
> other file objects that presumably do not have a FILE* internally.
Yes, because it simply uses the Python methods .read(), .write() etc.
> 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 think it is safe to re-use the same streambuf, e.g.
out = ostream(sys.stdout)
which is almost what you want.
But I'd avoid "out" as a global because output may get stuck in
the buffer and only appear surprisingly in unrelated contexts.
Note that buffer_size=1 introduces a noticeable performance
penalty. See time_it() in boost_adaptbx/tests/tst_python_streambuf.py .
More information about the Cplusplus-sig