[h5py] 46/455: More cleanup

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Thu Jul 2 18:19:16 UTC 2015


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

ghisvail-guest pushed a commit to annotated tag 1.3.0
in repository h5py.

commit 71e0fb77e60e64e750dcd75ee4d1921f90f42dff
Author: andrewcollette <andrew.collette at gmail.com>
Date:   Tue Jun 10 06:25:08 2008 +0000

    More cleanup
---
 h5py/h5a.pyx           |  7 ++++++-
 h5py/h5e.pyx           |  1 +
 h5py/h5f.pyx           |  4 ++--
 h5py/h5g.pyx           | 15 +++++++++++----
 h5py/h5i.pyx           |  9 +++++++--
 h5py/h5t.pyx           |  7 +++----
 h5py/tests/__init__.py |  6 +++---
 h5py/tests/test_h5d.py | 22 +++++++++++-----------
 h5py/tests/test_h5f.py | 23 +++++++++++------------
 h5py/tests/test_h5i.py | 10 +++++-----
 10 files changed, 60 insertions(+), 44 deletions(-)

diff --git a/h5py/h5a.pyx b/h5py/h5a.pyx
index a3f21c2..731a725 100644
--- a/h5py/h5a.pyx
+++ b/h5py/h5a.pyx
@@ -44,11 +44,16 @@ def create(hid_t loc_id, char* name, hid_t type_id, hid_t space_id):
     """
     return H5Acreate(loc_id, name, type_id, space_id, H5P_DEFAULT)
 
-def open_idx(hid_t loc_id, unsigned int idx):
+def open_idx(hid_t loc_id, int idx):
     """ (INT loc_id, UINT idx) => INT attr_id
 
         Open an exisiting attribute on an object, by zero-based index.
     """
+    # If the argument is declared UINT and someone passes in -1, the Pyrex
+    # layer happily converts it to something like 2**32 -1, which crashes the
+    # HDF5 library.
+    if idx < 0:
+        raise ValueError("Index must be a non-negative integer.")
     return H5Aopen_idx(loc_id, idx)
 
 def open_name(hid_t loc_id, char* name):
diff --git a/h5py/h5e.pyx b/h5py/h5e.pyx
index a2c959f..4b39e9c 100644
--- a/h5py/h5e.pyx
+++ b/h5py/h5e.pyx
@@ -221,6 +221,7 @@ cdef herr_t err_callback(void* client_data):
     # Can't use the standard Pyrex raise because then the traceback
     # points here!
 
+    print "Error callback"
     cdef H5E_error_t err_struct
     cdef H5E_major_t mj
     cdef H5E_minor_t mn
diff --git a/h5py/h5f.pyx b/h5py/h5f.pyx
index cc0d4ec..4f7e20b 100644
--- a/h5py/h5f.pyx
+++ b/h5py/h5f.pyx
@@ -156,9 +156,9 @@ def get_name(hid_t obj_id):
     name = NULL
 
     size = H5Fget_name(obj_id, NULL, 0)
-    name = <char*>emalloc(sizeof(char)*(retval+1))
+    name = <char*>emalloc(sizeof(char)*(size+1))
     try:    
-        H5Fget_name(obj_id, name, retval+1)
+        H5Fget_name(obj_id, name, size+1)
         pname = name
         return pname
     finally:
diff --git a/h5py/h5g.pyx b/h5py/h5g.pyx
index d5848b8..9ecfb45 100644
--- a/h5py/h5g.pyx
+++ b/h5py/h5g.pyx
@@ -139,12 +139,12 @@ def get_objname_by_idx(hid_t loc_id, hsize_t idx):
     buf = NULL
 
     size = H5Gget_objname_by_idx(loc_id, idx, NULL, 0)
-    if retval <= 0:
+    if size <= 0:
         raise RuntimeError("Failed to raise exception at get_objname_by_idx.")
 
-    buf = <char*>emalloc(sizeof(char)*(retval+1))
+    buf = <char*>emalloc(sizeof(char)*(size+1))
     try:
-        H5Gget_objname_by_idx(loc_id, idx, buf, retval+1)
+        H5Gget_objname_by_idx(loc_id, idx, buf, size+1)
         pystring = buf
         return pystring
     finally:
@@ -160,7 +160,14 @@ def get_objtype_by_idx(hid_t loc_id, hsize_t idx):
             - DATASET
             - DATATYPE
     """
-    return H5Gget_objtype_by_idx(loc_id, idx)
+    # What seems to be a bug in the HDF5 library prevents an error callback
+    # for an out-of-range index, although -1 is returned.  Interestingly,
+    # passing an invalid loc_id does raise an error.
+    cdef herr_t retval
+    retval = H5Gget_objtype_by_idx(loc_id, idx)
+    if retval < 0:
+        raise ValueError("Invalid argument.")
+    return retval
 
 def get_objinfo(hid_t loc_id, char* name, int follow_link=1):
     """ (INT loc_id, STRING name, BOOL follow_link=True)
diff --git a/h5py/h5i.pyx b/h5py/h5i.pyx
index 805e9ac..7b36d2c 100644
--- a/h5py/h5i.pyx
+++ b/h5py/h5i.pyx
@@ -55,13 +55,18 @@ def get_name(hid_t obj_id):
     """ (INT obj_id) => STRING name or None
 
         Determine (a) name of an HDF5 object.  Because an object has as many
-        names as there are hard links to it, this may not be unique.
+        names as there are hard links to it, this may not be unique.  If
+        the identifier is valid but is not associated with a name, returns
+        None.
     """
     cdef int namelen
     cdef char* name
 
     namelen = <int>H5Iget_name(obj_id, NULL, 0)
-    if namelen <= 0:
+    if namelen == 0:
+        return None
+
+    if namelen < 0:
         raise RuntimeError("Failed to raise exception at get_name")
 
     name = <char*>emalloc(sizeof(char)*(namelen+1))
diff --git a/h5py/h5t.pyx b/h5py/h5t.pyx
index 93cba17..91836d2 100644
--- a/h5py/h5t.pyx
+++ b/h5py/h5t.pyx
@@ -266,8 +266,7 @@ def close(hid_t type_id, int force=0):
     """ (INT type_id, BOOL force=False)
 
         Close this datatype.  If "force" is True, ignore any errors.  Useful
-        for exception handlers, when you're not sure if you've got an immutable
-        datatype.
+        when you're not sure if you've got an immutable datatype.
     """
     if force:
         PY_H5Tclose(type_id)
@@ -516,7 +515,7 @@ cdef void _enum_convert(hid_t type_id, long long *buf, int reverse) except *:
         if retval < 0:
             raise DatatypeError("Failed to convert enum value")
     finally:
-        H5Tclose(basetype)
+        PY_H5Tclose(basetype)
 
 def enum_insert(hid_t type_id, char* name, long long value):
     """ (INT type_id, STRING name, INT/LONG value)
@@ -884,7 +883,7 @@ def py_dtype_to_h5t(dtype dtype_in, object complex_names=None):
                 try:
                     insert(type_out, name, offset, tmp)
                 finally:
-                    H5Tclose(tmp)
+                    PY_H5Tclose(tmp)
 
     # Integers and floats map directly to HDF5 atomic types
     elif kind == c'u' or kind  == c'i' or kind == c'f': 
diff --git a/h5py/tests/__init__.py b/h5py/tests/__init__.py
index eea4fb6..8c0fd37 100644
--- a/h5py/tests/__init__.py
+++ b/h5py/tests/__init__.py
@@ -12,12 +12,12 @@
 
 import unittest
 import sys
-import test_h5a
+import test_h5a, test_h5f, test_h5i, test_h5d
 
 from h5py import h5a, h5f, h5g, h5d, h5s, h5i, h5z, h5p#, highlevel
 
-TEST_CASES = (test_h5a.TestH5A,)# test_h5f.TestH5F, test_h5g.TestH5G,
-              #test_h5i.TestH5I, test_h5d.TestH5D, test_h5.TestH5,
+TEST_CASES = (test_h5a.TestH5A, test_h5f.TestH5F, #test_h5g.TestH5G,
+              test_h5i.TestH5I, test_h5d.TestH5D)#, test_h5.TestH5,
               #test_h5p.TestH5P)
 
 def buildsuite(cases):
diff --git a/h5py/tests/test_h5d.py b/h5py/tests/test_h5d.py
index fef9665..d20da42 100644
--- a/h5py/tests/test_h5d.py
+++ b/h5py/tests/test_h5d.py
@@ -16,7 +16,7 @@ import numpy
 
 import h5py
 from h5py import h5f, h5d, h5i, h5s, h5t, h5p
-from h5py.errors import DatasetError
+from h5py.h5e import DatasetError
 
 HDFNAME = os.path.join(os.path.dirname(h5py.__file__), 'tests/data/smpl_compound_chunked.hdf5')
 DTYPE = numpy.dtype([('a_name','>i4'),
@@ -53,7 +53,7 @@ class TestH5D(unittest.TestCase):
         self.assertEqual(h5i.get_type(self.did), h5i.DATASET)
 
         self.assertRaises(DatasetError, h5d.open, self.fid, "Something else")
-        self.assertRaises(DatasetError, h5d.close, -1)
+        self.assertRaises(ValueError, h5d.close, -1)
 
     def test_read(self):
         array = numpy.ndarray(SHAPE, dtype=DTYPE)
@@ -62,7 +62,7 @@ class TestH5D(unittest.TestCase):
         for name in DTYPE.fields:
             self.assert_(numpy.all(array[name] == basearray[name]), "%s::\n%s\n!=\n%s" % (name, array[name], basearray[name]))
 
-        self.assertRaises(DatasetError, h5d.read, -1, h5s.ALL, h5s.ALL, array)
+        self.assertRaises(ValueError, h5d.read, -1, h5s.ALL, h5s.ALL, array)
 
     def test_get_space(self):
         sid = h5d.get_space(self.did)
@@ -71,16 +71,16 @@ class TestH5D(unittest.TestCase):
             self.assertEqual(shape, SHAPE)
         finally:
             h5s.close(sid)
-        self.assertRaises(DatasetError, h5d.get_space, -1)
+        self.assertRaises(ValueError, h5d.get_space, -1)
 
     def test_get_space_status(self):
         status = h5d.get_space_status(self.did)
         self.assert_(status in h5d.PY_SPACE_STATUS)
-        self.assertRaises(DatasetError, h5d.get_space_status, -1)
+        self.assertRaises(ValueError, h5d.get_space_status, -1)
 
     def test_get_offset(self):
         # Chunked datasets have no offset.  New test dset needed.
-        self.assertRaises(DatasetError, h5d.get_offset, -1)
+        self.assertRaises(ValueError, h5d.get_offset, -1)
 
     def test_get_storage_size(self):
         # This function can't intentionally raise an exception.
@@ -93,7 +93,7 @@ class TestH5D(unittest.TestCase):
             self.assertEqual(h5i.get_type(tid), h5i.DATATYPE)
         finally:
             h5t.close(tid)
-        self.assertRaises(DatasetError, h5d.get_type, -1)
+        self.assertRaises(ValueError, h5d.get_type, -1)
 
     def test_get_create_plist(self):
         pid = h5d.get_create_plist(self.did)
@@ -102,19 +102,19 @@ class TestH5D(unittest.TestCase):
         finally:
             h5p.close(pid)
 
-        self.assertRaises(DatasetError, h5d.get_create_plist, -1)
+        self.assertRaises(ValueError, h5d.get_create_plist, -1)
 
     def test_py_shape(self):
         self.assertEqual(h5d.py_shape(self.did), SHAPE)
-        self.assertRaises(DatasetError, h5d.py_shape, -1)
+        self.assertRaises(ValueError, h5d.py_shape, -1)
 
     def test_py_rank(self):
         self.assertEqual(h5d.py_rank(self.did), 1)
-        self.assertRaises(DatasetError, h5d.py_rank, -1)
+        self.assertRaises(ValueError, h5d.py_rank, -1)
 
     def test_py_dtype(self):
         self.assertEqual(type(h5d.py_dtype(self.did)), numpy.dtype)
-        self.assertRaises(DatasetError, h5d.py_dtype, -1)
+        self.assertRaises(ValueError, h5d.py_dtype, -1)
         
         
 
diff --git a/h5py/tests/test_h5f.py b/h5py/tests/test_h5f.py
index dbe31eb..2e49b59 100644
--- a/h5py/tests/test_h5f.py
+++ b/h5py/tests/test_h5f.py
@@ -16,7 +16,6 @@ import os
 
 import h5py
 from h5py import h5f, h5i, h5p
-from h5py.errors import FileError
 from common import getcopy, deletecopy, errstr
 
 HDFNAME = os.path.join(os.path.dirname(h5py.__file__), 'tests/data/attributes.hdf5')
@@ -35,21 +34,21 @@ class TestH5F(unittest.TestCase):
         h5f.close(fid)
         self.assertEqual(h5i.get_type(fid), h5i.BADID)
 
-        self.assertRaises(FileError, h5f.open, 'SOME OTHER NAME')
-        self.assertRaises(FileError, h5f.close, -1)
+        self.assertRaises(IOError, h5f.open, 'SOME OTHER NAME')
+        self.assertRaises(ValueError, h5f.close, -1)
 
     def test_create(self):
         name = tempfile.mktemp('.hdf5')
         fid = h5f.create(name)
         self.assertEqual(h5i.get_type(fid), h5i.FILE)
         h5f.close(fid)
-        self.assertRaises(FileError, h5f.create, name, h5f.ACC_EXCL)
+        self.assertRaises(IOError, h5f.create, name, h5f.ACC_EXCL)
         os.unlink(name)
 
     def test_flush(self):
         fid, fname = getcopy(HDFNAME)
         h5f.flush(fid)
-        self.assertRaises(FileError, h5f.flush, -1)
+        self.assertRaises(ValueError, h5f.flush, -1)
         deletecopy(fid, fname)
 
     def test_is_hdf5(self):
@@ -65,36 +64,36 @@ class TestH5F(unittest.TestCase):
     def test_get_filesize(self):
 
         self.assertEqual(h5f.get_filesize(self.fid), os.stat(HDFNAME).st_size)
-        self.assertRaises(FileError, h5f.get_filesize, -1)
+        self.assertRaises(ValueError, h5f.get_filesize, -1)
 
     def test_get_create_plist(self):
         cplist = h5f.get_create_plist(self.fid)
         self.assert_(h5p.equal(h5p.get_class(cplist), h5p.FILE_CREATE))
         h5p.close(cplist)
-        self.assertRaises(FileError, h5f.get_create_plist, -1)
+        self.assertRaises(ValueError, h5f.get_create_plist, -1)
 
     def test_get_access_plist(self):
         aplist = h5f.get_access_plist(self.fid)
         self.assert_(h5p.equal(h5p.get_class(aplist), h5p.FILE_ACCESS))
         h5p.close(aplist)
-        self.assertRaises(FileError, h5f.get_access_plist, -1)
+        self.assertRaises(ValueError, h5f.get_access_plist, -1)
 
     def test_get_freespace(self):
         self.assert_(h5f.get_freespace(self.fid) >= 0)
-        self.assertRaises(FileError, h5f.get_freespace, -1)
+        self.assertRaises(ValueError, h5f.get_freespace, -1)
 
     def test_get_name(self):
         self.assertEqual(h5f.get_name(self.fid), HDFNAME)
-        self.assertRaises(FileError, h5f.get_name, -1)
+        self.assertRaises(ValueError, h5f.get_name, -1)
 
     def test_get_obj_count(self):
         self.assert_(h5f.get_obj_count(self.fid, h5f.OBJ_ALL) >= 0)
-        self.assertRaises(FileError, h5f.get_obj_count, -1, h5f.OBJ_ALL)
+        self.assertRaises(ValueError, h5f.get_obj_count, -1, h5f.OBJ_ALL)
     
     def test_get_obj_ids(self):
         idlist = h5f.get_obj_ids(self.fid, h5f.OBJ_ALL)
         self.assert_(isinstance(idlist, list))
-        self.assertRaises(FileError, h5f.get_obj_ids, -1, h5f.OBJ_ALL)
+        self.assertRaises(ValueError, h5f.get_obj_ids, -1, h5f.OBJ_ALL)
 
 
 
diff --git a/h5py/tests/test_h5i.py b/h5py/tests/test_h5i.py
index b2086f9..8bee1f9 100644
--- a/h5py/tests/test_h5i.py
+++ b/h5py/tests/test_h5i.py
@@ -14,7 +14,7 @@ import os
 
 import h5py
 from h5py import h5f, h5g, h5i, h5t
-from h5py.errors import H5Error, IdentifierError
+from h5py.h5e import H5Error
 
 HDFNAME = os.path.join(os.path.dirname(h5py.__file__), 'tests/data/attributes.hdf5')
 OBJECTNAME = 'Group'
@@ -42,7 +42,7 @@ class TestH5I(unittest.TestCase):
     def test_get_file_id(self):
         nfid = h5i.get_file_id(self.obj)
         self.assertEqual(nfid, self.fid)
-        self.assertRaises(IdentifierError, h5i.get_file_id, -1)
+        self.assertRaises(H5Error, h5i.get_file_id, -1)
 
     def test_refs(self):
         refcnt = h5i.get_ref(self.obj)
@@ -54,9 +54,9 @@ class TestH5I(unittest.TestCase):
         h5i.dec_ref(self.obj)
         self.assertEqual(h5i.get_ref(self.obj), refcnt)
 
-        self.assertRaises(IdentifierError, h5i.get_ref, -1)
-        self.assertRaises(IdentifierError, h5i.inc_ref, -1)
-        self.assertRaises(IdentifierError, h5i.dec_ref, -1)
+        self.assertRaises(H5Error, h5i.get_ref, -1)
+        self.assertRaises(H5Error, h5i.inc_ref, -1)
+        self.assertRaises(H5Error, h5i.dec_ref, -1)
 
 
 

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



More information about the debian-science-commits mailing list