Skip to content

Commit

Permalink
[#819] ensure no side effects on SinglepartWriter exception (#820)
Browse files Browse the repository at this point in the history
* [#819] ensure no side effects on SinglepartWriter exception

* dummy commit to trigger CI

---------

Co-authored-by: Michael Penkov <[email protected]>
  • Loading branch information
donsokolone and mpenkov authored Aug 15, 2024
1 parent 472ef6e commit a55794d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -482,3 +482,4 @@ The old `smart_open.smart_open` function is deprecated, but continues to work as

- support for multistream bzip files (PR #9, @pombredanne)
- introduce this CHANGELOG

4 changes: 2 additions & 2 deletions smart_open/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -1209,8 +1209,8 @@ def write(self, b):
return length

def terminate(self):
"""Nothing to cancel in single-part uploads."""
return
self._buf = None
logger.debug('%s: terminated singlepart upload', self)

#
# Internal methods.
Expand Down
20 changes: 20 additions & 0 deletions smart_open/tests/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,26 @@ def test_str(self):
with smart_open.s3.open(BUCKET_NAME, 'key', 'wb', multipart_upload=False) as fout:
assert str(fout) == "smart_open.s3.SinglepartWriter('test-smartopen', 'key')"

def test_ensure_no_side_effects_on_exception(self):
class WriteError(Exception):
pass

s3_resource = _resource("s3")
obj = s3_resource.Object(BUCKET_NAME, KEY_NAME)

# wrap in closure to ease writer dereferencing
def _run():
with smart_open.s3.open(BUCKET_NAME, obj.key, "wb", multipart_upload=False) as fout:
fout.write(b"this should not be written")
raise WriteError

try:
_run()
except WriteError:
pass
finally:
self.assertRaises(s3_resource.meta.client.exceptions.NoSuchKey, obj.get)


ARBITRARY_CLIENT_ERROR = botocore.client.ClientError(error_response={}, operation_name='bar')

Expand Down

0 comments on commit a55794d

Please sign in to comment.