Visitar URL original
gh-100226: Clarify StreamReader.read behavior by jgosmann · Pull Request #101807 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20230216074637/https://github.com/python/cpython/pull/101807
Skip to content
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

gh-100226: Clarify StreamReader.read behavior #101807

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jgosmann
Copy link

@jgosmann jgosmann commented Feb 10, 2023 •

This clarifies the StreamReader.read behavior in the public documentation; mostly copying from the more precise docstring of the function.

While we're at it, also fix some minor grammar in the docstring itself.

Addresses #100226.

Should this be backported so that the documentation for older releases is also updated?

This clarifies the `StreamReader.read` behavior in the public
documentation; mostly copying from the more precise docstring of the
function.

While we're at it, also fix some minor grammar in the docstring itself.

Addresses python#100226.
@bedevere-bot
Copy link

Most changes to Python require a NEWS entry.

Please add it using the blurb_it web app or the blurb command-line tool.

@cpython-cla-bot
Copy link

cpython-cla-bot bot commented Feb 10, 2023 •

All commit authors signed the Contributor License Agreement.
CLA signed

@jgosmann
Copy link
Author

This does not seem to require a NEWS entry to me, but I'm happy to add one if I'm mistaken.

Copy link
Member

@CAM-Gerlach CAM-Gerlach left a comment

Choose a reason for hiding this comment

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

Standard reminder: You can directly apply all the suggestions you want in one go with Files changed -> Add to batch -> Commit

Sorry for the delay reviewing this. This is certainly an improvement—I'd suggest a number of further clarifications and revisions to fix things that still initially confused me.

If *n* is not provided, or set to ``-1``,
read until EOF. After EOF is received, return all read bytes.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
If *n* is not provided, or set to ``-1``,
read until EOF. After EOF is received, return all read bytes.
If *n* is not provided or set to ``-1``,
read until EOF (``EOT``, ``b"\x04"``/:kbd:`Ctrl-D`) is received,
then return all read :class:`bytes`.
  • Clarify that in this context (at least, I'm assuming this is the case if this follows the Unix read semantics like Guido indicated), EOF is specifically the EOT character code, rather than SUB (\x1A/Ctrl-Z, the Windows EOF), some other byte or some other out of band signalling.
  • Join the sentences accordingly to avoid repetition the same
  • Be more explicit and cross-reference the returned data type
  • Elide unnecessary internal comma

If EOF was received and the internal buffer is empty,
return an empty ``bytes`` object.

If *n* is zero, return empty ``bytes`` object immediately.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
If *n* is zero, return empty ``bytes`` object immediately.
If *n* is ``0``, return an empty ``bytes`` object immediately.
  • Fix grammar
  • Use the appropriate literal

If *n* is positive, this function tries to read *n* bytes, and may return
less or equal bytes than requested, but at least one byte. If EOF was
received before any byte is read, this function returns an empty
``bytes`` object.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
If *n* is positive, this function tries to read *n* bytes, and may return
less or equal bytes than requested, but at least one byte. If EOF was
received before any byte is read, this function returns an empty
``bytes`` object.
If *n* is positive, read at most *n* bytes.
May return less than *n* bytes, if EOF is reached first.
If EOF is received before any byte is read,
return an empty ``bytes`` object.
  • Specify the actual reason for less than n bytes being returned—EOF is reached first (assuming I'm interpreting this correctly, and it parallels the -1 case).
  • Avoid apparent contradictory statement that at least one byte will be returned, yet can also return empty bytes if EOF is received first
  • Split first sentence into two, for clarity and readability
  • Elide redundant (and imprecise) "this function" verbiage, and be consistent with the phrasing used elsewhere here

@@ -206,12 +206,20 @@ StreamReader

.. coroutinemethod:: read(n=-1)

Read up to *n* bytes. If *n* is not provided, or set to ``-1``,
read until EOF and return all read bytes.
Read up to *n* bytes.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Read up to *n* bytes.
Read up to *n* bytes from the stream.

For explicitness, and to match the docstring version

If n is positive, this function try to read `n` bytes, and may return
If n is positive, this function tries to read `n` bytes, and may return
less or equal bytes than requested, but at least one byte. If EOF was
received before any byte is read, this function returns empty byte
received before any byte is read, this function returns an empty bytes
Copy link
Member

Choose a reason for hiding this comment

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

I suggest porting the refined version from the above here as well.

@CAM-Gerlach
Copy link
Member

CAM-Gerlach commented Feb 14, 2023 •

Should this be backported so that the documentation for older releases is also updated?

Yes; standard practice is to backport any docs-only fixes that affect bugfix-supported branches, as it fixes a docs defect and ensures that other changes can be backported cleanly too. I've added the appropriate tags to line up the backports when this is merged.

This does not seem to require a NEWS entry to me, but I'm happy to add one if I'm mistaken.

No need, you're quite right—it's a docs-only fix.

@gvanrossum
Copy link
Member

No time to review today, but once CAM approves I'll skim and merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants