Merge "py3: Fix return type on CooperativeReader.read"

This commit is contained in:
Zuul 2019-03-12 14:42:11 +00:00 committed by Gerrit Code Review
commit 13c37de53d
2 changed files with 31 additions and 2 deletions

View File

@ -218,7 +218,7 @@ class CooperativeReader(object):
result = self.buffer[self.position:]
self.buffer = b''
self.position = 0
return str(result)
return bytes(result)
else:
# otherwise read the next chunk from the underlying iterator
# and return it as a whole. Reset the buffer, as subsequent
@ -228,7 +228,7 @@ class CooperativeReader(object):
self.iterator = self.__iter__()
return next(self.iterator)
except StopIteration:
return ''
return b''
finally:
self.buffer = b''
self.position = 0

View File

@ -67,6 +67,35 @@ class TestUtils(test_utils.BaseTestCase):
meat = b''.join(chunks)
self.assertEqual(b'aaabbbcccdddeeefffggghhh', meat)
def test_cooperative_reader_unbounded_read_on_iterator(self):
"""Ensure cooperative reader is happy with empty iterators"""
data = b'abcdefgh'
data_list = [data[i:i + 1] * 3 for i in range(len(data))]
reader = utils.CooperativeReader(data_list)
self.assertEqual(
[chunk for chunk in iter(lambda: reader.read(), b'')],
[b'aaa', b'bbb', b'ccc', b'ddd', b'eee', b'fff', b'ggg', b'hhh'])
def test_cooperative_reader_on_iterator_with_buffer(self):
"""Ensure cooperative reader is happy with empty iterators"""
data_list = [b'abcd', b'efgh']
reader = utils.CooperativeReader(data_list)
# read from part of a chunk, get the first item into the buffer
self.assertEqual(b'ab', reader.read(2))
# read purely from buffer
self.assertEqual(b'c', reader.read(1))
# unbounded read grabs the rest of the buffer
self.assertEqual(b'd', reader.read())
# then the whole next chunk
self.assertEqual(b'efgh', reader.read())
# past that, it's always empty
self.assertEqual(b'', reader.read())
def test_cooperative_reader_unbounded_read_on_empty_iterator(self):
"""Ensure cooperative reader is happy with empty iterators"""
reader = utils.CooperativeReader([])
self.assertEqual(b'', reader.read())
def test_cooperative_reader_of_iterator_stop_iteration_err(self):
"""Ensure cooperative reader supports iterator backends too"""
reader = utils.CooperativeReader([l * 3 for l in ''])