Skip to content

FSStore: use ensure_bytes() #1285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion zarr/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1378,13 +1378,16 @@ def __getitem__(self, key):
def setitems(self, values):
if self.mode == 'r':
raise ReadOnlyError()
values = {self._normalize_key(key): val for key, val in values.items()}

# Normalize keys and make sure the values are bytes
values = {self._normalize_key(key): ensure_bytes(val) for key, val in values.items()}
self.map.setitems(values)

def __setitem__(self, key, value):
if self.mode == 'r':
raise ReadOnlyError()
key = self._normalize_key(key)
value = ensure_bytes(value)
path = self.dir_path(key)
try:
if self.fs.isdir(path):
Expand Down
25 changes: 25 additions & 0 deletions zarr/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from numpy.testing import assert_array_almost_equal, assert_array_equal
from pkg_resources import parse_version

import zarr
from zarr._storage.store import (
v3_api_available,
)
Expand Down Expand Up @@ -3377,3 +3378,27 @@ def test_array_mismatched_store_versions():
Array(store_v3, path='dataset', read_only=False, chunk_store=chunk_store_v2)
with pytest.raises(ValueError):
Array(store_v2, path='dataset', read_only=False, chunk_store=chunk_store_v3)


@pytest.mark.skipif(have_fsspec is False, reason="needs fsspec")
def test_issue_1279(tmpdir):
"""See <https://github.com/zarr-developers/zarr-python/issues/1279>"""

data = np.arange(25).reshape((5, 5))
ds = zarr.create(
shape=data.shape,
chunks=(5, 5),
dtype=data.dtype,
compressor=(None),
store=FSStore(url=str(tmpdir), mode="a"),
order="F",
)

ds[:] = data

ds_reopened = zarr.open_array(
store=FSStore(url=str(tmpdir), mode="r")
)

written_data = ds_reopened[:]
assert_array_equal(data, written_data)