[python-hdf5storage] 01/84: Made it so that MATLAB compatible userblock is put in HDF5 file before data is written as opposed to after (fixes bug where the file becomes not MATLAB compatible if an exception occurs).

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


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

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

commit 9a271e869c3df9ef84f4dc86b7d6700444c92f77
Author: Freja Nordsiek <fnordsie at gmail.com>
Date:   Sun Aug 10 02:46:31 2014 -0400

    Made it so that MATLAB compatible userblock is put in HDF5 file before data is written as opposed to after (fixes bug where the file becomes not MATLAB compatible if an exception occurs).
---
 hdf5storage/__init__.py | 50 ++++++++++++++++++++++++++++++-------------------
 1 file changed, 31 insertions(+), 19 deletions(-)

diff --git a/hdf5storage/__init__.py b/hdf5storage/__init__.py
index 710cd99..20af459 100644
--- a/hdf5storage/__init__.py
+++ b/hdf5storage/__init__.py
@@ -947,11 +947,10 @@ def write(data, path='/', filename='data.h5', truncate_existing=False,
     if targetname == '':
         targetname = '.'
 
-    # Open the hdf5 file and start writing the data (and making the
-    # group groupname at the same time if it doesn't exist). This is all
-    # wrapped in a try block, so that the file can be closed if any
-    # errors happen (the error is re-raised).
-
+    # Open/create the hdf5 file but don't write the data yet since the
+    # userblock still needs to be set. This is all wrapped in a try
+    # block, so that the file can be closed if any errors happen (the
+    # error is re-raised).
     f = None
     try:
 
@@ -977,30 +976,20 @@ def write(data, path='/', filename='data.h5', truncate_existing=False,
                     and f.userblock_size < 128:
                 f.close()
                 f = h5py.File(filename, mode='w', userblock_size=512)
-
-        # Need to make sure groupname is a valid group in f and grab its
-        # handle to pass on to the low level function.
-
-        if groupname not in f:
-            grp = f.require_group(groupname)
-        else:
-            grp = f[groupname]
-
-        # Hand off to the low level function.
-        lowlevel.write_data(f, grp, targetname, data,
-                            None, options)
     except:
-        print("Unexpected error:", sys.exc_info()[0])
         raise
     finally:
+        # If the hdf5 file was opened at all, get the userblock size and
+        # close it since we need to set the userblock.
         if isinstance(f, h5py.File):
             userblock_size = f.userblock_size
             f.close()
+        else:
+            raise IOError('Unable to create or open file.')
 
     # If we are doing MATLAB formatting and there is a sufficiently
     # large userblock, write the new userblock. The same sort of error
     # handling is used.
-
     if options.matlab_compatible and userblock_size >= 128:
         # Get the time.
         now = datetime.datetime.now()
@@ -1049,6 +1038,29 @@ def write(data, path='/', filename='data.h5', truncate_existing=False,
         finally:
             fd.close()
 
+    # Open the hdf5 file again and write the data, making the Group if
+    # necessary. This is all wrapped in a try block, so that the file
+    # can be closed if any errors happen (the error is re-raised).
+    f = None
+    try:
+        f = h5py.File(filename)
+        
+        # Need to make sure groupname is a valid group in f and grab its
+        # handle to pass on to the low level function.
+        if groupname not in f:
+            grp = f.require_group(groupname)
+        else:
+            grp = f[groupname]
+
+        # Hand off to the low level function.
+        lowlevel.write_data(f, grp, targetname, data,
+                            None, options)
+    except:
+        raise
+    finally:
+        if isinstance(f, h5py.File):
+            f.close()
+
 
 def read(path='/', filename='data.h5',
          options=None, **keywords):

-- 
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