Skip to content

Commit bf8b6b5

Browse files
committed
BF+TST: test and fix header file position reset
Test reset of file position when reading trackvis header. This revealed that we were using the wrong flag to `seek`.
1 parent 5fc5dd9 commit bf8b6b5

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

nibabel/streamlines/tests/test_trk.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from nibabel.externals.six import BytesIO
88

99
from nibabel.testing import data_path
10-
from nibabel.testing import clear_and_catch_warnings
10+
from nibabel.testing import clear_and_catch_warnings, assert_arr_dict_equal
1111
from nose.tools import assert_equal, assert_raises, assert_true
1212
from numpy.testing import assert_array_equal
1313

@@ -460,3 +460,19 @@ def test_write_scalars_and_properties_name_too_long(self):
460460
def test_str(self):
461461
trk = TrkFile.load(DATA['complex_trk_fname'])
462462
str(trk) # Simply test it's not failing when called.
463+
464+
def test_header_read_restore(self):
465+
# Test that reading a header restores the file position
466+
trk_fname = DATA['simple_trk_fname']
467+
bio = BytesIO()
468+
bio.write(b'Along my very merry way')
469+
hdr_pos = bio.tell()
470+
hdr_from_fname = TrkFile._read_header(trk_fname)
471+
with open(trk_fname, 'rb') as fobj:
472+
bio.write(fobj.read())
473+
bio.seek(hdr_pos)
474+
# Check header is as expected
475+
hdr_from_fname['_offset_data'] += hdr_pos # Correct for start position
476+
assert_arr_dict_equal(TrkFile._read_header(bio), hdr_from_fname)
477+
# Check fileobject file position has not changed
478+
assert_equal(bio.tell(), hdr_pos)

nibabel/streamlines/trk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ def _read_header(fileobj):
606606

607607
# Set the file position where it was, if it was previously open
608608
if start_position is not None:
609-
fileobj.seek(start_position, os.SEEK_CUR)
609+
fileobj.seek(start_position, os.SEEK_SET)
610610

611611
return header
612612

0 commit comments

Comments
 (0)