Skip to content

Commit 441c010

Browse files
authored
gh-162230: Relay BufferedRWPair.readline() to the reader (#162231)
Call the internal reader's `readline()` in `_io` and `_pyio`. There is behaviour change. `BufferedRWPair.readline()` would behave like `BufferedReader.readline()`: in C, a non-blocking read with no data returns the partial line or `b''` instead of raising `OSError`, a closed pair always raises `ValueError`, and subclass overrides of `peek()` and `read()` would no longer apply.
1 parent 6c9f2b6 commit 441c010

5 files changed

Lines changed: 83 additions & 1 deletion

File tree

‎Lib/_pyio.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,15 @@ def peek(self, size=0):
14041404
def read1(self, size=-1):
14051405
return self.reader.read1(size)
14061406

1407+
def readline(self, size=-1):
1408+
r"""Read and return a line from the stream.
1409+
1410+
If size is specified, at most size bytes will be read.
1411+
1412+
The line terminator is always b'\n'.
1413+
"""
1414+
return self.reader.readline(size)
1415+
14071416
def readinto1(self, b):
14081417
return self.reader.readinto1(b)
14091418

‎Lib/test/test_io/test_bufferedio.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,20 @@ def test_read(self):
10571057
pair = self.tp(self.BytesIO(b"abc"), self.MockRawIO())
10581058
self.assertEqual(pair.read(None), b"abc")
10591059

1060+
def test_readline(self):
1061+
pair = self.tp(self.BytesIO(b"ab\ncd"), self.MockRawIO())
1062+
1063+
self.assertEqual(pair.readline(1), b"a")
1064+
self.assertEqual(pair.readline(5), b"b\n")
1065+
self.assertEqual(pair.readline(0), b"")
1066+
self.assertEqual(pair.readline(None), b"cd")
1067+
1068+
def test_readline_nonblocking(self):
1069+
raw = self.MockRawIO([b"abc", None, None])
1070+
pair = self.tp(raw, self.MockRawIO())
1071+
1072+
self.assertEqual(pair.readline(), b"abc")
1073+
10601074
def test_readlines(self):
10611075
pair = lambda: self.tp(self.BytesIO(b"abc\ndef\nh"), self.MockRawIO())
10621076
self.assertEqual(pair().readlines(), [b"abc\n", b"def\n", b"h"])
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Speed up :meth:`!io.BufferedRWPair.readline`. If a non-blocking read would
2+
block, return the bytes read so far (possibly ``b''``). Previously, it raised
3+
:exc:`OSError`.

‎Modules/_io/bufferedio.c‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2394,6 +2394,25 @@ _io_BufferedRWPair_read1_impl(rwpair *self, PyObject *size)
23942394
return _forward_call(self->reader, &_Py_ID(read1), size);
23952395
}
23962396

2397+
/*[clinic input]
2398+
_io.BufferedRWPair.readline
2399+
size: object(c_default="NULL") = -1
2400+
/
2401+
2402+
Read and return a line from the stream.
2403+
2404+
If size is specified, at most size bytes will be read.
2405+
2406+
The line terminator is always b'\n'.
2407+
[clinic start generated code]*/
2408+
2409+
static PyObject *
2410+
_io_BufferedRWPair_readline_impl(rwpair *self, PyObject *size)
2411+
/*[clinic end generated code: output=ee5bb2364231e6e1 input=1c552272a69db1ad]*/
2412+
{
2413+
return _forward_call(self->reader, &_Py_ID(readline), size);
2414+
}
2415+
23972416
/*[clinic input]
23982417
_io.BufferedRWPair.readinto
23992418
buffer: object
@@ -2735,6 +2754,7 @@ static PyMethodDef bufferedrwpair_methods[] = {
27352754
_IO_BUFFEREDRWPAIR_READ_METHODDEF
27362755
_IO_BUFFEREDRWPAIR_PEEK_METHODDEF
27372756
_IO_BUFFEREDRWPAIR_READ1_METHODDEF
2757+
_IO_BUFFEREDRWPAIR_READLINE_METHODDEF
27382758
_IO_BUFFEREDRWPAIR_READINTO_METHODDEF
27392759
_IO_BUFFEREDRWPAIR_READINTO1_METHODDEF
27402760

‎Modules/_io/clinic/bufferedio.c.h‎

Lines changed: 37 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)