[Python-ideas] changing sys.stdout encoding

Stephen J. Turnbull stephen at xemacs.org
Wed Jun 13 06:58:28 CEST 2012

Oscar Benjamin writes:

 > I also think I missed something in this thread. At the beginning of the
 > original thread it seemed that everyone was agreed that
 >   writer = codecs.getwriter(desired_encoding)
 >   sys.stdout = writer(sys.stdout.buffer)
 > was a reasonable solution (with the caveat that it should happen before any
 > output is written). Is there some reason why this is not a good
 > approach?

It's undocumented and unobvious, but it's needed for standard stream
filtering in some environments -- where a lot of coding is done by
people who otherwise never need to understand streams at anything but
a superficial level -- and the analogous case of a newly opened file,
pipe, or socket is documented and obvious, and usable by novices.

It's damn shame that we can't say the same about the stdin, stdout,
and stderr streams (even if I too have been at pains to explain why
that's hard to fix).

More information about the Python-ideas mailing list