the stupid encoding problem to stdout

Ian Kelly ian.g.kelly at gmail.com
Fri Jun 10 12:58:56 EDT 2011


2011/6/10 Sérgio Monteiro Basto <sergiomb at sapo.pt>:
> ok after thinking about this, this problem exist because Python want be
> smart with ttys, which is in my point of view is wrong, should not encode to
> utf-8, because tty is in utf-8. Python should always encode to the same
> thing. If the default is ascii, should always encode to ascii.
> yeah should send to tty in ascii, if I send my code to a guy in windows
> which use tty with cp1000whatever , shouldn't give decoding errors and
> should send in ascii .

You can't have your cake and eat it too.  If Python needs to output a
string in ascii, and that string can't be represented in ascii, then
raising an exception is the only reasonable thing to do.  You seem to
be suggesting that Python should do an implicit output.encode('ascii',
'replace') on all Unicode output, which might be okay for a TTY, but
you wouldn't want that for file output; it would allow Python to
silently create garbage data.

And what if you send your code to somebody with a UTF-16 terminal?
You try to output ASCII to that, and you're just going to get complete
garbage.

If you want your output to behave that way, then all you have to do is
specify that with an explicit encode step.

> If we want we change default for whatever we want, but without this "default
> change" Python should not change his behavior depending on output.
> yeah I prefer strange output for a different platform, to a decode errors.

Sorry, I disagree.  If your program is going to fail, it's better that
it fail noisily (with an error) than silently (with no notice that
anything is wrong).



More information about the Python-list mailing list