[C++-sig] Redirecting stdout for B.P extensions?

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Thu Sep 15 21:33:48 CEST 2005


--- Jonathan Brandmeyer <jbrandmeyer at earthlink.net> wrote:
> > Question: how can I redirect std::cout and std::cerr to use a custom
> streambuf
> > object? Can I simply write std::cout = my_custom_streambuf_instance? That
> would
> > be exciting.
> 
> std::streambuf* old = std::cout.rdbuf( &my_custom_streambuf_instance);

Cool. Thanks!

> Here is a start that adapts some of your code to suit.  
> 
> After importing sbuf_test, the program will crash on shutdown when cout
> and cerr's destructors try to flush the Python stream that has already
> been finalized.  Perhaps this would be a good point to add a PyFinalize
> hook that restores the streambufs?

I looked around a bit in the Python C API docs. The closest I could find is
Py_AtExit(), but to me it seems that the registered functions are called too
late, i.e. after sys.stdout is finalized already. Is there another hook I
missed?

Cheers,
        Ralf



		
__________________________________ 
Yahoo! Mail - PC Magazine Editors' Choice 2005 
http://mail.yahoo.com



More information about the Cplusplus-sig mailing list