Skip to content

Commit dcd5fa3

Browse files
committed
Merge pull request #140 from matthew-brett/py3-nipy-fixes
Py3 nipy fixes. A couple of fixes to allow nibabel to work with Python 3 nipy.
2 parents 1e171a3 + 2ac2d74 commit dcd5fa3

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

nibabel/data.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,16 @@
1818
'path to the package correctly?')
1919

2020

21-
class DataError(OSError):
21+
class DataError(Exception):
22+
pass
23+
24+
25+
class BomberError(DataError, AttributeError):
26+
""" Error when trying to access Bomber instance
27+
28+
Should be instance of AttributeError to allow Python 3 inspect to do various
29+
``hasattr`` checks without raising an error
30+
"""
2231
pass
2332

2433

@@ -302,7 +311,7 @@ def __init__(self, name, msg):
302311

303312
def __getattr__(self, attr_name):
304313
''' Raise informative error accessing not-found attributes '''
305-
raise DataError(
314+
raise BomberError(
306315
'Trying to access attribute "%s" '
307316
'of non-existent data "%s"\n\n%s\n' %
308317
(attr_name, self.name, self.msg))

nibabel/nifti1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1779,7 +1779,7 @@ def _write_header(self, header_file, header, slope, inter):
17791779
offset = header.get_data_offset()
17801780
diff = offset-header_file.tell()
17811781
if diff > 0:
1782-
header_file.write('\x00' * diff)
1782+
header_file.write(ZEROB * diff)
17831783

17841784
def update_header(self):
17851785
''' Harmonize header with image data and affine '''

nibabel/tests/test_data.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919

2020
from nose import with_setup
2121

22-
from nose.tools import assert_equal, \
23-
assert_raises, raises
22+
from nose.tools import (assert_equal, assert_raises, raises, assert_false)
2423

2524
from .test_environment import (setup_environment,
2625
teardown_environment,
@@ -241,6 +240,11 @@ def test_bomber():
241240
res = b.any_attribute
242241

243242

243+
def test_bomber_inspect():
244+
b = Bomber('bomber example', 'a message')
245+
assert_false(hasattr(b, 'any_attribute'))
246+
247+
244248
@with_environment
245249
def test_datasource_or_bomber():
246250
pkg_def = dict(

nibabel/tests/test_nifti1.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,15 @@ def test_set_sform(self):
469469
# Unexpected keyword raises error
470470
assert_raises(TypeError, img.get_sform, strange=True)
471471

472+
def test_hdr_diff(self):
473+
# Check an offset beyond data does not raise an error
474+
img = self.image_class(np.zeros((2,3,4)), np.eye(4))
475+
ext = dict(img.files_types)['image']
476+
hdr = img.get_header()
477+
hdr['vox_offset'] = 400
478+
with InTemporaryDirectory():
479+
img.to_filename('another_file' + ext)
480+
472481

473482
class TestNifti1Pair(TestNifti1Image):
474483
# Run analyze-flavor spatialimage tests

0 commit comments

Comments
 (0)