Skip to content

Commit 00457ae

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 eaa7672 commit 00457ae

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
@@ -526,11 +526,11 @@ _io_BytesIO_peek_impl(bytesio *self, Py_ssize_t size)
526526
if (size < 0)
527527
size = 0;
528528
}
529+
Py_ssize_t prev_pos = self->pos;
530+
PyObject* result = read_bytes_lock_held(self, size);
531+
self->pos = prev_pos;
529532

530-
assert(self->buf != NULL);
531-
assert(size <= self->string_size);
532-
output = PyBytes_AS_STRING(self->buf) + self->pos;
533-
return PyBytes_FromStringAndSize(output, size);
533+
return result;
534534
}
535535

536536

0 commit comments

Comments
 (0)