Rebinding stdout (was: Re: Python! Is! Truly! Amazing!)

michele.simionato at gmail.com michele.simionato at gmail.com
Mon Jan 3 09:40:04 EST 2005


Just wrote:
> In article <87u0py6elt.fsf at sme.intra.citec.fi>,
>  Simo Melenius <firstname.lastname at iki.fi-spam> wrote:
>
> > I've sometimes replaced sys.stdout (and/or sys.stderr) to
> > capture/redirect debugging information in existing code that has
> > unwisely just "print"ed error and warning messages, instead of
using
> > sys.stderr or error logging modules.
> >
> > py> def with_output_to_string (func):
> > ...     try:
> > ...         sys.stdout = StringIO.StringIO ()
> > ...         func ()
> > ...         return sys.stdout.getvalue ()
> > ...     finally:
> > ...         sys.stdout = sys.__stdout__
>
> Aargh, I can't believe how widespread this idiom is :-(. See my other
> reply in this thread: DON'T use sys.__stdout__. Ever.
> 
> Just




More information about the Python-list mailing list