Skip to content

Commit ee1a5fc

Browse files
Merge pull request #607 from kaczmarj/kaczmarj-bf
MRG: preserve error output if not valid klass in `nibabel.save()` This PR adds a fix for an `UnboundLocalError` in `nibabel.save`
2 parents 1140e18 + f90750f commit ee1a5fc

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

nibabel/loadsave.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ def save(img, filename):
133133
converted = klass.from_image(img)
134134
break
135135
except Exception as e:
136-
continue
136+
err = e
137137
# ... and if none of them work, raise an error.
138138
if converted is None:
139-
raise e
139+
raise err
140140

141141
# Here, we either have a klass or a converted image.
142142
if converted is None:

nibabel/tests/test_image_load_save.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from ..spatialimages import SpatialImage
3131

3232
from numpy.testing import assert_array_equal, assert_array_almost_equal
33-
from nose.tools import assert_true, assert_equal
33+
from nose.tools import assert_true, assert_equal, assert_raises
3434

3535
_, have_scipy, _ = optional_package('scipy') # No scipy=>no SPM-format writing
3636
DATA_PATH = pjoin(dirname(__file__), 'data')
@@ -322,3 +322,14 @@ def test_guessed_image_type():
322322
assert_equal(nils.guessed_image_type(
323323
pjoin(DATA_PATH, 'analyze.hdr')),
324324
Spm2AnalyzeImage)
325+
326+
327+
def test_fail_save():
328+
with InTemporaryDirectory():
329+
dataobj = np.ones((10, 10, 10), dtype=np.float16)
330+
affine = np.eye(4, dtype=np.float32)
331+
img = SpatialImage(dataobj, affine)
332+
# Fails because float16 is not supported.
333+
with assert_raises(AttributeError):
334+
nils.save(img, 'foo.nii.gz')
335+
del img

0 commit comments

Comments
 (0)