Skip to content

Commit 810dc56

Browse files
committed
catch segfault on read
1 parent e3d7972 commit 810dc56

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

zarr/core.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,7 +1570,8 @@ def _chunk_getitem(self, chunk_coords, chunk_selection, out, out_selection,
15701570
not fields and
15711571
is_contiguous_selection(out_selection) and
15721572
is_total_slice(chunk_selection, self._chunks) and
1573-
not self._filters):
1573+
not self._filters and
1574+
self._dtype != object):
15741575

15751576
dest = out[out_selection]
15761577
write_direct = (
@@ -1727,7 +1728,12 @@ def _decode_chunk(self, cdata):
17271728
chunk = f.decode(chunk)
17281729

17291730
# view as correct dtype
1730-
if isinstance(chunk, np.ndarray):
1731+
if self._dtype == object:
1732+
if isinstance(chunk, np.ndarray):
1733+
chunk = chunk.astype(self._dtype)
1734+
else:
1735+
raise RuntimeError('cannot read object array without object codec')
1736+
elif isinstance(chunk, np.ndarray):
17311737
chunk = chunk.view(self._dtype)
17321738
else:
17331739
chunk = np.frombuffer(chunk, self._dtype)
@@ -1746,8 +1752,7 @@ def _encode_chunk(self, chunk):
17461752

17471753
# check object encoding
17481754
if isinstance(chunk, np.ndarray) and chunk.dtype == object:
1749-
# TODO review error message
1750-
raise RuntimeError('object array detected without encoding')
1755+
raise RuntimeError('cannot write object array without object codec')
17511756

17521757
# compress
17531758
if self._compressor:

zarr/tests/test_core.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
from zarr.errors import PermissionError
2222
from zarr.compat import PY2
2323
from zarr.util import buffer_size
24-
from numcodecs import Delta, FixedScaleOffset, Zlib, Blosc, BZ2, MsgPack, Pickle
24+
from numcodecs import (Delta, FixedScaleOffset, Zlib, Blosc, BZ2, MsgPack, Pickle,
25+
Categorize)
2526

2627

2728
# noinspection PyMethodMayBeStatic
@@ -911,6 +912,32 @@ def test_object_arrays_danger(self):
911912
with assert_raises(RuntimeError):
912913
z[:] = 42
913914

915+
# do something else dangerous
916+
labels = [
917+
'¡Hola mundo!',
918+
'Hej Världen!',
919+
'Servus Woid!',
920+
'Hei maailma!',
921+
'Xin chào thế giới',
922+
'Njatjeta Botë!',
923+
'Γεια σου κόσμε!',
924+
'こんにちは世界',
925+
'世界,你好!',
926+
'Helló, világ!',
927+
'Zdravo svete!',
928+
'เฮลโลเวิลด์'
929+
]
930+
data = labels * 10
931+
for compressor in Zlib(1), Blosc():
932+
z = self.create_array(shape=len(data), chunks=30, dtype=object,
933+
object_codec=Categorize(labels, dtype=object),
934+
compressor=compressor)
935+
z[:] = data
936+
v = z.view(filters=[])
937+
with assert_raises(RuntimeError):
938+
# noinspection PyStatementEffect
939+
v[:]
940+
914941

915942
class TestArrayWithPath(TestArray):
916943

0 commit comments

Comments
 (0)