Skip to content

Commit 3c00d52

Browse files
committed
resolves #79; also some variable name consistification
1 parent e41e465 commit 3c00d52

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

zarr/core.py

+24-13
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ def __getitem__(self, item):
493493
else:
494494
return out[()]
495495

496-
def __setitem__(self, key, value):
496+
def __setitem__(self, item, value):
497497
"""Modify data for some portion of the array.
498498
499499
Examples
@@ -567,7 +567,7 @@ def __setitem__(self, key, value):
567567
self._load_metadata_nosync()
568568

569569
# normalize selection
570-
selection = normalize_array_selection(key, self._shape)
570+
selection = normalize_array_selection(item, self._shape)
571571

572572
# check value shape
573573
expected_shape = tuple(
@@ -674,14 +674,14 @@ def _chunk_getitem(self, cidx, item, dest):
674674
else:
675675
dest[()] = tmp
676676

677-
def _chunk_setitem(self, cidx, key, value):
677+
def _chunk_setitem(self, cidx, item, value):
678678
"""Replace part or whole of a chunk.
679679
680680
Parameters
681681
----------
682682
cidx : tuple of ints
683683
Indices of the chunk.
684-
key : tuple of slices
684+
item : tuple of slices
685685
Location of region within the chunk.
686686
value : scalar or ndarray
687687
Value to set.
@@ -690,19 +690,19 @@ def _chunk_setitem(self, cidx, key, value):
690690

691691
# synchronization
692692
if self._synchronizer is None:
693-
self._chunk_setitem_nosync(cidx, key, value)
693+
self._chunk_setitem_nosync(cidx, item, value)
694694
else:
695695
# synchronize on the chunk
696696
ckey = self._chunk_key(cidx)
697697
with self._synchronizer[ckey]:
698-
self._chunk_setitem_nosync(cidx, key, value)
698+
self._chunk_setitem_nosync(cidx, item, value)
699699

700-
def _chunk_setitem_nosync(self, cidx, key, value):
700+
def _chunk_setitem_nosync(self, cidx, item, value):
701701

702702
# obtain key for chunk storage
703703
ckey = self._chunk_key(cidx)
704704

705-
if is_total_slice(key, self._chunks):
705+
if is_total_slice(item, self._chunks):
706706
# totally replace chunk
707707

708708
# optimization: we are completely replacing the chunk, so no need
@@ -717,11 +717,22 @@ def _chunk_setitem_nosync(self, cidx, key, value):
717717

718718
else:
719719

720-
# ensure array is contiguous
721-
if self._order == 'F':
722-
chunk = np.asfortranarray(value, dtype=self._dtype)
720+
if not self._compressor and not self._filters:
721+
722+
# https://github.com/alimanfoo/zarr/issues/79
723+
# Ensure a copy is taken so we don't end up storing
724+
# a view into someone else's array.
725+
# N.B., this assumes that filters or compressor always
726+
# take a copy and never attempt to apply encoding in-place.
727+
chunk = np.array(value, dtype=self._dtype,
728+
order=self._order)
729+
723730
else:
724-
chunk = np.ascontiguousarray(value, dtype=self._dtype)
731+
# ensure array is contiguous
732+
if self._order == 'F':
733+
chunk = np.asfortranarray(value, dtype=self._dtype)
734+
else:
735+
chunk = np.ascontiguousarray(value, dtype=self._dtype)
725736

726737
else:
727738
# partially replace the contents of this chunk
@@ -747,7 +758,7 @@ def _chunk_setitem_nosync(self, cidx, key, value):
747758
chunk = chunk.copy(order='K')
748759

749760
# modify
750-
chunk[key] = value
761+
chunk[item] = value
751762

752763
# encode chunk
753764
cdata = self._encode_chunk(chunk)

zarr/tests/test_core.py

+10
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,16 @@ def test_array_order(self):
299299
actual = z[:]
300300
assert_array_equal(a, actual)
301301

302+
def test_setitem_data_not_shared(self):
303+
# check that data don't end up being shared with another array
304+
# https://github.com/alimanfoo/zarr/issues/79
305+
z = self.create_array(shape=20, chunks=10, dtype='i4')
306+
a = np.arange(20, dtype='i4')
307+
z[:] = a
308+
assert_array_equal(z[:], np.arange(20, dtype='i4'))
309+
a[:] = 0
310+
assert_array_equal(z[:], np.arange(20, dtype='i4'))
311+
302312
def test_resize_1d(self):
303313

304314
z = self.create_array(shape=105, chunks=10, dtype='i4',

0 commit comments

Comments
 (0)