Make stdout.write() return number of bytes actually written? #11807
thingsconnected
started this conversation in
Ideas
Replies: 2 comments 1 reply
-
After extensive testing, I can confirm that making
Any feedback or thoughts are more than welcome. |
Beta Was this translation helpful? Give feedback.
0 replies
-
@thingsconnected MicroPython should match CPython's behavior, and I agree that the proposed change also makes sense. I think it sounds like it's worth submitting a PR. Thanks! |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Not sure if this is a bug or a feature, but it seems that
sys.stdout.write()
returns the length of the data that should have been written, regardless of the number of bytes actually written. This is confusing behavior when dealing with data loss issues. I have seen no warnings about this type of behavior in the documentation.I've been researching an issue where after a few hours of runtime, incidentally chunks are missing from the data stream sent out via USB, and I've narrowed it down to somewhere between main.py code
sys.stdout.write(buf)
and the receiving end (an ARM based Linux device with ACM driver).It seems that in the MicroPython core code, this is how stdout data is sent, in
runtime/sys_stdio_mphal.c
:This calls
mp_hal_stdout_tx_strn_cooked()
and returns the number of (uncooked) bytes that should have been written, no matter what.Likewise, for
sys.stdout.buffer.write()
:From
mp_hal_stdout_tx_strn_cooked()
,mp_hal_stdout_tx_strn()
is called as well, which is board specific, and in my case inports/rp2/mphalport.c
:This looks like it might be the source of my issue. (Or maybe also that
timeout
is counting more than just millis... but that's a different topic.)Since I encountered this issue at the very start of my first MicroPython project, I suspect other people will run into this. So the idea I would like to share is to make
stdout.write()
behave like any otherwrite()
call, returning the number of bytes actually written. I am willing to write and submit the patch, but before doing so I'd like to know whether there are reasons why such behavior is not already implemented. One related issue I found was #7479 where someone is using USB stdout to transfer large amounts of data - so there is definitely a use case for high volume and reliablesys.stdout.write()
.Beta Was this translation helpful? Give feedback.
All reactions