[python-hdf5storage] 80/152: Fixed string encoding and decoding bugs.

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Feb 29 08:24:36 UTC 2016


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to annotated tag 0.1
in repository python-hdf5storage.

commit cadb9ebf16cb47a9a6436707f7806af89a84fd31
Author: Freja Nordsiek <fnordsie at gmail.com>
Date:   Sun Feb 2 04:50:13 2014 -0500

    Fixed string encoding and decoding bugs.
---
 hdf5storage/Marshallers.py |  6 ++++--
 hdf5storage/utilities.py   | 18 +++++++++---------
 2 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/hdf5storage/Marshallers.py b/hdf5storage/Marshallers.py
index 5bfe3a9..4866828 100644
--- a/hdf5storage/Marshallers.py
+++ b/hdf5storage/Marshallers.py
@@ -401,8 +401,10 @@ class NumpyScalarArrayMarshaller(TypeMarshaller):
             if data_to_store.nbytes == 0:
                 data_to_store = np.uint32([])
             else:
-                data_to_store = np.atleast_1d( \
-                    data_to_store).view(np.uint32)
+                shape = list(np.atleast_1d(data_to_store).shape)
+                shape[-1] *= data_to_store.dtype.itemsize//4
+                data_to_store = data_to_store.flatten().view(np.uint32)
+                data_to_store = data_to_store.reshape(tuple(shape))
 
         # Convert scalars to arrays if that option is set.
 
diff --git a/hdf5storage/utilities.py b/hdf5storage/utilities.py
index 8daeb31..3ddb125 100644
--- a/hdf5storage/utilities.py
+++ b/hdf5storage/utilities.py
@@ -100,17 +100,17 @@ def decode_to_str(data):
     if isinstance(data, (np.ndarray, np.uint8, np.uint16, np.uint32,
                   np.bytes_, np.str_)):
         if data.dtype.name == 'uint8':
-            return data.data.tobytes().decode(encoding='ASCII')
+            return data.flatten().tostring().decode(encoding='ASCII')
         elif data.dtype.name == 'uint16':
-            return data.data.tobytes().decode(encoding='UTF-16')
+            return data.tostring().decode(encoding='UTF-16')
         elif data.dtype.name == 'uint32':
-            return data.data.tobytes().decode(encoding='UTF-32')
+            return data.flatten.tostring().decode(encoding='UTF-32')
         elif data.dtype.name.startswith('bytes'):
             return data.decode(encoding='ASCII')
         else:
             if isinstance(data, np.ndarray):
-                return data.tostring().decode(encoding='UTF-32',
-                                              errors='replace')
+                return data.flatten.tostring().decode(encoding='UTF-32',
+                                                      errors='replace')
             else:
                 return data.encode(encoding='UTF-32').decode( \
                     encoding='UTF-32', errors='replace')
@@ -190,7 +190,7 @@ def decode_to_numpy_unicode(data, length=None):
         # needs to be have the dtype essentially changed by having its
         # bytes read into ndarray.
         return np.ndarray(shape=tuple(), dtype='U1',
-                          buffer=data.tostring())[()]
+                          buffer=data.flatten().tostring())[()]
     elif isinstance(data, np.ndarray) and data.dtype.char == 'S':
         # We just need to convert it elementwise.
         new_data = np.zeros(shape=data.shape,
@@ -224,7 +224,7 @@ def decode_to_numpy_unicode(data, length=None):
             new_shape = (shape[0]//length,)
         else:
             if length is None:
-                length = shape[1]
+                length = shape[-1]
             new_shape = shape.copy()
             new_shape[-1] //= length
 
@@ -320,7 +320,7 @@ def decode_to_numpy_ascii(data, length=None):
         # needs to be have the dtype essentially changed by having its
         # bytes read into ndarray.
         return np.ndarray(shape=tuple(), dtype='S1',
-                          buffer=data.tostring())[()]
+                          buffer=data.flatten().tostring())[()]
     elif isinstance(data, np.ndarray) and data.dtype.char == 'U':
         # We just need to convert it elementwise.
         new_data = np.zeros(shape=data.shape,
@@ -355,7 +355,7 @@ def decode_to_numpy_ascii(data, length=None):
             new_shape = (shape[0]//length,)
         else:
             if length is None:
-                length = shape[1]
+                length = shape[-1]
             new_shape = shape.copy()
             new_shape[-1] //= length
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/python-hdf5storage.git



More information about the debian-science-commits mailing list