-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
io.BufferReader.read() returns None #80050
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
class io.BufferedIOBase states "In addition, those methods [read(), readinto() and write()] can raise BlockingIOError if the underlying raw stream is in non-blocking mode and cannot take or give enough data; unlike their RawIOBase counterparts, they will never return None." However, class.io.BufferedReader (inheriting from io.BufferedIOBase) *does* return None in this case. Admittedly, io.BufferedReader does says it is overriding the inherited method, but I'm surprised that change in behaviour declared for buffered objects, is reverted to the RarIOBase behaviour on a more specific class. The attached file (a little long - sorry), simulates a slow non-blocking raw file, which it wraps in a BufferReader to test the behaviour defined in BufferedIOBase. |
The description of read in io.BufferedReader.read function states "Read and return size bytes, or if size is not given or negative, until EOF or if the read call would block in non-blocking mode." This does mention the non-block mode scenario, but I can't parse this sentence. |
This is covered by bpo-13322. There are a few other BufferedReader methods that contradict the documentation for non-blocking mode. A while ago I posted a patch to change the implementation to match the documentation, but nobody reviewed it or gave their opinion. These days I would prefer to just documentat the reality: the methods might raise an exception rather than returning None, or perhaps no particular behaviour at all is expected in general in the non-blocking case. But I don’t spend much time on Python now, so you might have to find someone else to move this forward. Regarding the entry for “BufferedReader.read”, it would make more sense to remove the last “if”: “if ‘size’ is not given . . ., until EOF, or the ‘read’ call would block”. But I don’t think even that is complete, because it should also say the read stops and returns short in the non-blocking case even when “size” is positive. |
This synchronizes the documentation with the current implementation, with particular focus on behavior around non-blocking streams.
Synchronize `io.BufferedReader` and `io.BufferedIOBase` documentation with the current implementation. PArticular focus on behavior around non-blocking streams.
Synchronize `io.BufferedReader` and `io.BufferedIOBase` documentation with the current implementation. PArticular focus on behavior around non-blocking streams.
Synchronize `io.BufferedReader` and `io.BufferedIOBase` documentation with the current implementation. Focused on behavior around non-blocking streams.
I looked at changing this and found three cases reading through the
All three of these cases consistently return Could potentially add a new mode/flag which would cause it to raise, but Blocking I/O behavior is not well specified in the I do think the documentation should be updated to include the |
…ythonGH-130653) (cherry picked from commit e1f8914) Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
…cking (pythonGH-130653) (cherry picked from commit e1f8914) Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
@vadmium , @steverpalmer the docs are now updated to match implementation behavior, can this be closed? |
In short - maybe. I understand why the preference is not to change the behaviour of the code given its flying hours and exposure. It may well be that some code out in the wild has been written around the actual behaviour rather than the previously described behaviour. The corollary to this though is that the description can become complex and there needs to be caution in offering simplified summaries. (I'm quoting from https://docs.python.org/3/library/io.html#io.BufferedIOBase) The summary description in
The detailed description in
The first quote seems to suggest that, in the case under consideration, the read function will return None is no data is available, but the later quote extends this to allow that it might alternatively raise a I wondered about editing the first quote to match the second, but that felt like duplication and too much detail for a summary. It also seems superfluous to change the first quote (summary) to refer to the second quote (detail). I then wondered about removing the first quote completely - what does it really add? I suspect that it was originally written about a perfectly reasonable, arguably better design intention, but as we now know this intention was carried inconsistently to the implementation. Oddly, the one thing that the first quote does add, not included in v3.12 of the document and that isn't detailed later in the function detail, is that the My suggestion, then. is to delete the first summary quote entirely; the actual behaviour being described is too complex to simplify in a summary. I would also suggest that the
Nevertheless, the new text is better than the v3.12 text which is simply incorrect. Additionally. the reference from |
re: re: In any case, glad you find better. Will experiment some more on improving further. |
Thank you for your consideration of my comments. Best of luck with improving the documentation. I'm happy if you want to close this issue. Cheers. |
Uh oh!
There was an error while loading. Please reload this page.
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
The text was updated successfully, but these errors were encountered: