Skip to content

Commit f767973

Browse files
committed
fix: process exceptions when trying to open X5
1 parent 8010fa0 commit f767973

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

nitransforms/io/x5.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,21 @@ def to_filename(fname: str | Path, x5_list: List[X5Transform]):
140140

141141
def from_filename(fname: str | Path) -> List[X5Transform]:
142142
"""Read a list of :class:`X5Transform` objects from an X5 HDF5 file."""
143-
with h5py.File(str(fname), "r") as in_file:
144-
if in_file.attrs.get("Format") != "X5":
145-
raise ValueError("Input file is not in X5 format")
146-
147-
tg = in_file["TransformGroup"]
148-
return [
149-
_read_x5_group(node)
150-
for _, node in sorted(tg.items(), key=lambda kv: int(kv[0]))
151-
]
152-
143+
try:
144+
with h5py.File(str(fname), "r") as in_file:
145+
if in_file.attrs.get("Format") != "X5":
146+
raise TypeError("Input file is not in X5 format")
147+
148+
tg = in_file["TransformGroup"]
149+
return [
150+
_read_x5_group(node)
151+
for _, node in sorted(tg.items(), key=lambda kv: int(kv[0]))
152+
]
153+
except OSError as err:
154+
if "file signature not found" in err.args[0]:
155+
raise TypeError(f"Input file is not HDF5.")
156+
157+
raise
153158

154159
def _read_x5_group(node) -> X5Transform:
155160
x5 = X5Transform(

nitransforms/tests/test_x5.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,5 @@ def test_from_filename_invalid(tmp_path):
7373
with H5File(fname, "w") as f:
7474
f.attrs["Format"] = "NOTX5"
7575

76-
with pytest.raises(ValueError):
76+
with pytest.raises(TypeError):
7777
from_filename(fname)

0 commit comments

Comments
 (0)