[Python-Dev] sys.__stdout__ - was RE: [Idle-dev] Doc Commentary
Mark Hammond
MarkH@ActiveState.com
Thu, 26 Oct 2000 10:29:52 +1100
[From a mail in idle-dev - dropped idle-dev and all CCs]
[idle-dev poster]
> > >>> import sys
> > >>> f = open('some-output-file', 'w')
> > >>> sys.stdout = f
> > >>> # do some stuff
> > >>> sys.stdout = sys.__stdout__
> >
> > Because on entry, sys.stdout is already redirected
> > (not reflected in sys.__stdout__)
[Guido]
> Where did you get this idiom? You should never use sys.__stdout__
> this way! The proper idiom is
>
> save_stdout = sys.stdout
> try:
> sys.stdout = f
> # do some stuff
> finally:
> sys.stdout = save_stdout
>
> If you know of any places that are promoting direct use of __stdout__,
> please let them know this is wrong. If you are contemplating to use
> this in code examples you intend to distribute, please don't!
I admit that I first became aware of sys.__stdout__ after seeing this exact
same idiom in other peoples code. I always assumed that this is _exactly_
what sys.__stdout__ was to be used for!
The following examples exist:
* Tools/unicode/makeunicodedata.py - exactly the idiom you despise.
* Tools/il8n/pygettext.py - ditto
* Tools/idle/PyParse.py - disabled, debugging output function - presumably
to get around sys.stdout redirection - but this sounds suspect as an idiom
to me too!
So what _is_ it intended for?
Mark.