Skip to content

Commit

Permalink
Handle edge case (read 0 bytes) in read function. Fix #171 (#193)
Browse files Browse the repository at this point in the history
* Resolve issue #171: handle edge case in read function

* get rid of unneeded parameter in test
  • Loading branch information
mpenkov authored and menshikh-iv committed May 6, 2018
1 parent ea8e10d commit 9c339fc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 3 additions & 1 deletion smart_open/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ def detach(self):

def read(self, size=-1):
"""Read up to size bytes from the object and return them."""
if size <= 0:
if size == 0:
return b''
elif size < 0:
if len(self._buffer):
from_buf = self._read_from_buffer(len(self._buffer))
else:
Expand Down
9 changes: 9 additions & 0 deletions smart_open/tests/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,15 @@ def test_readline_tiny_buffer(self):
expected = [b'englishman\n', b'in\n', b'new\n', b'york\n']
self.assertEqual(expected, actual)

def test_read0_does_not_return_data(self):
content = b'englishman\nin\nnew\nyork\n'
create_bucket_and_key(contents=content)

with smart_open.s3.BufferedInputBase(BUCKET_NAME, KEY_NAME) as fin:
data = fin.read(0)

self.assertEqual(data, b'')


@maybe_mock_s3
class BufferedOutputBaseTest(unittest.TestCase):
Expand Down

0 comments on commit 9c339fc

Please sign in to comment.