Gustavo Niemeyer wrote:
The idiom presented by Bob is the right way to go: wrap sys.stdout with a StreamWriter.
I don't see that as a good solution, since every Python software that is internationalizaed will have do figure out this wrapping, introducing extra overhead unnecessarily.
I don't see any unnecessary overhead and using the wrappers is really easy, e.g.:
# # Application uses Latin-1 for I/O, terminal uses UTF-8 # import codecs, sys
# Make stdout translate Latin-1 output into UTF-8 output sys.stdout = codecs.EncodedFile(sys.stdout, 'latin-1', 'utf-8')
# Have stdin translate Latin-1 input into UTF-8 input sys.stdin = codecs.EncodedFile(sys.stdin, 'utf-8', 'latin-1')
We should probably extend the support in StreamRecoder (which is used by the above EncodedFile helper) to also support Unicode input to .write() and have a special codec 'unicode' that converts Unicode to Unicode, so that you can request the EncodedFile object to return Unicode for .read().