[h5py] 324/455: Make references behave more reasonably

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Thu Jul 2 18:19:47 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 65bb44fb99ef78e1b945749637a9fd7b032f5838
Author: andrewcollette <andrew.collette at gmail.com>
Date:   Tue Nov 24 05:23:36 2009 +0000

    Make references behave more reasonably
---
 h5py/h5r.pyx | 54 ++++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 46 insertions(+), 8 deletions(-)

diff --git a/h5py/h5r.pyx b/h5py/h5r.pyx
index 9567e51..5c124cb 100644
--- a/h5py/h5r.pyx
+++ b/h5py/h5r.pyx
@@ -29,7 +29,6 @@ init_hdf5()
 OBJECT = H5R_OBJECT
 DATASET_REGION = H5R_DATASET_REGION
 
-
 # === Reference API ===========================================================
 
 
@@ -64,19 +63,22 @@ def create(ObjectID loc not None, char* name, int ref_type, SpaceID space=None):
 
 
 def dereference(Reference ref not None, ObjectID id not None):
-    """(Reference ref, ObjectID id) => ObjectID
+    """(Reference ref, ObjectID id) => ObjectID or None
 
     Open the object pointed to by the reference and return its
     identifier.  The file identifier (or the identifier for any object
-    in the file) must also be provided.
+    in the file) must also be provided.  Returns None if the reference
+    is zero-filled.
 
     The reference type may be either OBJECT or DATASET_REGION.
     """
+    if not ref:
+        return None
     return wrap_identifier(H5Rdereference(id.id, <H5R_type_t>ref.typecode, &ref.ref))
 
 
 def get_region(Reference ref not None, ObjectID id not None):
-    """(Reference ref, ObjectID id) => SpaceID
+    """(Reference ref, ObjectID id) => SpaceID or None
 
     Retrieve the dataspace selection pointed to by the reference.
     Returns a copy of the dataset's dataspace, with the appropriate
@@ -84,13 +86,16 @@ def get_region(Reference ref not None, ObjectID id not None):
     object in the file (including the dataset itself) must also be
     provided.
 
-    The reference object must be of type DATASET_REGION.
+    The reference object must be of type DATASET_REGION.  If it's not, or
+    if the reference is zero-filled, returns None.
     """
+    if ref.typecode != H5R_DATASET_REGION or not ref:
+        return None
     return SpaceID(H5Rget_region(id.id, <H5R_type_t>ref.typecode, &ref.ref))
 
 
 def get_obj_type(Reference ref not None, ObjectID id not None):
-    """(Reference ref, ObjectID id) => INT obj_code
+    """(Reference ref, ObjectID id) => INT obj_code or None
 
     Determine what type of object the eference points to.  The
     reference may be either type OBJECT or DATASET_REGION.  The file
@@ -103,7 +108,11 @@ def get_obj_type(Reference ref not None, ObjectID id not None):
     - h5g.GROUP
     - h5g.DATASET
     - h5g.TYPE
+
+    If the reference is zero-filled, returns None.
     """
+    if not ref:
+        return None
     return <int>H5Rget_obj_type(id.id, <H5R_type_t>ref.typecode, &ref.ref)
 
 cdef class Reference:
@@ -115,13 +124,42 @@ cdef class Reference:
         cannot be modified.  The read-only attribute "typecode" determines 
         whether the reference is to an object in an HDF5 file (OBJECT) 
         or a dataset region (DATASET_REGION).
+
+        The object's truth value indicates whether it contains a nonzero
+        reference.  This does not guarantee that is valid, but is useful
+        for rejecting "background" elements in a dataset.
     """
 
+    def __cinit__(self, *args, **kwds):
+        self.typecode = -1
+
+    def __nonzero__(self):
+        # Whether or not the reference is zero-filled.  Note a True result
+        # does *not* mean the reference is valid.
+        cdef size_t obsize
+        cdef int i
+        cdef unsigned char *buf
+
+        if self.typecode == H5R_OBJECT:
+            obsize = sizeof(hobj_ref_t)
+        elif self.typecode == H5R_DATASET_REGION:
+            obsize = sizeof(hdset_reg_ref_t)
+        else:
+            raise TypeError("Unknown reference type")
+        
+        buf = <unsigned char*>&self.ref
+
+        for i from 0<=i<obsize:
+            if buf[i] != 0:
+                return True
+        return False
+
     def __repr__(self):
+        empty_str = "non-empty" if self else "empty"
         if self.typecode == H5R_OBJECT:
-            return "<HDF5 object reference>"
+            return "<HDF5 object reference (%s)>" % empty_str
         elif self.typecode == H5R_DATASET_REGION:
-            return "<HDF5 dataset region reference>"
+            return "<HDF5 dataset region reference (%s)>" % empty_str
         return "<Invalid HDF5 reference>"
 
 

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