[h5py] 241/455: Improve Python exception mapping
Ghislain Vaillant
ghisvail-guest at moszumanska.debian.org
Thu Jul 2 18:19:37 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 b2a4e542494d9c61f948e9383ebdadd37cf77ef4
Author: andrewcollette <andrew.collette at gmail.com>
Date: Thu Apr 30 17:38:25 2009 +0000
Improve Python exception mapping
---
h5py/h5.pyx | 2 --
h5py/h5e.pxd | 6 ++---
h5py/h5e.pyx | 20 ++++++++++++---
h5py/tests/test_highlevel.py | 60 ++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 78 insertions(+), 10 deletions(-)
diff --git a/h5py/h5.pyx b/h5py/h5.pyx
index d812da5..693353e 100644
--- a/h5py/h5.pyx
+++ b/h5py/h5.pyx
@@ -375,8 +375,6 @@ def _open():
H5open()
-# === Public exception hierarchy ==============================================
-
# === Library init ============================================================
def _exithack():
diff --git a/h5py/h5e.pxd b/h5py/h5e.pxd
index 2cedcdf..d5adea0 100644
--- a/h5py/h5e.pxd
+++ b/h5py/h5e.pxd
@@ -6,10 +6,8 @@
include "config.pxi"
include "defs.pxd"
-# === H5E - Error handling API ================================================
-
-cdef herr_t err_callback(void* client_data) with gil
-
+# Register the current thread for native Python exception support.
+# Safe to call more than once.
cpdef int register_thread() except -1
cdef extern from "hdf5.h":
diff --git a/h5py/h5e.pyx b/h5py/h5e.pyx
index 0904418..bef7a8d 100644
--- a/h5py/h5e.pyx
+++ b/h5py/h5e.pyx
@@ -6,11 +6,14 @@
include "config.pxi"
-
from python_exc cimport PyErr_SetString
from h5 cimport SmartStruct
+
import _stub
+
+# === Exception hierarchy based on major error codes ==========================
+
class H5Error(Exception):
""" Base class for internal HDF5 library exceptions.
"""
@@ -205,7 +208,7 @@ cdef dict _minor_table = {
H5E_UNSUPPORTED: NotImplementedError, # Feature is unsupported
H5E_NOTFOUND: KeyError, # Object not found
- H5E_CANTINSERT: TypeError, # Unable to insert object
+ H5E_CANTINSERT: ValueError, # Unable to insert object
H5E_BADTYPE: TypeError, # Inappropriate type
H5E_BADRANGE: ValueError, # Out of range
@@ -224,8 +227,8 @@ cdef dict _exact_table = {
(H5E_INTERNAL, H5E_SYSERRSTR): IOError, # e.g. wrong file permissions
}
-# === Error stack inspection ==================================================
+# === Error stack inspection ==================================================
cdef class ErrorStackElement(SmartStruct):
@@ -263,6 +266,12 @@ def get_minor(int code):
_verbose = False
def verbose(bint v):
+ """ (BOOL verbose)
+
+ If FALSE (default), exception messages are a single line. If TRUE,
+ an HDF5 stack trace is attached.
+ """
+
global _verbose
_verbose = bool(v)
@@ -348,7 +357,10 @@ cdef herr_t err_callback(void* client_data) with gil:
return 1
cpdef int register_thread() except -1:
- """ Register the current thread for native HDF5 exception support.
+ """ ()
+
+ Register the current thread for native HDF5 exception support. This is
+ automatically called by h5py on startup. Safe to call more than once.
"""
if H5Eset_auto(err_callback, NULL) < 0:
raise RuntimeError("Failed to register HDF5 exception callback")
diff --git a/h5py/tests/test_highlevel.py b/h5py/tests/test_highlevel.py
index 0c92a51..4d02ee9 100644
--- a/h5py/tests/test_highlevel.py
+++ b/h5py/tests/test_highlevel.py
@@ -336,6 +336,66 @@ class TestDataset(HDF5TestCase):
self.assertRaises(H5Error, dsid.id.read, h5s.ALL, h5s.ALL, arr)
# or it'll segfault...
+class TestExceptions(HDF5TestCase):
+
+ def setUp(self):
+
+ self.fname = tempfile.mktemp('.hdf5')
+ self.f = File(self.fname, 'w')
+
+ def tearDown(self):
+ try:
+ self.f.close()
+ except Exception:
+ pass
+ try:
+ os.unlink(self.fname)
+ except Exception:
+ pass
+
+ def test_groups(self):
+
+ self.assertRaises(KeyError, self.f.__getitem__, "foobar")
+ self.f.create_group("foobar")
+ self.assertRaises(ValueError, self.f.create_group, "foobar")
+
+ def test_files(self):
+
+ self.f.create_group("foobar")
+ self.f.close()
+
+ valerrors = {"__getitem__": ("foobar",),
+ "create_group": ("foobar",),
+ "create_dataset": ("foobar", (), 'i'),}
+
+ for meth, args in valerrors.iteritems():
+ print " Testing %s" % meth
+ self.assertRaises(ValueError, getattr(self.f, meth), *args)
+
+ def test_attributes(self):
+
+ g = self.f.create_group("foobar")
+ self.assertRaises(KeyError, g.attrs.__getitem__, "attr")
+
+ def test_mode(self):
+
+ fname = tempfile.mktemp('hdf5')
+ try:
+ f = File(fname,'w')
+ g = f.create_group("foobar")
+ g.attrs["attr"] = 42
+ f.close()
+
+ f = File(fname, 'r')
+ g = f["foobar"]
+ self.assertRaises(IOError, g.attrs.__setitem__, "attr", 43)
+ f.close()
+ finally:
+ try:
+ f.close()
+ except Exception:
+ pass
+ os.unlink(fname)
class TestGroup(HDF5TestCase):
--
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