From ec3a4a001ed6e1b4df9ff6e01ce8fcf0c99f7552 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Fri, 18 Jan 2013 22:15:29 -0500 Subject: [PATCH] storage:hdf5: Add support for dtype(object) and 0d string arrays I'm not sure if it was a due to upgrading libhdf5, h5py, or Numpy, but I'm getting strings back as `array('abc', dtype=object)` instead of `array('abc', dtype='|S3')`. Add some 0d-array-extraction logic to handle this case. --- h5config/storage/hdf5.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/h5config/storage/hdf5.py b/h5config/storage/hdf5.py index ce81289..47a522c 100644 --- a/h5config/storage/hdf5.py +++ b/h5config/storage/hdf5.py @@ -211,7 +211,7 @@ class HDF5_Storage (_FileStorage): if isinstance(v, _numpy.ndarray): if isinstance(s, _config.BooleanSetting): v = bool(v) # array(True, dtype=bool) -> True - elif v.dtype.type == _numpy.string_: + elif v.dtype.type in [_numpy.string_, _numpy.object_]: if isinstance(s, _config.ListSetting): try: v = list(v) @@ -222,6 +222,10 @@ class HDF5_Storage (_FileStorage): if isinstance(v_, bytes): v[i] = str(v_, 'utf-8') else: # array('abc', dtype='|S3') -> 'abc' + if not v.shape: + # array('abc', dtype=object) -> 'abc' + # convert from numpy 0d array + v = v.item() if _sys.version_info >= (3,): v = str(v, 'utf-8') else: -- 2.26.2