Skip to content

Commit d6a11e6

Browse files
committed
RF+BF - gradual cleanup of py3k test errors; still 10 E 15 F to fix
1 parent 7ceaab4 commit d6a11e6

10 files changed

+100
-79
lines changed

nibabel/nifti1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ def write_to(self, fileobj, byteswap):
308308
fileobj.write(self._mangle(self._content))
309309
# be nice and zero out remaining part of the extension till the
310310
# next 16 byte border
311-
fileobj.write('\x00' * (extstart + rawsize - fileobj.tell()))
311+
fileobj.write(ZEROB * (extstart + rawsize - fileobj.tell()))
312312

313313

314314
# NIfTI header extension type codes (ECODE)
@@ -428,7 +428,7 @@ def from_fileobj(klass, fileobj, size, byteswap):
428428
raise HeaderDataError(
429429
'extension size is not a multiple of 16 bytes')
430430
# read extension itself; esize includes the 8 bytes already read
431-
evalue = fileobj.read(esize - 8)
431+
evalue = fileobj.read(int(esize - 8))
432432
if not len(evalue) == esize - 8:
433433
raise HeaderDataError('failed to read extension content')
434434
# note that we read a full extension

nibabel/tests/test_batteryrunners.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
1111
'''
1212

13-
from StringIO import StringIO
13+
from ..py3k import StringIO
1414

1515
import logging
1616

@@ -92,7 +92,7 @@ def test_init_basic():
9292
def test_init_report():
9393
rep = Report()
9494
assert_equal(rep, Report(Exception, 0, '', ''))
95-
95+
9696

9797
def test_report_strings():
9898
rep = Report()
@@ -107,21 +107,21 @@ def test_report_strings():
107107
rep.problem_level = 30
108108
rep.write_raise(str_io)
109109
assert_equal(str_io.getvalue(), 'Level 30: msg; fix\n')
110-
str_io.truncate(0)
110+
str_io.truncate(0); str_io.seek(0)
111111
# No fix string, no fix message
112112
rep.fix_msg = ''
113113
rep.write_raise(str_io)
114114
assert_equal(str_io.getvalue(), 'Level 30: msg\n')
115115
rep.fix_msg = 'fix'
116-
str_io.truncate(0)
116+
str_io.truncate(0); str_io.seek(0)
117117
# If we drop the level, nothing goes to the log
118118
rep.problem_level = 20
119119
rep.write_raise(str_io)
120120
assert_equal(str_io.getvalue(), '')
121121
# Unless we set the default log level in the call
122122
rep.write_raise(str_io, log_level=20)
123123
assert_equal(str_io.getvalue(), 'Level 20: msg; fix\n')
124-
str_io.truncate(0)
124+
str_io.truncate(0); str_io.seek(0)
125125
# If we set the error level down this low, we raise an error
126126
assert_raises(ValueError, rep.write_raise, str_io, 20)
127127
# But the log level wasn't low enough to do a log entry
@@ -131,7 +131,7 @@ def test_report_strings():
131131
assert_raises(ValueError, rep.write_raise, str_io, 20, 20)
132132
assert_equal(str_io.getvalue(), 'Level 20: msg; fix\n')
133133
# If there's no error, we can't raise
134-
str_io.truncate(0)
134+
str_io.truncate(0); str_io.seek(0)
135135
rep.error = None
136136
rep.write_raise(str_io, 20)
137137
assert_equal(str_io.getvalue(), '')
@@ -148,7 +148,7 @@ def test_logging():
148148
rep.problem_level = 30
149149
rep.log_raise(logger)
150150
assert_equal(str_io.getvalue(), 'msg; fix\n')
151-
str_io.truncate(0)
151+
str_io.truncate(0); str_io.seek(0)
152152

153153

154154
def test_checks():

nibabel/tests/test_binary.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,10 @@ def test_general_init(self):
8484

8585
def test_mappingness(self):
8686
hdr = self.header_class()
87-
if sys.version_info[0] >= 3:
88-
print('Skipping due to numpy python 3 bug')
89-
else:
90-
assert_raises(ValueError,
91-
hdr.__setitem__,
92-
'nonexistent key',
93-
0.1)
87+
assert_raises(ValueError,
88+
hdr.__setitem__,
89+
'nonexistent key',
90+
0.1)
9491
hdr_dt = hdr.structarr.dtype
9592
keys = hdr.keys()
9693
assert_equal(keys, list(hdr))

nibabel/tests/test_image_load_save.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from os.path import join as pjoin
1212
import shutil
1313
from tempfile import mkdtemp
14-
from StringIO import StringIO
14+
from ..py3k import BytesIO
1515

1616
import numpy as np
1717

@@ -42,7 +42,7 @@
4242

4343
def round_trip(img):
4444
# round trip a nifti single
45-
sio = StringIO()
45+
sio = BytesIO()
4646
img.file_map['image'].fileobj = sio
4747
img.to_file_map()
4848
img2 = Nifti1Image.from_file_map(img.file_map)
@@ -157,16 +157,16 @@ def test_two_to_one():
157157
# single file format
158158
img = ni1.Nifti1Image(data, affine)
159159
assert_equal(img.get_header()['magic'], 'n+1')
160-
str_io = StringIO()
160+
str_io = BytesIO()
161161
img.file_map['image'].fileobj = str_io
162162
# check that the single format vox offset is set correctly
163163
img.to_file_map()
164164
assert_equal(img.get_header()['magic'], 'n+1')
165165
assert_equal(img.get_header()['vox_offset'], 352)
166166
# make a new pair image, with the single image header
167167
pimg = ni1.Nifti1Pair(data, affine, img.get_header())
168-
isio = StringIO()
169-
hsio = StringIO()
168+
isio = BytesIO()
169+
hsio = BytesIO()
170170
pimg.file_map['image'].fileobj = isio
171171
pimg.file_map['header'].fileobj = hsio
172172
pimg.to_file_map()
@@ -178,7 +178,7 @@ def test_two_to_one():
178178
ana_img = ana.AnalyzeImage.from_image(img)
179179
assert_equal(ana_img.get_header()['vox_offset'], 352)
180180
# back to the single image, save it again to a stringio
181-
str_io = StringIO()
181+
str_io = BytesIO()
182182
img.file_map['image'].fileobj = str_io
183183
img.to_file_map()
184184
assert_equal(img.get_header()['vox_offset'], 352)
@@ -206,7 +206,7 @@ def test_negative_load_save():
206206
hdr = ni1.Nifti1Header()
207207
hdr.set_data_dtype(np.int16)
208208
img = Nifti1Image(data, affine, hdr)
209-
str_io = StringIO()
209+
str_io = BytesIO()
210210
img.file_map['image'].fileobj = str_io
211211
img.to_file_map()
212212
str_io.seek(0)

nibabel/tests/test_nifti1.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from __future__ import with_statement
1111
import os
1212

13-
from StringIO import StringIO
13+
from ..py3k import BytesIO, ZEROB
1414

1515
import numpy as np
1616

@@ -56,7 +56,7 @@ def test_empty(self):
5656
assert_equal(hdr['vox_offset'], 0)
5757

5858
def test_from_eg_file(self):
59-
hdr = Nifti1Header.from_fileobj(open(self.example_file))
59+
hdr = Nifti1Header.from_fileobj(open(self.example_file, 'rb'))
6060
assert_equal(hdr.endianness, '<')
6161
assert_equal(hdr['magic'], 'ni1')
6262
assert_equal(hdr['sizeof_hdr'], 348)
@@ -169,9 +169,9 @@ def test_binblock_is_file(self):
169169
# the case of the single file version of the header, we need to append
170170
# the extension string (4 0s)
171171
hdr = self.header_class()
172-
str_io = StringIO()
172+
str_io = BytesIO()
173173
hdr.write_to(str_io)
174-
assert_equal(str_io.getvalue(), hdr.binaryblock + '\x00' * 4)
174+
assert_equal(str_io.getvalue(), hdr.binaryblock + ZEROB * 4)
175175

176176

177177
class TestNifti1Image(tana.TestAnalyzeImage):
@@ -407,7 +407,7 @@ def test_nifti1_images():
407407
img = Nifti1Image(data, affine)
408408
assert_equal(img.get_shape(), shape)
409409
img.set_data_dtype(npt)
410-
stio = StringIO()
410+
stio = BytesIO()
411411
img.file_map['image'].fileobj = stio
412412
img.to_file_map()
413413
img2 = Nifti1Image.from_file_map(img.file_map)
@@ -491,7 +491,7 @@ def test_loadsave_cycle():
491491
exts_container = hdr.extensions
492492
assert_true(len(exts_container) > 0)
493493
# write into the air ;-)
494-
stio = StringIO()
494+
stio = BytesIO()
495495
nim.file_map['image'].fileobj = stio
496496
nim.to_file_map()
497497
stio.seek(0)
@@ -516,7 +516,7 @@ def test_loadsave_cycle():
516516
assert_equal(wnim.get_data_dtype(), np.int16)
517517
assert_equal(wnim.get_header().get_slope_inter(), (2, 8))
518518
# write into the air again ;-)
519-
stio = StringIO()
519+
stio = BytesIO()
520520
wnim.file_map['image'].fileobj = stio
521521
wnim.to_file_map()
522522
stio.seek(0)
@@ -605,7 +605,7 @@ def test_load_pixdims():
605605
assert_array_equal(img_hdr.get_zooms(), [2,3,4])
606606
# Save to stringio
607607
fm = Nifti1Image.make_file_map()
608-
fm['image'].fileobj = StringIO()
608+
fm['image'].fileobj = BytesIO()
609609
simg.to_file_map(fm)
610610
# Load again
611611
re_simg = Nifti1Image.from_file_map(fm)

nibabel/tests/test_orientations.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,20 @@ def same_transform(taff, ornt, shape):
107107

108108
def test_apply():
109109
# most tests are in ``same_transform`` above, via the
110-
# test_io_orientations.
110+
# test_io_orientations
111111
a = np.arange(24).reshape((2,3,4))
112-
# Test 4D
112+
# Test 4D with an example orientation
113+
ornt = OUT_ORNTS[-1]
113114
t_arr = apply_orientation(a[:,:,:,None], ornt)
114115
assert_equal(t_arr.ndim, 4)
115116
# Orientation errors
116117
assert_raises(OrientationError,
117-
apply_orientation,
118-
a[:,:,1], ornt)
118+
apply_orientation,
119+
a[:,:,1], ornt)
119120
assert_raises(OrientationError,
120-
apply_orientation,
121-
a,
122-
[[0,1],[np.nan,np.nan],[2,1]])
121+
apply_orientation,
122+
a,
123+
[[0,1],[np.nan,np.nan],[2,1]])
123124

124125

125126
def test_flip_axis():

nibabel/tests/test_spm99analyze.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
# copyright and license terms.
77
#
88
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
9-
from StringIO import StringIO
109

1110
import numpy as np
1211

12+
from ..py3k import BytesIO
13+
1314
from numpy.testing import assert_array_equal, assert_array_almost_equal, dec
1415

1516
# Decorator to skip tests requiring save / load if scipy not available for mat
@@ -43,7 +44,7 @@ def test_scaling(self):
4344
hdr = self.header_class()
4445
hdr.set_data_shape((1,2,3))
4546
hdr.set_data_dtype(np.int16)
46-
S3 = StringIO()
47+
S3 = BytesIO()
4748
data = np.arange(6, dtype=np.float64).reshape((1,2,3))
4849
# This uses scaling
4950
hdr.data_to_fileobj(data, S3)

nibabel/tests/test_trackvis.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
''' Testing trackvis module '''
22

3-
from StringIO import StringIO
4-
53
import numpy as np
64

5+
from ..py3k import BytesIO
76
from .. import trackvis as tv
87
from ..volumeutils import swapped_code
98

@@ -14,7 +13,7 @@
1413

1514
def test_write():
1615
streams = []
17-
out_f = StringIO()
16+
out_f = BytesIO()
1817
tv.write(out_f, [], {})
1918
assert_equal(out_f.getvalue(), tv.empty_header().tostring())
2019
out_f.truncate(0)
@@ -48,7 +47,7 @@ def test_write_scalars_props():
4847
scalars = np.arange(N*M).reshape((N,M)) + 100
4948
props = np.arange(P) + 1000
5049
# If scalars not same size for each point, error
51-
out_f = StringIO()
50+
out_f = BytesIO()
5251
streams = [(points, None, None),
5352
(points, scalars, None)]
5453
assert_raises(tv.DataError, tv.write, out_f, streams)
@@ -109,7 +108,7 @@ def streamlist_equal(streamlist1, streamlist2):
109108

110109

111110
def test_round_trip():
112-
out_f = StringIO()
111+
out_f = BytesIO()
113112
xyz0 = np.tile(np.arange(5).reshape(5,1), (1, 3))
114113
xyz1 = np.tile(np.arange(5).reshape(5,1) + 10, (1, 3))
115114
streams = [(xyz0, None, None), (xyz1, None, None)]
@@ -135,7 +134,7 @@ def test_round_trip():
135134
streams3, hdr = tv.read(out_f, as_generator=True)
136135
# Now we need a new file object, because we're still using the old one for
137136
# our generator
138-
out_f_write = StringIO()
137+
out_f_write = BytesIO()
139138
tv.write(out_f_write, streams3, {})
140139
# and re-read just to check
141140
out_f_write.seek(0)
@@ -217,7 +216,7 @@ def test_tv_class():
217216
assert_true(isinstance(tvf.header, np.ndarray))
218217
assert_equal(tvf.endianness, tv.native_code)
219218
assert_equal(tvf.filename, None)
220-
out_f = StringIO()
219+
out_f = BytesIO()
221220
tvf.to_file(out_f)
222221
assert_equal(out_f.getvalue(), tv.empty_header().tostring())
223222
out_f.truncate(0)

0 commit comments

Comments
 (0)