Binary over sys.stdin question, sys.stdin is type TextIOWrapper, not FileIO? #12070
-
Hello all, Brief, I'm writing a CLI tool with micropython where I need to pipe in binary over stdin. On unix port, I'type of sys.stdin is a TextIOWrapper. Conversely, on ESP32, sys.stdin is a FileIO with sys.stdin.buffer available for use. Why not FileIO which would provide text and buffered versions? I have a naive workaround, this feels like a total hack, BUT, if I change is_text from true to false in extmod/vfs_posix_file.c, I no longer get unicode error, reading every raw byte value from stdin as expected.
Naturally this breaks my compatibility and portability. Any other options (or even better, something completely obvious that I don't know yet! 😉 ) for binary of stdin on unix port? Micropython rocks! 🚀 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
This is the correct behavior. However what's missing is that it should have a
Confusingly, despite being named FileIO, this object (on all the embedded ports) is actually a TextIOWrapper-like implementation. It is a text stream, and supports the .buffer attribute to get the underlying binary stream. I don't know the history, but I'm guessing it's called "FileIO" just to save the additional code size for "TextIOWrapper", but if you look at
I think the solution is just to provide the buffer attribute on the unix version (in vfs_posix_file.c), which should be do-able using basically the same approach as sys_stdio_mphal.c. |
Beta Was this translation helpful? Give feedback.
@stephanelsmith
This is the correct behavior. However what's missing is that it should have a
.buffer
attribute to access the underlying binary stream -- this is the CPython-compatible way to do this (see the note at the end of the sys.stdin docs -- https://docs.python.org/3/library/sys.html#sys.stderr ).Confusingly, despite being named FileIO, this object (on all the embedded ports) is actually a TextIOWrapper-like implementation. It is a text stream, and supports the .buffer attribute to get the underlying binary stream.
I don't know the history…