If size is specified, try to read at least size characters.

This is a alternative version of patch #1379332.
This commit is contained in:
Walter Dörwald 2006-03-06 22:39:12 +00:00
parent f8d767198f
commit ca199432c2
2 changed files with 13 additions and 6 deletions

View file

@ -274,7 +274,10 @@ class StreamReader(Codec):
while True: while True:
# can the request can be satisfied from the character buffer? # can the request can be satisfied from the character buffer?
if chars < 0: if chars < 0:
if self.charbuffer: if size < 0:
if self.charbuffer:
break
elif len(self.charbuffer) >= size:
break break
else: else:
if len(self.charbuffer) >= chars: if len(self.charbuffer) >= chars:

View file

@ -46,19 +46,23 @@ class ReadTest(unittest.TestCase):
stream = StringIO.StringIO(input.encode(self.encoding)) stream = StringIO.StringIO(input.encode(self.encoding))
return codecs.getreader(self.encoding)(stream) return codecs.getreader(self.encoding)(stream)
def readalllines(input, keepends=True): def readalllines(input, keepends=True, size=None):
reader = getreader(input) reader = getreader(input)
lines = [] lines = []
while True: while True:
line = reader.readline(keepends=keepends) line = reader.readline(size=size, keepends=keepends)
if not line: if not line:
break break
lines.append(line) lines.append(line)
return "".join(lines) return "|".join(lines)
s = u"foo\nbar\r\nbaz\rspam\u2028eggs" s = u"foo\nbar\r\nbaz\rspam\u2028eggs"
self.assertEqual(readalllines(s, True), s) sexpected = u"foo\n|bar\r\n|baz\r|spam\u2028|eggs"
self.assertEqual(readalllines(s, False), u"foobarbazspameggs") 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()) # Test long lines (multiple calls to read() in readline())
vw = [] vw = []