Skip to content

Commit 7fa5221

Browse files
committed
cover ZipStore with mode "r"
1 parent c19b6c0 commit 7fa5221

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

zarr/storage.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from zarr.meta import encode_array_metadata, encode_group_metadata
1717
from zarr.compat import PY2, binary_type
1818
from zarr.codecs import codec_registry
19+
from zarr.errors import PermissionError
1920

2021

2122
array_meta_key = '.zarray'
@@ -734,13 +735,10 @@ def __getitem__(self, key):
734735
return f.read()
735736

736737
def __setitem__(self, key, value):
738+
if self.mode == 'r':
739+
raise PermissionError('mapping is read-only')
737740
value = ensure_bytes(value)
738-
if self.mode in {'w', 'a', 'x'}:
739-
mode = 'a'
740-
else:
741-
# let zipfile raise an error when trying to write
742-
mode = 'r'
743-
with zipfile.ZipFile(self.path, mode=mode,
741+
with zipfile.ZipFile(self.path, mode='a',
744742
compression=self.compression,
745743
allowZip64=self.allowZip64) as zf:
746744
zf.writestr(key, value)

zarr/tests/test_storage.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from zarr.compat import text_type
2222
from zarr.storage import default_compressor
2323
from zarr.codecs import Zlib, Blosc
24+
from zarr.errors import PermissionError
2425

2526

2627
class StoreTests(object):
@@ -634,6 +635,13 @@ def create_store(self):
634635
store = ZipStore(path)
635636
return store
636637

638+
def test_mode(self):
639+
store = ZipStore('example.zip', mode='w')
640+
store['foo'] = b'bar'
641+
store = ZipStore('example.zip', mode='r')
642+
with assert_raises(PermissionError):
643+
store['foo'] = b'bar'
644+
637645

638646
def test_getsize():
639647
store = dict()

0 commit comments

Comments
 (0)