FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-159739: Respect readline limits after a short read by jelle-openai · Pull Request #159974 · python/cpython · GitHub

Repository navigation

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

Filter by extension

Filter by extension .c  (1) .py  (2) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 1 addition & 1 deletion Lib/_pyio.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ def nreadahead():
return 1
n = (readahead.find(b"\n") + 1) or len(readahead)
if size >= 0:
n = min(n, size)
n = min(n, size - len(res))
return n
else:
def nreadahead():
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_io/test_general.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,22 @@ def test_readline(self):
with self.open(os_helper.TESTFN, "r", encoding="utf-8") as f:
self.assertRaises(TypeError, f.readline, 5.3)

def test_readline_limit_with_short_read(self):
class Reader(self.IOBase):
data = b"abcdef"

def peek(self, size):
return self.data

def read(self, size):
data = self.data[:min(size, 2)]
self.data = self.data[len(data):]
return data

reader = Reader()
self.assertEqual(reader.readline(3), b"abc")
self.assertEqual(reader.data, b"def")

def test_readline_nonsizeable(self):
# Issue #30061
# Crash when readline() returns an object without __len__
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :meth:`io.IOBase.readline` returning more bytes than requested when a
stream supports :meth:`!peek` and returns short reads.
3 changes: 2 additions & 1 deletion Modules/_io/iobase.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,9 @@ _io__IOBase_readline_impl(PyObject *self, Py_ssize_t limit)
Py_ssize_t n = 0;
const char *buf = PyBytes_AS_STRING(readahead);
if (limit >= 0) {
Py_ssize_t remaining = limit - PyBytesWriter_GetSize(writer);
do {
if (n >= PyBytes_GET_SIZE(readahead) || n >= limit)
if (n >= PyBytes_GET_SIZE(readahead) || n >= remaining)
break;
if (buf[n++] == '\n')
break;
Expand Down
Loading

Back | FazBrowse Home | New Git URL