Skip to content

gh-161272: Fix off-by-one in _pyio.IOBase.readlines(hint) - #161274

Closed
tsayin4 wants to merge 1 commit into
python:mainfrom
tsayin4:fix-pyio-readlines-hint
Closed

tsayin4 wants to merge 1 commit into
python:mainfrom
tsayin4:fix-pyio-readlines-hint

Conversation

@tsayin4

@tsayin4 tsayin4 commented Nov 3, 2026 •

Copy link
Copy Markdown

IOBase.readlines(hint) is documented to stop reading only when the total size of
the lines read so far exceeds hint, and Modules/_io/iobase.c implements exactly
that (line_length > hint - length). _pyio breaks on n >= hint, so it returns
one line fewer whenever the accumulated size is exactly hint:

>>> import io, _pyio
>>> open("x", "wb").write(b"a\nbb\nccc\n")
9
>>> io.open("x", "rb").readlines(2), _pyio.open("x", "rb").readlines(2)
([b'a\n', b'bb\n'], [b'a\n'])

The _pyio docstring itself says "exceeds", so only the comparison is wrong.

Tested: the new test was run against _io, unpatched _pyio and patched _pyio on
3.13 (the touched code is byte-identical on main); it fails on unpatched _pyio
(hint=2, 5 and 9) and passes for both implementations with the fix. I have not
run the full test_io suite locally (no local build yet), so CI is the check for
that.

@python-cla-bot

python-cla-bot Bot commented Nov 3, 2026

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants