All you have to do is use sys.stdin.buffer and sys.stdout.buffer; the caveat is that if sys.stdin has been replaced with a StringIO instance, this won't work. But in Armin's simple cat example, we can trivially make sure that won't happen.
I'd be a lot more willing to listen to this argument if it didn't overlook basic stuff like this.
Yes, the documentation mentions that you can use buffer and follows that by a sentence explaining that you can do this unless you can't:
> Note that the streams may be replaced with objects (like io.StringIO) that do not support the buffer attribute or the detach() method and can raise AttributeError or io.UnsupportedOperation.
So no this is neither basic nor easy to do correctly in general. That's only the case, if you are writing an application and use well-behaved libraries that handle the edge cases you introduce.
I guess it's a little odd that Python 3 treats stdin and stdout by default as unicode text streams. And sys.argv is a list of unicode strings, too, instead of bytes.
I like Python 3's unicode handling but I agree that this seems strange. It is because people expect to see "characters" from these interfaces after treating them as ASCII-only for so long. If Python 3 had insisted on real purity with bytes objects I think it would have died a long time ago. Which is sad.
> I'd be a lot more willing to listen to this argument if it didn't overlook basic stuff like this.
Just because there's a way around this particular issue doesn't mean that the attitude of Unicode by default of Python 3 isn't problematic. There's also sys.argv, os.listdir, and other filename stuff which Python 3 attempts to decode.
https://docs.python.org/3/library/sys.html#sys.stdin
All you have to do is use sys.stdin.buffer and sys.stdout.buffer; the caveat is that if sys.stdin has been replaced with a StringIO instance, this won't work. But in Armin's simple cat example, we can trivially make sure that won't happen.
I'd be a lot more willing to listen to this argument if it didn't overlook basic stuff like this.