From 024e4e1ebb9a7b3fe1425aa800ef2f07ae1b146b Mon Sep 17 00:00:00 2001 From: Taeknology <20297177+Taeknology@users.noreply.github.com> Date: Sun, 2 Aug 2026 19:35:39 +0900 Subject: [PATCH 1/3] gh-150942: Use reference-stealing append in BytesIO.readlines --- Modules/_io/bytesio.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Modules/_io/bytesio.c b/Modules/_io/bytesio.c index 7d6053d85cd9e4a..fa3778c12273ca5 100644 --- a/Modules/_io/bytesio.c +++ b/Modules/_io/bytesio.c @@ -1,5 +1,6 @@ #include "Python.h" #include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION() +#include "pycore_list.h" // _PyList_AppendTakeRef() #include "pycore_object.h" #include "pycore_pyatomic_ft_wrappers.h" #include "pycore_sysmodule.h" // _PySys_GetSizeOf() @@ -663,11 +664,9 @@ _io_BytesIO_readlines_impl(bytesio *self, PyObject *arg) line = PyBytes_FromStringAndSize(output, n); if (!line) goto on_error; - if (PyList_Append(result, line) == -1) { - Py_DECREF(line); + if (_PyList_AppendTakeRef((PyListObject *)result, line) < 0) { goto on_error; } - Py_DECREF(line); size += n; if (maxsize > 0 && size >= maxsize) break; From 356c57f4b1d3b53ef4cf52bc9b957170911b08f8 Mon Sep 17 00:00:00 2001 From: Taeknology <20297177+Taeknology@users.noreply.github.com> Date: Tue, 4 Aug 2026 19:50:52 +0900 Subject: [PATCH 2/3] gh-150942: Add NEWS entry for BytesIO.readlines optimization --- .../next/Library/2026-08-04-19-50-52.gh-issue-150942.B8ytes.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-08-04-19-50-52.gh-issue-150942.B8ytes.rst diff --git a/Misc/NEWS.d/next/Library/2026-08-04-19-50-52.gh-issue-150942.B8ytes.rst b/Misc/NEWS.d/next/Library/2026-08-04-19-50-52.gh-issue-150942.B8ytes.rst new file mode 100644 index 000000000000000..be50a73a029044c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-04-19-50-52.gh-issue-150942.B8ytes.rst @@ -0,0 +1,2 @@ +Improve the performance of :meth:`io.BytesIO.readlines`, particularly in +free-threaded builds. From 26c3dbbf92d52a93c1307c18f2ffbf3b363abd93 Mon Sep 17 00:00:00 2001 From: Taeknology <20297177+Taeknology@users.noreply.github.com> Date: Tue, 4 Aug 2026 21:46:53 +0900 Subject: [PATCH 3/3] gh-150942: Fix NEWS cross-reference --- .../Library/2026-08-04-19-50-52.gh-issue-150942.B8ytes.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2026-08-04-19-50-52.gh-issue-150942.B8ytes.rst b/Misc/NEWS.d/next/Library/2026-08-04-19-50-52.gh-issue-150942.B8ytes.rst index be50a73a029044c..bdc93872f012616 100644 --- a/Misc/NEWS.d/next/Library/2026-08-04-19-50-52.gh-issue-150942.B8ytes.rst +++ b/Misc/NEWS.d/next/Library/2026-08-04-19-50-52.gh-issue-150942.B8ytes.rst @@ -1,2 +1,2 @@ -Improve the performance of :meth:`io.BytesIO.readlines`, particularly in -free-threaded builds. +Improve the performance of :meth:`~io.IOBase.readlines` for +:class:`io.BytesIO`, particularly in free-threaded builds.