Skip to content

Commit

Permalink
[h5dataset] Catch H5Py indexing ValueError
Browse files Browse the repository at this point in the history
H5Py throws ValueError for out-of-bounds indexing. We catch it and throw
IndexError instead, reusing the error message from H5Py.

This also makes indexing more flexible. Slices now work the same as in
numpy, so no slice is ever out-of-bounds. The corresponding test has
been updated accordingly.

This is an updated version of PR G-Node#303, which was a fix for G-Node#299.
  • Loading branch information
achilleas-k committed Jul 12, 2018
1 parent 1202513 commit 0b1c141
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 6 additions & 1 deletion nixio/hdf5/h5dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ def write_data(self, data, sl=None):
def read_data(self, sl=None):
if sl is None:
return self.dataset[:]
return self.dataset[sl]
try:
return self.dataset[sl]
except ValueError as ve:
# h5py throws ValueError for out-of-bounds index
# Let's change it to IndexError
raise IndexError(ve)

def set_attr(self, name, value):
if value is None:
Expand Down
2 changes: 1 addition & 1 deletion nixio/test/test_data_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ def test_outofbounds_indexing(self):
with self.assertRaises(IndexError):
oobtestda[10]
with self.assertRaises(IndexError):
oobtestda[1:4]
oobtestda[-7]

def test_data_array_numpy_indexing(self):
data = np.random.rand(50)
Expand Down

0 comments on commit 0b1c141

Please sign in to comment.