Skip to content

Commit fbf672e

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 244fff3 commit fbf672e

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

Modules/_io/bytesio.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,11 +486,11 @@ _io_BytesIO_peek_impl(bytesio *self, Py_ssize_t size)
486486
if (size < 0)
487487
size = 0;
488488
}
489+
Py_ssize_t prev_pos = self->pos;
490+
PyObject* result = read_bytes(self, size);
491+
self->pos = prev_pos;
489492

490-
assert(self->buf != NULL);
491-
assert(size <= self->string_size);
492-
output = PyBytes_AS_STRING(self->buf) + self->pos;
493-
return PyBytes_FromStringAndSize(output, size);
493+
return result;
494494
}
495495

496496

0 commit comments

Comments
 (0)