QUERY · ISSUE
Binary stdin and stdout buffers for Unix port
enhancement
$ micropython
>>> import sys
>>> sys.stdout.buffer
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'TextIOWrapper' object has no attribute 'buffer'
>>>
Well, other ports have it … CPython has it … and I'd really like to not have to fall back to open("/dev/stdout","wb") in my unit tests.
Same for stdin of course.
CANDIDATE · PULL REQUEST
stmhal: Implement sys.std{in,out,err}.buffer, for raw byte mode.
This is proof of concept (and not necessarily going to be merged) for adding sys.stdin.buffer, sys.stdout.buffer and sys.stderr.buffer objects. These are raw, uncooked, bytes versions of sys.std{in,out,err}.
It costs 170 bytes ROM. See issue #1260 for background.
For comparison with cooked version:
>>> sys.stdout.write('aaa\na')
aaa
a5
>>> sys.stdout.buffer.write('aaa\na')
aaa
a5
>>> sys.stdin.read(1) # then hit enter as the character to read
'\n'
>>> sys.stdin.buffer.read(1) # then hit enter as the character to read
b'\r'