[Python-ideas] changing sys.stdout encoding

Stephen J. Turnbull stephen at xemacs.org
Tue Jun 5 21:37:16 CEST 2012

Rurpy writes:

 > It is excessively complex for what is conceptually a simple and
 > straight-forward operation.

The operation is not conceptually straightforward.  The problem is
that you can't just change the encoding of an open stream, encodings
are generally stateful.  The straightforward way to deal with this
issue is to close the stream and reinitialize it.  Your proposed
.set_encoding() method implies something completely different about
what's going on.

I wouldn't object to a method with the semantics of reinitialization,
but it should have a name implying reinitialization.  It probably
should also error if the stream is open and has been written to.

 > Needing to change the encoding of a sys.std* stream is not an 
 > uncommon need and a user should not have to go through the 
 > codecs dance above to do so IMO.

I suspect needing to *change* the encoding of an open stream is
generally quite rare.  Needing to *initialize* the std* streams with
an appropriate codec is common.  That's why it doesn't so much matter
that PYTHONIOENCODING can't be changed within a program.

I agree that use of PYTHONIOENCODING is pretty awkward.

More information about the Python-ideas mailing list