Skip to content

Commit 2506967

Browse files
committed
Implement with the help of read_bytes()
This allows peek() to use the same optimization that read_bytes() has of returning a reference to the buffer when possible (without copying).
1 parent b3f3c3d commit 2506967

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

Modules/_io/bytesio.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -489,11 +489,11 @@ _io_BytesIO_peek_impl(bytesio *self, Py_ssize_t size)
489489
if (size < 0)
490490
size = 0;
491491
}
492+
Py_ssize_t prev_pos = self->pos;
493+
PyObject* result = read_bytes(self, size);
494+
self->pos = prev_pos;
492495

493-
assert(self->buf != NULL);
494-
assert(size <= self->string_size);
495-
output = PyBytes_AS_STRING(self->buf) + self->pos;
496-
return PyBytes_FromStringAndSize(output, size);
496+
return result;
497497
}
498498

499499

0 commit comments

Comments
 (0)