
On 06/07/2012 06:59 PM, Nathan Schneider wrote:
On Thu, Jun 7, 2012 at 5:14 PM, Rurpy <rurpy-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:
On 06/07/2012 03:45 PM, Nick Coghlan wrote: [...]
level code doesn't want those streams, it needs to replace them with something else.
Yes, this is what the code I googled up does: import codecs sys.stdout = codecs.getwriter(opts.encoding)(sys.stdout.buffer)
What if codecs contained convenience methods for stdin and stdout? I.e. the above could be written more simply as
import codecs codecs.encode_stdout(opts.encoding)
This is much more memorable than the current option, and would also make life easier when working with fileinput (whose openhook argument can be set to control encoding of input *file* streams, but when it falls back to stdin this preference is ignored).
How ironic. In Python2 I hated having to import codecs and use codecs.open() (the only thing I ever used from the codecs module) rather than just having an encoding parameter on open(). But seems like might be a reasonable thing to do. I'm sure there will be opinions. :-). It's not just sys.stdout though, the same issue exists with sys.stdin and sys.stderr so one might want either three functions, or one function that includes the a stream as parameter.