Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Lib/_pyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,15 @@ def peek(self, size=0):
def read1(self, size=-1):
return self.reader.read1(size)

def readline(self, size=-1):
r"""Read and return a line from the stream.

If size is specified, at most size bytes will be read.

The line terminator is always b'\n'.
"""
return self.reader.readline(size)

def readinto1(self, b):
return self.reader.readinto1(b)

Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_io/test_bufferedio.py
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,20 @@ def test_read(self):
pair = self.tp(self.BytesIO(b"abc"), self.MockRawIO())
self.assertEqual(pair.read(None), b"abc")

def test_readline(self):
pair = self.tp(self.BytesIO(b"ab\ncd"), self.MockRawIO())

self.assertEqual(pair.readline(1), b"a")
self.assertEqual(pair.readline(5), b"b\n")
self.assertEqual(pair.readline(0), b"")
self.assertEqual(pair.readline(None), b"cd")

def test_readline_nonblocking(self):
raw = self.MockRawIO([b"abc", None, None])
pair = self.tp(raw, self.MockRawIO())

self.assertEqual(pair.readline(), b"abc")

def test_readlines(self):
pair = lambda: self.tp(self.BytesIO(b"abc\ndef\nh"), self.MockRawIO())
self.assertEqual(pair().readlines(), [b"abc\n", b"def\n", b"h"])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Speed up :meth:`!io.BufferedRWPair.readline`. If a non-blocking read would
Comment thread
maurycy marked this conversation as resolved.
Resolved
block, return the bytes read so far (possibly ``b''``). Previously, it raised
:exc:`OSError`.
20 changes: 20 additions & 0 deletions Modules/_io/bufferedio.c
Original file line number Diff line number Diff line change
Expand Up @@ -2394,6 +2394,25 @@ _io_BufferedRWPair_read1_impl(rwpair *self, PyObject *size)
return _forward_call(self->reader, &_Py_ID(read1), size);
}

/*[clinic input]
_io.BufferedRWPair.readline
size: object(c_default="NULL") = -1
/
Comment thread
maurycy marked this conversation as resolved.
Resolved

Read and return a line from the stream.

If size is specified, at most size bytes will be read.

The line terminator is always b'\n'.
[clinic start generated code]*/

static PyObject *
_io_BufferedRWPair_readline_impl(rwpair *self, PyObject *size)
/*[clinic end generated code: output=ee5bb2364231e6e1 input=1c552272a69db1ad]*/
{
return _forward_call(self->reader, &_Py_ID(readline), size);
}

/*[clinic input]
_io.BufferedRWPair.readinto
buffer: object
Expand Down Expand Up @@ -2735,6 +2754,7 @@ static PyMethodDef bufferedrwpair_methods[] = {
_IO_BUFFEREDRWPAIR_READ_METHODDEF
_IO_BUFFEREDRWPAIR_PEEK_METHODDEF
_IO_BUFFEREDRWPAIR_READ1_METHODDEF
_IO_BUFFEREDRWPAIR_READLINE_METHODDEF
_IO_BUFFEREDRWPAIR_READINTO_METHODDEF
_IO_BUFFEREDRWPAIR_READINTO1_METHODDEF

Expand Down
38 changes: 37 additions & 1 deletion Modules/_io/clinic/bufferedio.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading