-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-90533: Implement BytesIO.peek() #30808
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
base: main
Are you sure you want to change the base?
Changes from all commits
b833b83
50a2cfb
eaa7672
00457ae
882579d
c1eed72
afc200c
79ab9a4
b493914
2a1c85c
d398717
26d1e81
9a19ff9
9300ade
d214089
d6691b8
3e51adb
3661b65
cd40d77
04372bd
6b9ae8c
f7406f6
d9528e2
bc8134b
b6ffca8
5fe5645
4126a64
1ea40c2
77e04d6
4d2f2dd
c16bebf
08bd7da
6174fca
b8b8cf4
7ac914e
abbd8f0
07d9e4d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -739,6 +739,15 @@ than raw I/O does. | |
|
|
||
| Return :class:`bytes` containing the entire contents of the buffer. | ||
|
|
||
| .. method:: peek(size=0, /) | ||
|
|
||
| Return bytes from the current position onwards without advancing the position. | ||
| At least one byte of data is returned if not at EOF. | ||
| Return an empty :class:`bytes` object at EOF. | ||
| The number of read bytes depends on the buffer size | ||
| and the current position in the internal buffer. | ||
|
|
||
| .. versionadded:: 3.15 | ||
|
|
||
| .. method:: read1(size=-1, /) | ||
|
|
||
|
|
@@ -772,8 +781,13 @@ than raw I/O does. | |
|
|
||
| .. method:: peek(size=0, /) | ||
|
|
||
| Return bytes from the stream without advancing the position. The number of | ||
| bytes returned may be less or more than requested. If the underlying raw | ||
| Return bytes from the current position onwards without advancing the position. | ||
| At least one byte of data is returned if not at EOF. | ||
| Return an empty :class:`bytes` object at EOF. | ||
| At most one single read on the underlying raw stream is done to satisfy the call. | ||
| The *size* argument is ignored. | ||
| The number of read bytes depends on the buffer size and the current position in the internal buffer. | ||
| If the underlying raw | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cmaloney Can you confirm whether this change of the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually think it's inaccurate (it doesn't actually guarantee a single read since PEP-0475 EINTR retry was added...). To me is out of scope for this change (Adding BytesIO.peek). Possibly worth fixing in a separate github issue + discussion. |
||
| stream is non-blocking and the operation would block, returns empty bytes. | ||
|
|
||
| .. method:: read(size=-1, /) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add :meth:`io.BytesIO.peek`. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should something like this be added here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would keep it an undocumented implementation detail. Most people won't care and can explicitly specify a size to get the better
BytesIO.peekbehavior that will hopefully get extended in 3.16 toBufferedReader.peek. There's more options and tradeoffs there (ex. if we want the max to be configurable could potentially add amax_sizekeyword argument to peek).