@@ -462,6 +462,42 @@ _io_BytesIO_read1_impl(bytesio *self, Py_ssize_t size)
462462 return _io_BytesIO_read_impl (self , size );
463463}
464464
465+
466+ /*[clinic input]
467+ _io.BytesIO.peek
468+ size: Py_ssize_t(accept={int, NoneType}) = -1
469+ /
470+
471+ Return bytes from the stream without advancing the position.
472+
473+ Return an empty bytes object at EOF.
474+ [clinic start generated code]*/
475+
476+ static PyObject *
477+ _io_BytesIO_peek_impl (bytesio * self , Py_ssize_t size )
478+ /*[clinic end generated code: output=fa4d8ce28b35db9b input=afc80e71b37e7c59]*/
479+ {
480+ Py_ssize_t n ;
481+ const char * output ;
482+
483+ CHECK_CLOSED (self );
484+
485+ /* adjust invalid sizes */
486+ n = self -> string_size - self -> pos ;
487+ if (size < 1 || size > n ) {
488+ size = n ;
489+ if (size < 0 )
490+ size = 0 ;
491+ }
492+
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 );
497+ }
498+
499+
500+
465501/*[clinic input]
466502_io.BytesIO.readline
467503 size: Py_ssize_t(accept={int, NoneType}) = -1
@@ -1019,6 +1055,7 @@ static struct PyMethodDef bytesio_methods[] = {
10191055 _IO_BYTESIO_READLINE_METHODDEF
10201056 _IO_BYTESIO_READLINES_METHODDEF
10211057 _IO_BYTESIO_READ_METHODDEF
1058+ _IO_BYTESIO_PEEK_METHODDEF
10221059 _IO_BYTESIO_GETBUFFER_METHODDEF
10231060 _IO_BYTESIO_GETVALUE_METHODDEF
10241061 _IO_BYTESIO_SEEK_METHODDEF
0 commit comments