Skip to content

Commit

Permalink
Merge pull request #454 from reef-technologies/empty_stream_error
Browse files Browse the repository at this point in the history
In case no data is provided from the stream, we still provide an empty WriteIntent
  • Loading branch information
mjurbanski-reef authored Dec 6, 2023
2 parents 22b8d3d + 53168d5 commit 80e99c0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions b2sdk/transfer/emerge/unbound_write_intent.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ def iterator(self) -> Iterator[WriteIntent]:
offset += len(self.buffer)
self._rotate_leftovers()

# If we didn't stream anything, we should still provide
# at least an empty WriteIntent, so that the file will be created.
if offset == 0:
source = UnboundSourceBytes(bytearray(), release_function=lambda: None)
yield WriteIntent(source, destination_offset=offset)

def _trim_to_leftovers(self) -> None:
if len(self.buffer) <= self.buffer_size_bytes:
return
Expand Down
1 change: 1 addition & 0 deletions changelog.d/+can-stream-empty-stream.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Streaming from empty stream no longer ends with "Empty emerge parts iterator" error.
7 changes: 7 additions & 0 deletions test/unit/bucket/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -2850,6 +2850,13 @@ def test_upload_unbound_stream(self):
)
test_case.assert_(file_version)

def test_upload_empty_unbound_stream(self):
for test_case in self.test_cases:
file_version = self.bucket.upload_unbound_stream(
io.BytesIO(b''), 'file1', **test_case.kwargs
)
test_case.assert_(file_version)

def test_upload_local_file(self):
for test_case in self.test_cases:
with tempfile.TemporaryDirectory() as d:
Expand Down

0 comments on commit 80e99c0

Please sign in to comment.