[h5py] 433/455: Modify register_thread and unregister_thread

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Thu Jul 2 18:19:58 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 6489ad29f9d6a62022460ac5f83ad8e36d9e4cfd
Author: andrewcollette <andrew.collette at gmail.com>
Date:   Tue Feb 23 23:03:14 2010 +0000

    Modify register_thread and unregister_thread
---
 h5py/h5e.pxd                    | 13 ++++++++-
 h5py/h5e.pyx                    | 59 ++++++++++++++++++++++++++++-------------
 h5py/tests/high/test_threads.py |  2 +-
 3 files changed, 53 insertions(+), 21 deletions(-)

diff --git a/h5py/h5e.pxd b/h5py/h5e.pxd
index 35c6686..c48c0c5 100644
--- a/h5py/h5e.pxd
+++ b/h5py/h5e.pxd
@@ -17,9 +17,20 @@
 include "config.pxi"
 include "defs.pxd"
 
+cdef class HDF5ErrorHandler:
+
+    """
+        Opaque python-side encapsulation of an HDF5 error callback.
+        Contains a reference to both a function and its associated void* data.
+    """
+ 
+    cdef void* auto
+    cdef void* data
+
 # Register the current thread for native Python exception support.
 # Safe to call more than once.
-cpdef int register_thread() except -1
+cpdef object register_thread()
+cpdef object unregister_thread(HDF5ErrorHandler handler=*)
 
 cdef extern from "hdf5.h":
 
diff --git a/h5py/h5e.pyx b/h5py/h5e.pyx
index 5b5d2d2..4d2b9ab 100644
--- a/h5py/h5e.pyx
+++ b/h5py/h5e.pyx
@@ -378,8 +378,16 @@ cdef herr_t err_callback(void* client_data) with gil:
 
     return 1
 
-cpdef int register_thread() except -1:
-    """ ()
+cdef class HDF5ErrorHandler:
+
+    def __cinit__(self, *args, **kwds):
+        self.auto = NULL
+        self.data = NULL
+
+NullErrorHandler = HDF5ErrorHandler()
+
+cpdef object register_thread():
+    """ () => HDF5ErrorHandler
 
     Register the current thread for native HDF5 exception support.
 
@@ -388,32 +396,45 @@ cpdef int register_thread() except -1:
     registered when h5py is imported.  The high-level interface (h5py.*)
     is unaffected.
 
-    Safe to call more than once.
+    Returns an opaque object which represents the previously-installed error
+    handler.  Passing this object to unregister_thread will restore the
+    previous behavior.
     """
+    cdef HDF5ErrorHandler handler = HDF5ErrorHandler()
+    cdef H5E_auto_t auto
+    cdef void* data
+    if H5Eget_auto(&auto, &data) < 0:
+        raise RuntimeError("Failed to retrieve HDF5 error handler")
+    handler.auto = auto
+    handler.data = data
     if H5Eset_auto(err_callback, NULL) < 0:
         raise RuntimeError("Failed to register HDF5 exception callback")
-    return 0
+    return handler
 
-cpdef int unregister_thread(bint silent=0) except -1:
-    """ (silent=False)
+cpdef object unregister_thread(HDF5ErrorHandler handler=None):
+    """ (HDF5ErrorHandler handler=None)
 
     Unregister the current thread, turning off HDF5 exception support.
 
-    Restore the default HDF5 error handler, disabling h5py in the current
-    thread.  Third-party libraries in this thread are then free to interact
-    with the HDF5 error subsystem as they wish.  Call register_thread()
-    again to re-enable exception support.
+    This will disable h5py in the current thread, making third-party libraries
+    free to interact with the HDF5 error subsystem as they wish.  Call
+    register_thread() again to re-enable exception support.
+
+    If a "native" error handler has been retrieved with register_thread(), it
+    can be reinstalled by passing it to this function.  If not, it installs
+    the default HDF5 handler H5Eprint.
 
-    Does not affect any other thread.  Safe to call more than once.  If
-    "silent" is specified, uses a NULL error handler rather than H5Eprint.
+    Does not affect any other thread.  Safe to call more than once.
     """
-    if not silent:
-        if H5Eset_auto(H5Eprint, NULL) < 0:
-            raise RuntimeError("Failed to unregister HDF5 exception callback")
-    else:
-        if H5Eset_auto(NULL, NULL) < 0:
-            raise RuntimeError("Failed to unregister HDF5 exception callback")
-    return 0
+    cdef H5E_auto_t auto = H5Eprint
+    cdef void* data = NULL
+
+    if handler is not None:
+        auto = <H5E_auto_t>handler.auto
+        data = handler.data
+
+    if H5Eset_auto(auto, data) < 0:
+        raise RuntimeError("Failed to unregister HDF5 exception callback")
 
 cdef err_cookie disable_errors() except *:
     # Temporarily disable errors for the current thread
diff --git a/h5py/tests/high/test_threads.py b/h5py/tests/high/test_threads.py
index 780f800..03c2aa1 100644
--- a/h5py/tests/high/test_threads.py
+++ b/h5py/tests/high/test_threads.py
@@ -13,7 +13,7 @@ class TestThreads(tests.HTest):
 
         def badfunc():
 
-            h5py.h5e.unregister_thread(silent=True)
+            h5py.h5e.unregister_thread(h5py.h5e.NullErrorHandler)
             try:
                 h5py.h5f.is_hdf5('missing')
             except Exception:

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