File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1616from zarr .meta import encode_array_metadata , encode_group_metadata
1717from zarr .compat import PY2 , binary_type
1818from zarr .codecs import codec_registry
19+ from zarr .errors import PermissionError
1920
2021
2122array_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 )
Original file line number Diff line number Diff line change 2121from zarr .compat import text_type
2222from zarr .storage import default_compressor
2323from zarr .codecs import Zlib , Blosc
24+ from zarr .errors import PermissionError
2425
2526
2627class 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
638646def test_getsize ():
639647 store = dict ()
You can’t perform that action at this time.
0 commit comments