Skip to content

Commit 4606320

Browse files
committed
fix PY2 issues
1 parent 12b448a commit 4606320

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

zarr/meta.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44

55
import os
66
import json
7-
import ast
7+
import sys
88
import numpy as np
99

1010

1111
from zarr import defaults as _defaults
1212

1313

14+
PY2 = sys.version_info[0] == 2
15+
16+
1417
def read_array_metadata(path):
1518

1619
# check path exists
@@ -38,12 +41,15 @@ def read_array_metadata(path):
3841
def write_array_metadata(path, shape, chunks, dtype, cname, clevel, shuffle,
3942
fill_value):
4043

44+
if not PY2:
45+
cname = str(cname, 'ascii')
46+
4147
# construct metadata dictionary
4248
meta = dict(
4349
shape=shape,
4450
chunks=chunks,
4551
dtype=encode_dtype(dtype),
46-
cname=str(cname, 'ascii'),
52+
cname=cname,
4753
clevel=clevel,
4854
shuffle=shuffle,
4955
fill_value=fill_value,
@@ -63,12 +69,17 @@ def encode_dtype(d):
6369

6470

6571
def _decode_dtype_descr(d):
66-
# need to convert list items to tuples
72+
# need to convert list of lists to list of tuples
6773
if isinstance(d, list):
6874
# recurse to handle nested structures
69-
d = [(f, _decode_dtype_descr(v)) for f, v in d]
75+
if PY2:
76+
# under PY2 numpy rejects unicode field names
77+
d = [(f.encode('ascii'), _decode_dtype_descr(v)) for f, v in d]
78+
else:
79+
d = [(f, _decode_dtype_descr(v)) for f, v in d]
7080
return d
7181

7282

7383
def decode_dtype(d):
74-
return np.dtype(_decode_dtype_descr(d))
84+
d = _decode_dtype_descr(d)
85+
return np.dtype(d)

0 commit comments

Comments
 (0)