diff options
author | Walter Dörwald <walter@livinglogic.de> | 2006-03-06 22:44:03 (GMT) |
---|---|---|
committer | Walter Dörwald <walter@livinglogic.de> | 2006-03-06 22:44:03 (GMT) |
commit | a4b4dc328f0769f90c3ebb743280b48d4eae7b7c (patch) | |
tree | 2e3e4ea94091450259c6a80fdf319d03bc3def20 | |
parent | 39be80600b9e6cb74978fa86c7c2b4956e7ff12b (diff) | |
download | cpython-a4b4dc328f0769f90c3ebb743280b48d4eae7b7c.zip cpython-a4b4dc328f0769f90c3ebb743280b48d4eae7b7c.tar.gz cpython-a4b4dc328f0769f90c3ebb743280b48d4eae7b7c.tar.bz2 |
Backport revision 42872:
If size is specified, try to read at least size characters.
This is a alternative version of patch #1379332.
-rw-r--r-- | Lib/codecs.py | 5 | ||||
-rw-r--r-- | Lib/test/test_codecs.py | 14 |
2 files changed, 13 insertions, 6 deletions
diff --git a/Lib/codecs.py b/Lib/codecs.py index 103a6f6..9a217b3 100644 --- a/Lib/codecs.py +++ b/Lib/codecs.py @@ -274,7 +274,10 @@ class StreamReader(Codec): while True: # can the request can be satisfied from the character buffer? if chars < 0: - if self.charbuffer: + if size < 0: + if self.charbuffer: + break + elif len(self.charbuffer) >= size: break else: if len(self.charbuffer) >= chars: diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 44d7896..a1a8073 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -57,19 +57,23 @@ class ReadTest(unittest.TestCase): stream = StringIO.StringIO(input.encode(self.encoding)) return codecs.getreader(self.encoding)(stream) - def readalllines(input, keepends=True): + def readalllines(input, keepends=True, size=None): reader = getreader(input) lines = [] while True: - line = reader.readline(keepends=keepends) + line = reader.readline(size=size, keepends=keepends) if not line: break lines.append(line) - return "".join(lines) + return "|".join(lines) s = u"foo\nbar\r\nbaz\rspam\u2028eggs" - self.assertEqual(readalllines(s, True), s) - self.assertEqual(readalllines(s, False), u"foobarbazspameggs") + sexpected = u"foo\n|bar\r\n|baz\r|spam\u2028|eggs" + sexpectednoends = u"foo|bar|baz|spam|eggs" + self.assertEqual(readalllines(s, True), sexpected) + self.assertEqual(readalllines(s, False), sexpectednoends) + self.assertEqual(readalllines(s, True, 10), sexpected) + self.assertEqual(readalllines(s, False, 10), sexpectednoends) # Test long lines (multiple calls to read() in readline()) vw = [] |