storage:hdf5: Add support for dtype(object) and 0d string arrays
authorW. Trevor King <wking@tremily.us>
Sat, 19 Jan 2013 03:15:29 +0000 (22:15 -0500)
committerW. Trevor King <wking@tremily.us>
Sat, 19 Jan 2013 03:15:29 +0000 (22:15 -0500)
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

index ce81289a4dccd430332a32284dce9378bcc4c26e..47a522c7c28d18b4e48045eacc10c965fd2ee548 100644 (file)
@@ -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: