@@ -499,6 +499,42 @@ _io_BytesIO_read1_impl(bytesio *self, Py_ssize_t size)
499499 return _io_BytesIO_read_impl (self , size );
500500}
501501
502+
503+ /*[clinic input]
504+ _io.BytesIO.peek
505+ size: Py_ssize_t(accept={int, NoneType}) = -1
506+ /
507+
508+ Return bytes from the stream without advancing the position.
509+
510+ Return an empty bytes object at EOF.
511+ [clinic start generated code]*/
512+
513+ static PyObject *
514+ _io_BytesIO_peek_impl (bytesio * self , Py_ssize_t size )
515+ /*[clinic end generated code: output=fa4d8ce28b35db9b input=afc80e71b37e7c59]*/
516+ {
517+ Py_ssize_t n ;
518+ const char * output ;
519+
520+ CHECK_CLOSED (self );
521+
522+ /* adjust invalid sizes */
523+ n = self -> string_size - self -> pos ;
524+ if (size < 1 || size > n ) {
525+ size = n ;
526+ if (size < 0 )
527+ size = 0 ;
528+ }
529+
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 );
534+ }
535+
536+
537+
502538/*[clinic input]
503539@critical_section
504540_io.BytesIO.readline
@@ -1135,6 +1171,7 @@ static struct PyMethodDef bytesio_methods[] = {
11351171 _IO_BYTESIO_READLINE_METHODDEF
11361172 _IO_BYTESIO_READLINES_METHODDEF
11371173 _IO_BYTESIO_READ_METHODDEF
1174+ _IO_BYTESIO_PEEK_METHODDEF
11381175 _IO_BYTESIO_GETBUFFER_METHODDEF
11391176 _IO_BYTESIO_GETVALUE_METHODDEF
11401177 _IO_BYTESIO_SEEK_METHODDEF
0 commit comments