[h5py] 375/455: Rationalize typecode handling in H5R

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Thu Jul 2 18:19:52 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 ebdd72170fb003e4d056d1f5fff0b9fb08d70d06
Author: andrewcollette <andrew.collette at gmail.com>
Date:   Fri Jan 29 05:30:58 2010 +0000

    Rationalize typecode handling in H5R
---
 h5py/h5r.pxd                 |  1 +
 h5py/h5r.pyx                 | 37 ++++++++++---------------------------
 h5py/tests/high/test_refs.py |  8 ++++++--
 3 files changed, 17 insertions(+), 29 deletions(-)

diff --git a/h5py/h5r.pxd b/h5py/h5r.pxd
index 90a978c..2ad219f 100644
--- a/h5py/h5r.pxd
+++ b/h5py/h5r.pxd
@@ -25,6 +25,7 @@ cdef class Reference:
 
     cdef ref_u ref
     cdef readonly int typecode
+    cdef readonly size_t typesize
 
 cdef class RegionReference(Reference):
 
diff --git a/h5py/h5r.pyx b/h5py/h5r.pyx
index 85c508a..722dfc3 100644
--- a/h5py/h5r.pyx
+++ b/h5py/h5r.pyx
@@ -64,7 +64,6 @@ def create(ObjectID loc not None, char* name, int ref_type, SpaceID space=None):
         space_id = space.id
 
     H5Rcreate(&ref.ref, loc.id, name, <H5R_type_t>ref_type, space_id)
-    ref.typecode = ref_type
 
     return ref
 
@@ -157,37 +156,17 @@ cdef class Reference:
     """
 
     def __cinit__(self, *args, **kwds):
-        self.typecode = -1
+        self.typecode = H5R_OBJECT
+        self.typesize = sizeof(hobj_ref_t)
 
     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
+        for i from 0<=i<self.typesize:
+            if (<unsigned char*>&self.ref)[i] != 0: return True
         return False
 
     def __repr__(self):
-        if self.typecode == H5R_OBJECT:
-            desc_str = PyString_FromStringAndSize(<char*>&self.ref, sizeof(hobj_ref_t))
-            return "<HDF5 object reference (%r)>" % desc_str
-        elif self.typecode == H5R_DATASET_REGION:
-            desc_str = PyString_FromStringAndSize(<char*>&self.ref, sizeof(hdset_reg_ref_t))
-            return "<HDF5 dataset region reference (%r)>" % desc_str
-        return "<Invalid HDF5 reference>"
+        return "<HDF5 object reference%s>" % ("" if self else " (null)")
 
 cdef class RegionReference(Reference):
 
@@ -198,8 +177,12 @@ cdef class RegionReference(Reference):
         convenience.
     """
 
-    pass
+    def __cinit__(self, *args, **kwds):
+        self.typecode = H5R_DATASET_REGION
+        self.typesize = sizeof(hdset_reg_ref_t)
 
+    def __repr__(self):
+        return "<HDF5 region reference%s>" % ("" if self else " (null")
 
 
 
diff --git a/h5py/tests/high/test_refs.py b/h5py/tests/high/test_refs.py
index 4a78ec8..20c4092 100644
--- a/h5py/tests/high/test_refs.py
+++ b/h5py/tests/high/test_refs.py
@@ -53,14 +53,18 @@ class TestObjRef(Base):
         x = self.f[g.ref]
         self.assertIsNone(x.name)
 
-    @tests.fixme("TypeError in h5r")
     def test_bool(self):
         """ (Refs) __nonzero__ tracks validity """
         ref = h5py.Reference()
         self.assert_(not ref)
         self.assert_(self.f.ref)
 
-    @tests.fixme("TypeError in h5r")
+    def test_repr(self):
+        """ (Refs) __repr__ works on live and dead references """
+        ref = h5py.Reference()
+        self.assertIsInstance(repr(ref), basestring)
+        self.assertIsInstance(repr(self.f.ref), basestring)
+
     def test_exc(self):
         """ (Refs) Deref of empty ref raises ValueError """
         ref = h5py.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