diff --git a/Lib/_pyio.py b/Lib/_pyio.py index 77239f026efbb1..21f40add53593b 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -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) diff --git a/Lib/test/test_io/test_bufferedio.py b/Lib/test/test_io/test_bufferedio.py index e83dd0d4e28d00..ce594762c325ac 100644 --- a/Lib/test/test_io/test_bufferedio.py +++ b/Lib/test/test_io/test_bufferedio.py @@ -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"]) diff --git a/Misc/NEWS.d/next/Library/2026-11-18-18-28-37.gh-issue-162230.ezR86S.rst b/Misc/NEWS.d/next/Library/2026-11-18-18-28-37.gh-issue-162230.ezR86S.rst new file mode 100644 index 00000000000000..00b96c972ece6f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-18-18-28-37.gh-issue-162230.ezR86S.rst @@ -0,0 +1,3 @@ +Speed up :meth:`!io.BufferedRWPair.readline`. If a non-blocking read would +block, return the bytes read so far (possibly ``b''``). Previously, it raised +:exc:`OSError`. diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c index 44c3f56caee18b..757e6a10558e48 100644 --- a/Modules/_io/bufferedio.c +++ b/Modules/_io/bufferedio.c @@ -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 + / + +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 @@ -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 diff --git a/Modules/_io/clinic/bufferedio.c.h b/Modules/_io/clinic/bufferedio.c.h index a85dd6a20bc2c1..0cedf4a9948e9d 100644 --- a/Modules/_io/clinic/bufferedio.c.h +++ b/Modules/_io/clinic/bufferedio.c.h @@ -1197,6 +1197,42 @@ _io_BufferedRWPair_read1(PyObject *self, PyObject *const *args, Py_ssize_t nargs return return_value; } +PyDoc_STRVAR(_io_BufferedRWPair_readline__doc__, +"readline($self, size=-1, /)\n" +"--\n" +"\n" +"Read and return a line from the stream.\n" +"\n" +"If size is specified, at most size bytes will be read.\n" +"\n" +"The line terminator is always b\'\\n\'."); + +#define _IO_BUFFEREDRWPAIR_READLINE_METHODDEF \ + {"readline", _PyCFunction_CAST(_io_BufferedRWPair_readline), METH_FASTCALL, _io_BufferedRWPair_readline__doc__}, + +static PyObject * +_io_BufferedRWPair_readline_impl(rwpair *self, PyObject *size); + +static PyObject * +_io_BufferedRWPair_readline(PyObject *self, PyObject *const *args, Py_ssize_t nargs) +{ + PyObject *return_value = NULL; + PyObject *size = NULL; + + if (!_PyArg_CheckPositional("readline", nargs, 0, 1)) { + goto exit; + } + if (nargs < 1) { + goto skip_optional; + } + size = args[0]; +skip_optional: + return_value = _io_BufferedRWPair_readline_impl((rwpair *)self, size); + +exit: + return return_value; +} + PyDoc_STRVAR(_io_BufferedRWPair_readinto__doc__, "readinto($self, buffer, /)\n" "--\n" @@ -1441,4 +1477,4 @@ _io_BufferedRandom___init__(PyObject *self, PyObject *args, PyObject *kwargs) #define _IO_BUFFEREDRWPAIR_CLOSED_GETSETDEF {"closed", (getter)_io_BufferedRWPair_closed_get, (setter)NULL, NULL}, -/*[clinic end generated code: output=d9e1eb0efa4a2431 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=1596bb353f00a1ea input=a9049054013a1b77]*/