py3: Fix return type on CooperativeReader.read
This should always be bytes; previously there was a path that returned an empty native string. Change-Id: I1cb4997cd3e766f9a7aea41ebe41a73971496bc3 Closes-Bug: #1818919
This commit is contained in:
parent
2cf78f339d
commit
d5c0daa8b7
|
@ -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
|
||||
|
|
|
@ -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 ''])
|
||||
|
|
Loading…
Reference in New Issue