@@ -459,6 +459,42 @@ _io_BytesIO_read1_impl(bytesio *self, Py_ssize_t size)
459459 return _io_BytesIO_read_impl (self , size );
460460}
461461
462+
463+ /*[clinic input]
464+ _io.BytesIO.peek
465+ size: Py_ssize_t(accept={int, NoneType}) = -1
466+ /
467+
468+ Return bytes from the stream without advancing the position.
469+
470+ Return an empty bytes object at EOF.
471+ [clinic start generated code]*/
472+
473+ static PyObject *
474+ _io_BytesIO_peek_impl (bytesio * self , Py_ssize_t size )
475+ /*[clinic end generated code: output=fa4d8ce28b35db9b input=afc80e71b37e7c59]*/
476+ {
477+ Py_ssize_t n ;
478+ const char * output ;
479+
480+ CHECK_CLOSED (self );
481+
482+ /* adjust invalid sizes */
483+ n = self -> string_size - self -> pos ;
484+ if (size < 1 || size > n ) {
485+ size = n ;
486+ if (size < 0 )
487+ size = 0 ;
488+ }
489+
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 );
494+ }
495+
496+
497+
462498/*[clinic input]
463499_io.BytesIO.readline
464500 size: Py_ssize_t(accept={int, NoneType}) = -1
@@ -1014,6 +1050,7 @@ static struct PyMethodDef bytesio_methods[] = {
10141050 _IO_BYTESIO_READLINE_METHODDEF
10151051 _IO_BYTESIO_READLINES_METHODDEF
10161052 _IO_BYTESIO_READ_METHODDEF
1053+ _IO_BYTESIO_PEEK_METHODDEF
10171054 _IO_BYTESIO_GETBUFFER_METHODDEF
10181055 _IO_BYTESIO_GETVALUE_METHODDEF
10191056 _IO_BYTESIO_SEEK_METHODDEF
0 commit comments