Skip to content

Commit

Permalink
Translate out-of-bounds error from HDF5 to IndexError
Browse files Browse the repository at this point in the history
  • Loading branch information
takluyver committed Jun 16, 2022
1 parent a3d7ae4 commit d266559
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions h5py/_errors.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ _exact_table = {
(H5E_FILE, H5E_CANTCONVERT): ValueError, # Invalid file format
}

# Further fudge: In some cases, the error numbers we care about are those from
# the bottom (innermost) error in the stack.
_bottom_exact_table = {
(H5E_DATASPACE, H5E_BADRANGE): IndexError, # Out of range selection
}

IF HDF5_VERSION > (1, 12, 0):
_exact_table[(H5E_DATASET, H5E_CANTCREATE)] = ValueError # bad param for dataset setup

Expand Down Expand Up @@ -140,6 +146,8 @@ cdef int set_exception() except -1:
if H5Ewalk(<hid_t>H5E_DEFAULT, H5E_WALK_DOWNWARD, walk_cb, &err) < 0:
raise RuntimeError("Failed to walk error stack")

eclass = _bottom_exact_table.get((err.err.maj_num, err.err.min_num), eclass)

desc_bottom = err.err.desc
if desc_bottom is NULL:
raise RuntimeError("Failed to extract bottom-level error description")
Expand Down
11 changes: 11 additions & 0 deletions h5py/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,11 @@ def test_read_points(writable_file):
res = ds.points[[(0, 0), (1, 0), (2, 2)]]
np.testing.assert_array_equal(res, [0, 6, 14])

with pytest.raises(IndexError):
ds.points[[(0, 7)]]

with pytest.raises(IndexError):
ds.points[[(-1, 0)]]

def test_write_points(writable_file):
ds = writable_file.create_dataset('a', shape=(3, 3))
Expand All @@ -1764,6 +1769,12 @@ def test_write_points(writable_file):
[0, 0, 3],
])

with pytest.raises(IndexError):
ds.points[[(0, 4)]] = [4]

with pytest.raises(IndexError):
ds.points[[(-1, 0)]] = [5]


class TestCommutative(BaseDataset):
"""
Expand Down

0 comments on commit d266559

Please sign in to comment.