-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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-90533: Implement BytesIO.peek() #30808
base: main
Are you sure you want to change the base?
Changes from 25 commits
1e5e3ab
acdfe2e
b3f3c3d
2506967
a5ac601
9da7f9f
79d5032
2963dab
7d8793a
9bf291a
d4d5a55
9cdd231
d9948c8
69ddb4f
58a5d58
3d21011
4f4999f
56fbee3
4c3c908
1f2b5c5
a1504a7
827a785
eebd289
4855189
fd85b46
5733d5a
4d832e0
7a449e1
1d793e3
9b0b04f
bb6447d
611bdaa
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 |
---|---|---|
|
@@ -978,6 +978,13 @@ def tell(self): | |
raise ValueError("tell on closed file") | ||
return self._pos | ||
|
||
def peek(self, size=1): | ||
if self.closed: | ||
raise ValueError("peek on closed file") | ||
if size < 1: | ||
size = len(self._buffer) - self._pos | ||
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. The C implementation is differently implemented, which surprised me at first. 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. Do you mean that you would have expected something like 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 don't think that is needed. Perhaps a comment? 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 added a comment. Let me know if it’s too verbose now. 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. This code looks kind of complicated, whereas you can just do: if size < 1:
return self._buffer[self._pos:] 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. Changed |
||
return self._buffer[self._pos : self._pos + size] | ||
|
||
def truncate(self, pos=None): | ||
if self.closed: | ||
raise ValueError("truncate on closed file") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -517,6 +517,41 @@ def test_relative_seek(self): | |
memio.seek(1, 1) | ||
self.assertEqual(memio.read(), buf[1:]) | ||
|
||
def test_peek(self): | ||
buf = self.buftype("1234567890") | ||
with self.ioclass(buf) as memio: | ||
self.assertEqual(memio.tell(), 0) | ||
self.assertEqual(memio.peek(1), buf[:1]) | ||
self.assertEqual(memio.peek(1), buf[:1]) | ||
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. You can add tests reading 3 and 5 bytes? It seems like most tests read 1 byte or everything. 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. Absolutely, added now. |
||
self.assertEqual(memio.peek(), buf[:1]) | ||
self.assertEqual(memio.peek(0), buf) | ||
marcelm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.assertEqual(memio.peek(len(buf) + 100), buf) | ||
self.assertEqual(memio.peek(-1), buf) | ||
self.assertEqual(memio.tell(), 0) | ||
memio.read(1) | ||
self.assertEqual(memio.tell(), 1) | ||
self.assertEqual(memio.peek(1), buf[1:2]) | ||
self.assertEqual(memio.peek(1), buf[1:2]) | ||
self.assertEqual(memio.peek(), buf[1:2]) | ||
self.assertEqual(memio.peek(0), buf[1:]) | ||
self.assertEqual(memio.peek(len(buf) + 100), buf[1:]) | ||
self.assertEqual(memio.peek(-1), buf[1:]) | ||
self.assertEqual(memio.tell(), 1) | ||
memio.read() | ||
self.assertEqual(memio.tell(), len(buf)) | ||
self.assertEqual(memio.peek(1), self.EOF) | ||
self.assertEqual(memio.tell(), len(buf)) | ||
marcelm marked this conversation as resolved.
Show resolved
Hide resolved
marcelm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Peeking works after writing | ||
abc = self.buftype("abc") | ||
memio.write(abc) | ||
self.assertEqual(memio.peek(), self.EOF) | ||
memio.seek(len(buf)) | ||
self.assertEqual(memio.peek(), abc[:1]) | ||
self.assertEqual(memio.peek(-1), abc) | ||
self.assertEqual(memio.peek(len(abc) + 100), abc) | ||
self.assertEqual(memio.tell(), len(buf)) | ||
self.assertRaises(ValueError, memio.peek) | ||
|
||
def test_unicode(self): | ||
memio = self.ioclass() | ||
|
||
|
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.
I can understand that size=-1 or size=None return the whole content. But I'm surprised that size=0 returns something different than an empty string or raise an exception.
I suggest to return an empty string when peek(0) is called, it would be similar to read(0).
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.
Agreed, changed now.
This was originally for (perceived) consistency with
BufferedReader.peek()
, which does not return empty bytes objects for size=0. But thenBufferedReader.peek()
ignores the size anyway.