Skip to content

Commit

Permalink
Fix off by one, raise ValueError on closed file and set readable = Tr…
Browse files Browse the repository at this point in the history
…ue (#69)

* raise Value Error on closed file
* fix off by one error
* update changelog
  • Loading branch information
hoffmann authored and Cornelius Riemenschneider committed Nov 28, 2017
1 parent 4cd3dee commit 8a95539
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Changelog
*********
0.11.5
======
* Fix one off in open() method interfaces for azure backend

0.11.4
======
Expand Down
11 changes: 10 additions & 1 deletion simplekv/net/azurestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,20 @@ def __init__(self, block_blob_service, container_name, key):

def tell(self):
"""Returns he current offset as int. Always >= 0."""
if self.closed:
raise ValueError("I/O operation on closed file")
return self.pos

def read(self, size=-1):
"""Returns 'size' amount of bytes or less if there is no more data.
If no size is given all data is returned. size can be >= 0."""
if self.closed:
raise ValueError("I/O operation on closed file")
with map_azure_exceptions(key=self.key):
if size < 0:
size = self.size - self.pos

end = min(self.pos + size - 1, self.size)
end = min(self.pos + size - 1, self.size - 1)
if self.pos > end:
return b''
b = self.block_blob_service.get_blob_to_bytes(
Expand All @@ -173,6 +177,8 @@ def seek(self, offset, whence=0):
Any seek operation which moves the position after the stream
should succeed. tell() should report that position and read()
should return an empty bytes object."""
if self.closed:
raise ValueError("I/O operation on closed file")
if whence == 0:
if offset < 0:
raise IOError('seek would move position outside the file')
Expand All @@ -188,3 +194,6 @@ def seek(self, offset, whence=0):

def seekable(self):
return True

def readable(self):
return True
9 changes: 9 additions & 0 deletions tests/test_azure_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def test_open_seek_and_tell(self, store, key, long_value):
store.put(key, long_value)
ok = store.open(key)
assert ok.seekable()
assert ok.readable()
ok.seek(10)
assert ok.tell() == 10
ok.seek(-6, 1)
Expand All @@ -94,6 +95,14 @@ def test_open_seek_and_tell(self, store, key, long_value):
assert ok.tell() == length_lv + 10
assert b'' == ok.read()

ok.close()
with pytest.raises(ValueError):
ok.tell()
with pytest.raises(ValueError):
ok.read(1)
with pytest.raises(ValueError):
ok.seek(10)


class TestExtendedKeysAzureStorage(TestAzureStorage, ExtendedKeyspaceTests):
@pytest.fixture
Expand Down

0 comments on commit 8a95539

Please sign in to comment.