Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions libcloud/test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# limitations under the License.

import sys
import zlib
import random
import socket
import string
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 3 additions & 16 deletions libcloud/utils/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down