Skip to content

Commit 398820f

Browse files
committed
Simplify ensure_str in zarr.meta
If the data is already a `str` instance, turn `ensure_str` into a no-op. For all other cases, make use of Numcodecs' `ensure_bytes` to aid `ensure_str` in coercing data through the buffer protocol. If we are on Python 3, then decode the `bytes` object to a `str`.
1 parent 2c6ac77 commit 398820f

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

zarr/meta.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,20 @@
55

66

77
import numpy as np
8+
from numcodecs.compat import ensure_bytes
89

910

10-
from zarr.compat import PY2, binary_type, Mapping
11+
from zarr.compat import PY2, Mapping
1112
from zarr.errors import MetadataError
1213

1314

1415
ZARR_FORMAT = 2
1516

1617

1718
def ensure_str(s):
18-
if PY2: # pragma: py3 no cover
19-
# noinspection PyUnresolvedReferences
20-
if isinstance(s, buffer): # noqa
21-
s = str(s)
22-
else: # pragma: py2 no cover
23-
if isinstance(s, memoryview):
24-
s = s.tobytes()
25-
if isinstance(s, binary_type):
19+
if not isinstance(s, str):
20+
s = ensure_bytes(s)
21+
if not PY2: # pragma: py2 no cover
2622
s = s.decode('ascii')
2723
return s
2824

0 commit comments

Comments
 (0)