File tree Expand file tree Collapse file tree 2 files changed +12
-6
lines changed Expand file tree Collapse file tree 2 files changed +12
-6
lines changed Original file line number Diff line number Diff line change 16
16
from zarr .meta import encode_array_metadata , encode_group_metadata
17
17
from zarr .compat import PY2 , binary_type
18
18
from zarr .codecs import codec_registry
19
+ from zarr .errors import PermissionError
19
20
20
21
21
22
array_meta_key = '.zarray'
@@ -734,13 +735,10 @@ def __getitem__(self, key):
734
735
return f .read ()
735
736
736
737
def __setitem__ (self , key , value ):
738
+ if self .mode == 'r' :
739
+ raise PermissionError ('mapping is read-only' )
737
740
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' ,
744
742
compression = self .compression ,
745
743
allowZip64 = self .allowZip64 ) as zf :
746
744
zf .writestr (key , value )
Original file line number Diff line number Diff line change 21
21
from zarr .compat import text_type
22
22
from zarr .storage import default_compressor
23
23
from zarr .codecs import Zlib , Blosc
24
+ from zarr .errors import PermissionError
24
25
25
26
26
27
class StoreTests (object ):
@@ -634,6 +635,13 @@ def create_store(self):
634
635
store = ZipStore (path )
635
636
return store
636
637
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
+
637
645
638
646
def test_getsize ():
639
647
store = dict ()
You can’t perform that action at this time.
0 commit comments