-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
sys.stdin.read() throws a TypeError when stdin is set to be non-blocking #109523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Please assign this issue to me |
This would be a behaviour change so would only go into 3.13. @benjaminp and @gpshead are listed as experts for IO, so copying them for thoughts. A |
I agree that read() on a non-blocking file object should never raise TypeError. The internal People expecting that not to happen at all won't like the TypeErrors at the level of their own code, by putting stdin into non-blocking mode they signed up for that... the error now comes directly from their code and could be handled without a try: except: by checking the return value rather than from within our internals where it seems questionable. Do note that typeshed says io.TextIOBase.read only ever returns If people want non blocking IO I really wish they'd avoid layering things like text mode wrappers on top of it, or go full-on async for such things. |
As for why .read() shouldn't just return So having it return an empty string could remain consistent return type wise and within a reasonable set of expectations for something in non-blocking mode. |
Per my note on https://discuss.python.org/t/handling-sys-stdin-read-in-non-blocking-mode/59633/6, lets go with raising a |
I’ve created a new pull request with the required changes to I would appreciate some guidance on where to document this change, as I’m not entirely sure of the best approach. If someone else is available to handle the documentation, that would also be perfectly fine. |
For documentation, I think a note in the I/O docs TextIO section and BufferedIO would help people understand the behavior better from just the docs without having to go lookup the Python 3 I/O PEP. Something around that passing or changing a file descriptor which to be non-blocking is unsupported, and that generally the standard library will try to raise a NonBlockingIO error if non-blocking I/O is encountered. It might also make sense to add a similar note specifically around read That documentation is generated from the file: https://github.com/python/cpython/blob/main/Doc/library/io.rst |
Thanks for the suggestion! I've added notes to the I/O docs to clarify how I hope these changes hit the mark and make things clearer for everyone! |
Just dropping a friendly ping here. This issue has a pull request that is in good shape and has undergone intensive review. Could a maintainer kindly take a look for final review and, if everything looks good, consider merging it? |
…g stream cannot immediately return bytes. (GH-122933)
Merged. Thank you for your perserverance, @giosiragusa! |
…locking stream cannot immediately return bytes. (pythonGH-122933)
…locking stream cannot immediately return bytes. (pythonGH-122933)
Bug report
Bug description:
sys.stdin.read() throws a TypeError if stdin has been set to be non-blocking. The code below should just exit without issue. It throws a TypeError if no input is provided.
CPython versions tested on:
3.11
Operating systems tested on:
Linux
Linked PRs
The text was updated successfully, but these errors were encountered: