Skip to content

gh-80050: Update BufferedReader.read docs around non-blocking #130653

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

Merged
merged 15 commits into from
May 21, 2025

Conversation

cmaloney
Copy link
Contributor

@cmaloney cmaloney commented Feb 27, 2025

Synchronize io.BufferedReader and io.BufferedIOBase documentation with the current implementation. Focused on behavior around non-blocking streams.


📚 Documentation preview 📚: https://cpython-previews--130653.org.readthedocs.build/

Synchronize `io.BufferedReader` and `io.BufferedIOBase` documentation
with the current implementation. Focused on behavior around non-blocking
streams.
@bedevere-app bedevere-app bot added docs Documentation in the Doc dir skip news awaiting review labels Feb 27, 2025
@github-project-automation github-project-automation bot moved this to Todo in Docs PRs Feb 27, 2025
@cmaloney cmaloney changed the title gh-80050: Update BufferedReader.read around non-blocking gh-80050: Update BufferedReader.read docs around non-blocking Feb 27, 2025
BufferedReader at least just forwards `None` from the underlying stream.

1. `BufferedReader.read(size=10)` `raw.read(size=10)` returns None
2. `BufferedReader.read()` without size, raw implements `readall()` which returns None
3. `BufferedReader.read()` without size, raw doesn't have readall so uses a loop that calls `raw.read()` which returns None.
@cmaloney
Copy link
Contributor Author

cc: @gpshead

@@ -826,8 +823,8 @@ than raw I/O does.

Write the :term:`bytes-like object`, *b*, and return the
number of bytes written. When in non-blocking mode, a
:exc:`BlockingIOError` is raised if the buffer needs to be written out but
the raw stream blocks.
:exc:`BlockingIOError` with :attr:`BlockingIOError.characters_written` set
Copy link
Contributor Author

@cmaloney cmaloney May 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

characters_written is fairly well tested in this case

cpython/Lib/test/test_io.py

Lines 1918 to 1943 in a7f317d

def test_write_non_blocking(self):
raw = self.MockNonBlockWriterIO()
bufio = self.tp(raw, 8)
self.assertEqual(bufio.write(b"abcd"), 4)
self.assertEqual(bufio.write(b"efghi"), 5)
# 1 byte will be written, the rest will be buffered
raw.block_on(b"k")
self.assertEqual(bufio.write(b"jklmn"), 5)
# 8 bytes will be written, 8 will be buffered and the rest will be lost
raw.block_on(b"0")
try:
bufio.write(b"opqrwxyz0123456789")
except self.BlockingIOError as e:
written = e.characters_written
else:
self.fail("BlockingIOError should have been raised")
self.assertEqual(written, 16)
self.assertEqual(raw.pop_written(),
b"abcdefghijklmnopqrwxyz")
self.assertEqual(bufio.write(b"ABCDEFGHI"), 9)
s = raw.pop_written()
# Previously buffered bytes were flushed
self.assertTrue(s.startswith(b"01234567A"), s)

@github-project-automation github-project-automation bot moved this from Todo to In Progress in Sprint 2024 May 21, 2025
@gpshead gpshead added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels May 21, 2025
@gpshead gpshead enabled auto-merge (squash) May 21, 2025 16:02
@gpshead gpshead merged commit e1f8914 into python:main May 21, 2025
31 of 32 checks passed
@github-project-automation github-project-automation bot moved this from Todo to Done in Docs PRs May 21, 2025
@github-project-automation github-project-automation bot moved this from In Progress to Done in Sprint 2024 May 21, 2025
@miss-islington-app
Copy link

Thanks @cmaloney for the PR, and @gpshead for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 21, 2025
@miss-islington-app
Copy link

Sorry, @cmaloney and @gpshead, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker e1f891414b2329414a6160ed246f5f869a218bfd 3.13

@bedevere-app
Copy link

bedevere-app bot commented May 21, 2025

GH-134444 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label May 21, 2025
@cmaloney cmaloney deleted the docs_buffferedreader branch May 21, 2025 16:07
gpshead pushed a commit that referenced this pull request May 21, 2025
…H-130653) (#134444)

gh-80050: Update BufferedReader.read docs around non-blocking (GH-130653)
(cherry picked from commit e1f8914)

Co-authored-by: Cody Maloney <[email protected]>
gpshead pushed a commit to gpshead/cpython that referenced this pull request May 21, 2025
@bedevere-app
Copy link

bedevere-app bot commented May 21, 2025

GH-134445 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label May 21, 2025
gpshead added a commit that referenced this pull request May 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip news sprint
Projects
Status: Done
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants