[h5py] 38/455: H5T additions

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Thu Jul 2 18:19:15 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 068b36041592f56352a78cd37e1ceef31c59a977
Author: andrewcollette <andrew.collette at gmail.com>
Date:   Fri Jun 6 02:19:49 2008 +0000

    H5T additions
---
 h5py/h5.pyx  |  7 +++--
 h5py/h5t.pxd |  7 ++---
 h5py/h5t.pyx | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++---------
 3 files changed, 84 insertions(+), 18 deletions(-)

diff --git a/h5py/h5.pyx b/h5py/h5.pyx
index c72d7ad..6e0a618 100644
--- a/h5py/h5.pyx
+++ b/h5py/h5.pyx
@@ -24,15 +24,18 @@
 """
 from h5e cimport H5Eset_auto, H5E_walk_t, H5Ewalk, H5E_error_t, \
                       H5E_WALK_DOWNWARD
-
+from h5t cimport H5Tset_overflow
 from errors import H5LibraryError
 
-# Activate the library
+# === Library init ============================================================
+
 H5open()
 
 # Disable automatic error printing to stderr
 H5Eset_auto(NULL, NULL)
 
+# === API =====================================================================
+
 def get_libversion():
     """ () => TUPLE (major, minor, release)
 
diff --git a/h5py/h5t.pxd b/h5py/h5t.pxd
index 4010402..0e1eaa9 100644
--- a/h5py/h5t.pxd
+++ b/h5py/h5t.pxd
@@ -159,8 +159,11 @@ cdef extern from "hdf5.h":
   herr_t        H5Tclose(hid_t type_id)
 
   hid_t         H5Tget_native_type(hid_t type_id, H5T_direction_t direction)
+
   # Not for public API
-  herr_t        H5Tconvert(hid_t src_id, hid_t dst_id, size_t nelmts, void *buf, void *background, hid_t plist_id)
+  herr_t        H5Tconvert(hid_t src_id, hid_t dst_id, size_t nelmts, void *buf, void *background, hid_t plist_id) except *
+  ctypedef herr_t  (*H5T_overflow_t)(hid_t src_id, hid_t dst_id, void *src_buf, void *dst_buf) except -1
+  herr_t        H5Tset_overflow(H5T_overflow_t func)
 
   # Atomic datatypes
   herr_t        H5Tset_size(hid_t type_id, size_t size)
@@ -217,8 +220,6 @@ cdef extern from "hdf5.h":
   herr_t    H5Tenum_nameof( hid_t type, void *value, char *name, size_t size  )
   herr_t    H5Tenum_valueof( hid_t type, char *name, void *value  )
   herr_t    H5Tget_member_value(hid_t type,  unsigned int memb_no, void *value  )
-  #char*     H5Tget_member_name(hid_t type_id, unsigned field_idx  )
-  #int       H5Tget_member_index(hid_t type_id, char * field_name  )
 
   # Array data types
   hid_t H5Tarray_create(hid_t base_id, int ndims, hsize_t dims[], int perm[])
diff --git a/h5py/h5t.pyx b/h5py/h5t.pyx
index 8f6fba4..348b842 100644
--- a/h5py/h5t.pyx
+++ b/h5py/h5t.pyx
@@ -587,38 +587,100 @@ def enum_create(hid_t base_id):
         raise DatatypeError("Failed to create enum of class %d" % base_id)
     return retval
 
+cdef void _enum_convert(hid_t type_id, long long *buf, int reverse) except *:
+    # Convert the long long value in "buf" to the native representation
+    # of type_id.  Conversion performed in-place, DatatypeError raised
+    # on failure.
+    # Reverse: zero => llong->type; nonzero => type->llong
+    cdef hid_t basetype
+    cdef herr_t retval
+    cdef H5T_class_t class_code
+
+    class_code = H5Tget_class(type_id)
+    if class_code != H5T_ENUM:
+        raise DatatypeError("Type %d is not of class ENUM")
+
+    basetype = H5Tget_super(type_id)
+    if basetype < 0:
+        raise DatatypeError("Failed to determine base type of datatype %d" % type_id)
+
+    try:
+        if not reverse:
+            retval = H5Tconvert(H5T_NATIVE_LLONG, basetype, 1, buf, NULL, H5P_DEFAULT)
+        else:
+            retval = H5Tconvert(basetype, H5T_NATIVE_LLONG, 1, buf, NULL, H5P_DEFAULT)
+        if retval < 0:
+            raise DatatypeError("Failed to convert enum value")
+    finally:
+        H5Tclose(basetype)
+
 def enum_insert(hid_t type_id, char* name, long long value):
     """ (INT type_id, STRING name, INT/LONG value)
 
         Define a new member of an enumerated type.  <value> will be
-        automatically converted to the base type defined for this enum.
+        automatically converted to the base type defined for this enum.  If
+        the conversion results in overflow, the value will be silently clipped.
     """
     cdef herr_t retval
     cdef hid_t ptype
-    cdef long long *data_ptr
+    cdef long long buf
     ptype = 0
 
-    data_ptr = <long long*>malloc(sizeof(long long))
     try:
-        data_ptr[0] = value
-        ptype = H5Tget_super(type_id)
-        retval = H5Tconvert(H5T_NATIVE_LLONG, ptype, 1, data_ptr, NULL, H5P_DEFAULT)
-        if retval < 0:
-            raise DatatypeError("Can't preconvert integer for enum insert")
-        retval = H5Tenum_insert(type_id, name, data_ptr)
+        buf = value
+        _enum_convert(type_id, &buf, 0)
+        retval = H5Tenum_insert(type_id, name, &buf)
         if retval < 0:
             raise DatatypeError("Failed to insert '%s' (value %d) into enum %d" % (name, value, type_id))
     finally:
         if ptype:
             H5Tclose(ptype)
-        free(data_ptr)
 
-#  herr_t    H5Tget_member_value(hid_t type  unsigned memb_no, void *value  )
+def enum_nameof(hid_t type_id, long long value):
+    """ (INT type_id, LLONG value) => STRING name
+
+        Determine the name associated with the given value.  Due to a
+        limitation of the HDF5 library, this can only retrieve names up to
+        1023 characters; otherwise, DatatypeError is raised.
+    """
+    cdef herr_t retval
+    cdef char* name
+    cdef long long buf
+
+    name = <char*>malloc(1024)
+
+    try:
+        buf = value
+        _enum_convert(type_id, &buf, 0)
+        retval = H5Tenum_nameof(type_id, &buf, name, 1024)
+        if retval < 0:
+            raise DatatypeError("Failed to determine enum name of %d" % value)
+        retstring = name
+
+    finally:
+        free(name)
+
+    return retstring
+
+def enum_valueof(hid_t type_id, char* name):
+    """ (INT type_id, STRING name) => LONG value)
+
+        Get the value associated with an enum name.
+    """
+    cdef herr_t retval
+    cdef long long buf
+
+    retval = H5Tenum_valueof(type_id, name, &buf)
+    if retval < 0:
+        raise DatatypeError("Failed to recover value of %s" % name)
+
+    _enum_convert(type_id, &buf, 1)
+    return buf
+
 def get_member_value(hid_t type_id, unsigned int idx):
     """ (INT type_id, UINT index) => LONG value
 
-        Determine the value for the member at <index> of enumerated type
-        <type_id>
+        Determine the value for the member at the given zero-based index.
     """
     cdef herr_t retval
     cdef hid_t ptype

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