diff --git a/CHANGES.rst b/CHANGES.rst index 048d163435..52d3a5df57 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -131,6 +131,11 @@ Compute Storage ~~~~~~~ +- Preserve data after empty chunks in stream iterators, including compressed + uploads, in ``read_in_chunks`` and ``exhaust_iterator``. + (GITHUB-2252) + [Khushal Dangar - @khushalx] + - [Local Storage] Delete the partially written file when ``download_object_range`` fails and ``delete_on_failure`` is set. (GITHUB-2223) diff --git a/libcloud/test/test_utils.py b/libcloud/test/test_utils.py index 47b5ed59d8..26f42f630a 100644 --- a/libcloud/test/test_utils.py +++ b/libcloud/test/test_utils.py @@ -14,6 +14,7 @@ # limitations under the License. import sys +import zlib import random import socket import string @@ -224,6 +225,27 @@ def iterator(): self.assertEqual(result, b("a") * 10) self.assertEqual(chunk_count, 200) + def test_read_in_chunks_empty_chunks(self): + for fill_size in (False, True): + chunks = iter([b"", b"hello", b"", b"world", b""]) + output = libcloud.utils.files.read_in_chunks(chunks, chunk_size=4, fill_size=fill_size) + self.assertEqual(b"".join(output), b"helloworld") + + def test_compressed_iterator_with_empty_chunks(self): + payload = b"first row\nsecond row\nthird row\n" + compressor = zlib.compressobj() + chunks = [compressor.compress(row) for row in payload.splitlines(keepends=True)] + chunks.append(compressor.flush()) + self.assertIn(b"", chunks) + + for fill_size in (False, True): + output = b"".join( + libcloud.utils.files.read_in_chunks(iter(chunks), chunk_size=4, fill_size=fill_size) + ) + self.assertEqual(zlib.decompress(output), payload) + output = libcloud.utils.files.exhaust_iterator(iter(chunks)) + self.assertEqual(zlib.decompress(output), payload) + def test_read_in_chunks_large_iterator_batches(self): def iterator(): for x in range(0, 10): @@ -358,6 +380,10 @@ def iterator_func(): result = libcloud.utils.files.exhaust_iterator(iterator=iterator) self.assertEqual(result, b(data)) + def test_exhaust_iterator_empty_chunks(self): + for chunks in ([b"", b"hello", b"", b"world", b""], ["", "hello", "", "world", ""]): + self.assertEqual(libcloud.utils.files.exhaust_iterator(iter(chunks)), b"helloworld") + def test_exhaust_iterator_empty_iterator(self): data = "" iterator = StringIO(data) diff --git a/libcloud/utils/files.py b/libcloud/utils/files.py index ae69ea843b..2ed61464ca 100644 --- a/libcloud/utils/files.py +++ b/libcloud/utils/files.py @@ -64,6 +64,8 @@ def read_in_chunks(iterator, chunk_size=None, fill_size=False, yield_empty=False chunk = b(get_data(*args)) if len(chunk) > 0: data += chunk + elif get_data is next: + continue else: empty = True except StopIteration: @@ -109,22 +111,7 @@ def exhaust_iterator(iterator): :rtype ``str`` :return Data returned by the iterator. """ - data = b("") - - try: - chunk = b(next(iterator)) - except StopIteration: - chunk = b("") - - while len(chunk) > 0: - data += chunk - - try: - chunk = b(next(iterator)) - except StopIteration: - chunk = b("") - - return data + return b("").join(b(chunk) for chunk in iterator) def guess_file_mime_type(file_path):