Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
b833b83
Implement BytesIO.peek()
marcelm Jan 22, 2022
50a2cfb
📜🤖 Added by blurb_it.
blurb-it[bot] Apr 10, 2022
eaa7672
Document BytesIO.peek()
marcelm Nov 9, 2022
00457ae
Implement with the help of read_bytes()
marcelm Nov 9, 2022
882579d
Add to What’s New
marcelm Jul 8, 2023
c1eed72
versionadded: 3.12 -> 3.13
marcelm Jul 8, 2023
afc200c
Remove unused variable
marcelm Jul 8, 2023
79ab9a4
Test tell() after peek()
marcelm Sep 22, 2023
b493914
Update docs, factor out peek_bytes, semantics
marcelm Sep 22, 2023
2a1c85c
Update Misc/NEWS.d/next/Library/2022-04-10-20-10-59.bpo-46375.8j1ogZ.rst
marcelm Sep 28, 2023
d398717
Update Modules/_io/bytesio.c
marcelm Sep 28, 2023
26d1e81
Update Modules/_io/bytesio.c
marcelm Sep 28, 2023
9a19ff9
Use SemBr
marcelm Sep 28, 2023
9300ade
Update Doc/whatsnew/3.13.rst
marcelm Sep 28, 2023
d214089
Apply suggestions from code review
marcelm Sep 28, 2023
d6691b8
Use a context manager around memio in test_peek
marcelm Sep 28, 2023
3e51adb
Add more tests for tell() after peek()
marcelm Sep 28, 2023
3661b65
Document why size < 0 can happen
marcelm Sep 28, 2023
cd40d77
Update Modules/_io/bytesio.c
marcelm Sep 29, 2023
04372bd
Do not update pos if peek_bytes failed
marcelm Sep 29, 2023
6b9ae8c
Size can be negative after truncate or seek
marcelm Sep 29, 2023
f7406f6
Test with size<0 and size>len(buf)
marcelm Sep 29, 2023
d9528e2
Test peek() after write()
marcelm Sep 29, 2023
bc8134b
Document BufferedReader.peek and BytesIO.peek similarly
marcelm Sep 29, 2023
b6ffca8
Comment
marcelm Sep 29, 2023
5fe5645
Make it more explicit that size is ignored
marcelm Sep 29, 2023
4126a64
Return an empty bytes object for size=0
marcelm Oct 23, 2023
1ea40c2
Simplify
marcelm Oct 23, 2023
77e04d6
Test peek(3) and peek(5)
marcelm Oct 23, 2023
4d2f2dd
Run clinic.py
marcelm Apr 10, 2026
c16bebf
Apply suggestions from code review
marcelm Apr 13, 2026
08bd7da
Do not return an empty bytes object for size=0
marcelm Apr 11, 2026
6174fca
Decorate BytesIO.peek with `@critical_section`
marcelm Apr 15, 2026
b8b8cf4
Test peek returns EOF after seeking to EOF
marcelm Apr 21, 2026
7ac914e
Free-threading test for peek
marcelm Apr 21, 2026
abbd8f0
Fix free-threading test
marcelm Apr 27, 2026
07d9e4d
Default to size=0; cap to DEFAULT_BUFFER_SIZE
marcelm Apr 29, 2026
3d57f45
Revert changes to BufferedReader.peek documentation
marcelm Apr 30, 2026
023ad25
Test peek after seek or truncate
marcelm May 1, 2026
203749b
Merge branch 'main' into fix-issue-46375
emmatyping May 1, 2026
32fd791
Do not cap at DEFAULT_BUFFER_SIZE
marcelm Jun 4, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Implement BytesIO.peek()
  • Loading branch information
marcelm committed Apr 10, 2026
commit b833b832f8e236eefe5632f338667eff4809c6cf
8 changes: 8 additions & 0 deletions Lib/_pyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,14 @@ def tell(self):
raise ValueError("tell on closed file")
return self._pos

def peek(self, size=-1):
pos = self.tell()
if size == 0:
size = -1
b = self.read(size)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would it be possible to implement it without touching the position? This code is not thread safe. I don't know if it's supposed to be thread-safe. Maybe add a private read method which has an argument to decide to move the position or not.

Same remark for C code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point; I implemented this now so that the position is not changed. I factored out a peek_bytes function in the C version that does not advance the position.

It seems though that neither the C nor the Python version of BytesIO are supposed to be thread safe. (They don’t use locks as e.g. BufferedReader does.) So I would suggest making them so would be a task for a different PR.

self.seek(pos)
return b

def truncate(self, pos=None):
if self.closed:
raise ValueError("truncate on closed file")
Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_io/test_memoryio.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,23 @@ def test_issue141311(self):
buf = bytearray(2)
self.assertEqual(0, memio.readinto(buf))

def test_peek(self):
buf = self.buftype("1234567890")
memio = self.ioclass(buf)

Comment thread
cmaloney marked this conversation as resolved.
self.assertEqual(memio.peek(1), buf[:1])
self.assertEqual(memio.peek(1), buf[:1])
self.assertEqual(memio.peek(), buf)
self.assertEqual(memio.peek(0), buf)
memio.read(1)
self.assertEqual(memio.peek(1), buf[1:2])
self.assertEqual(memio.peek(), buf[1:])
self.assertEqual(memio.peek(42), buf[1:])
memio.read()
self.assertEqual(memio.peek(1), self.EOF)
memio.close()
self.assertRaises(ValueError, memio.peek)

def test_unicode(self):
memio = self.ioclass()

Expand Down
37 changes: 37 additions & 0 deletions Modules/_io/bytesio.c
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,42 @@ _io_BytesIO_read1_impl(bytesio *self, Py_ssize_t size)
return _io_BytesIO_read_impl(self, size);
}


/*[clinic input]
_io.BytesIO.peek
size: Py_ssize_t(accept={int, NoneType}) = -1
/

Return bytes from the stream without advancing the position.

Return an empty bytes object at EOF.
[clinic start generated code]*/

static PyObject *
_io_BytesIO_peek_impl(bytesio *self, Py_ssize_t size)
/*[clinic end generated code: output=fa4d8ce28b35db9b input=afc80e71b37e7c59]*/
{
Py_ssize_t n;
const char *output;

CHECK_CLOSED(self);

/* adjust invalid sizes */
n = self->string_size - self->pos;
if (size < 1 || size > n) {
size = n;
if (size < 0)
Comment thread
marcelm marked this conversation as resolved.
Outdated
size = 0;
Comment thread
marcelm marked this conversation as resolved.
Outdated
}

assert(self->buf != NULL);
assert(size <= self->string_size);
output = PyBytes_AS_STRING(self->buf) + self->pos;
return PyBytes_FromStringAndSize(output, size);
}



Comment thread
marcelm marked this conversation as resolved.
Outdated
/*[clinic input]
@critical_section
_io.BytesIO.readline
Expand Down Expand Up @@ -1135,6 +1171,7 @@ static struct PyMethodDef bytesio_methods[] = {
_IO_BYTESIO_READLINE_METHODDEF
_IO_BYTESIO_READLINES_METHODDEF
_IO_BYTESIO_READ_METHODDEF
_IO_BYTESIO_PEEK_METHODDEF
_IO_BYTESIO_GETBUFFER_METHODDEF
_IO_BYTESIO_GETVALUE_METHODDEF
_IO_BYTESIO_SEEK_METHODDEF
Expand Down
38 changes: 37 additions & 1 deletion Modules/_io/clinic/bytesio.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.