Skip to content

Commit e272e79

Browse files
committed
BF - clear up remaining test and doctest failures in python 3
1 parent 0476325 commit e272e79

File tree

10 files changed

+33
-31
lines changed

10 files changed

+33
-31
lines changed

nibabel/externals/netcdf.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@
117117
To read the NetCDF file we just created:
118118
119119
>>> f = netcdf_file(fname, 'r')
120-
>>> print f.history
121-
Created for a test
120+
>>> f.history #2to3: next; bytes
121+
'Created for a test'
122122
>>> time = f.variables['time']
123-
>>> print time.units
124-
days since 2008-01-01
123+
>>> time.units #2to3: next; bytes
124+
'days since 2008-01-01'
125125
>>> print time.shape
126126
(10,)
127127
>>> print time[-1]

nibabel/nicom/csareader.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
'''
44
import numpy as np
55

6-
from ..py3k import ZEROB
6+
from ..py3k import ZEROB, asbytes, asstr
77
from .structreader import Unpacker
88

99
# DICOM VR code to Python type
@@ -85,7 +85,7 @@ def read(csa_str):
8585
csa_dict = {'tags': {}}
8686
hdr_id = csa_str[:4]
8787
up_str = Unpacker(csa_str, endian='<')
88-
if hdr_id == 'SV10': # CSA2
88+
if hdr_id == asbytes('SV10'): # CSA2
8989
hdr_type = 2
9090
up_str.ptr = 4 # omit the SV10
9191
csa_dict['unused0'] = up_str.read(4)
@@ -251,4 +251,4 @@ def nt_str(s):
251251
zero_pos = s.find(ZEROB)
252252
if zero_pos == -1:
253253
return s
254-
return s[:zero_pos]
254+
return asstr(s[:zero_pos])

nibabel/nicom/structreader.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ class Unpacker(object):
1414
1515
Examples
1616
--------
17-
>>> a = '1234567890' #2to3: here; replace("'1", "b'1")
17+
>>> a = '1234567890' #2to3: here; bytes
1818
>>> upk = Unpacker(a)
19-
>>> upk.unpack('2s')
19+
>>> upk.unpack('2s') #2to3: next; bytes
2020
('12',)
21-
>>> upk.unpack('2s')
21+
>>> upk.unpack('2s') #2to3: next; bytes
2222
('34',)
2323
>>> upk.ptr
2424
4
25-
>>> upk.read(3)
25+
>>> upk.read(3) #2to3: next; bytes
2626
'567'
2727
>>> upk.ptr
2828
7

nibabel/nicom/tests/test_structreader.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44
import struct
55

6+
from ...py3k import asbytes
67
from ..structreader import Unpacker
78

89
from nose.tools import (assert_true, assert_false, assert_equal, assert_raises)
@@ -11,9 +12,9 @@
1112

1213

1314
def test_unpacker():
14-
s = '1234\x00\x01'
15-
le_int, = struct.unpack('<h', '\x00\x01')
16-
be_int, = struct.unpack('>h', '\x00\x01')
15+
s = asbytes('1234\x00\x01')
16+
le_int, = struct.unpack('<h', asbytes('\x00\x01'))
17+
be_int, = struct.unpack('>h', asbytes('\x00\x01'))
1718
if sys.byteorder == 'little':
1819
native_int = le_int
1920
swapped_int = be_int
@@ -25,12 +26,12 @@ def test_unpacker():
2526
native_code = '>'
2627
swapped_code = '<'
2728
up_str = Unpacker(s, endian='<')
28-
assert_equal(up_str.read(4), '1234')
29+
assert_equal(up_str.read(4), asbytes('1234'))
2930
up_str.ptr = 0
30-
assert_equal(up_str.unpack('4s'), ('1234',))
31+
assert_equal(up_str.unpack('4s'), (asbytes('1234'),))
3132
assert_equal(up_str.unpack('h'), (le_int,))
3233
up_str = Unpacker(s, endian='>')
33-
assert_equal(up_str.unpack('4s'), ('1234',))
34+
assert_equal(up_str.unpack('4s'), (asbytes('1234'),))
3435
assert_equal(up_str.unpack('h'), (be_int,))
3536
# now test conflict of endian
3637
up_str = Unpacker(s, ptr=4, endian='>')
@@ -45,10 +46,10 @@ def test_unpacker():
4546
assert_equal(up_str.unpack('@h'), (native_int,))
4647
# test -1 for read
4748
up_str.ptr = 2
48-
assert_equal(up_str.read(), '34\x00\x01')
49+
assert_equal(up_str.read(), asbytes('34\x00\x01'))
4950
# past end
50-
assert_equal(up_str.read(), '')
51+
assert_equal(up_str.read(), asbytes(''))
5152
# with n_bytes
5253
up_str.ptr = 2
53-
assert_equal(up_str.read(2), '34')
54-
assert_equal(up_str.read(2), '\x00\x01')
54+
assert_equal(up_str.read(2), asbytes('34'))
55+
assert_equal(up_str.read(2), asbytes('\x00\x01'))

nibabel/nifti1.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,8 @@ def get_intent(self, code_repr='label'):
921921
raise TypeError('repr can be "label" or "code"')
922922
n_params = len(recoder.parameters[code])
923923
params = (float(hdr['intent_p%d' % (i+1)]) for i in range(n_params))
924-
return label, tuple(params), np.asscalar(hdr['intent_name'])
924+
name = asstr(np.asscalar(hdr['intent_name']))
925+
return label, tuple(params), name
925926

926927
def set_intent(self, code, params=(), name=''):
927928
''' Set the intent code, parameters and name

nibabel/tests/test_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def test_data_path():
138138
if USER_KEY in env:
139139
del os.environ[USER_KEY]
140140
fake_user_dir = '/user/path'
141-
nibd.get_nipy_system_dir = lambda : ''
141+
nibd.get_nipy_system_dir = lambda : '/unlikely/path'
142142
nibd.get_nipy_user_dir = lambda : fake_user_dir
143143
# now we should only have anything pointed to in the user's dir
144144
old_pth = get_data_path()

nibabel/tests/test_nifti1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,13 +329,13 @@ def test_intents():
329329
ehdr = Nifti1Header()
330330
ehdr.set_intent('t test', (10,), name='some score')
331331
assert_equal(ehdr.get_intent(),
332-
('t test', (10.0,), asbytes('some score')))
332+
('t test', (10.0,), 'some score'))
333333
# invalid intent name
334334
assert_raises(KeyError,
335335
ehdr.set_intent, 'no intention')
336336
# too many parameters
337337
assert_raises(HeaderDataError,
338-
ehdr.set_intent,
338+
ehdr.set_intent,
339339
't test', (10,10))
340340
# too few parameters
341341
assert_raises(HeaderDataError,

nibabel/tmpdirs.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717
class TemporaryDirectory(object):
1818
"""Create and return a temporary directory. This has the same
1919
behavior as mkdtemp but can be used as a context manager.
20-
20+
2121
Upon exiting the context, the directory and everthing contained
2222
in it are removed.
23-
23+
2424
Examples
2525
--------
2626
>>> import os
2727
>>> with TemporaryDirectory() as tmpdir:
2828
... fname = os.path.join(tmpdir, 'example_file.txt')
2929
... with open(fname, 'wt') as fobj:
30-
... fobj.write('a string\\n')
30+
... _ = fobj.write('a string\\n')
3131
>>> os.path.exists(tmpdir)
3232
False
3333
"""
@@ -56,7 +56,7 @@ class InTemporaryDirectory(TemporaryDirectory):
5656
>>> import os
5757
>>> my_cwd = os.getcwd()
5858
>>> with InTemporaryDirectory() as tmpdir:
59-
... open('test.txt', 'wt').write('some text')
59+
... _ = open('test.txt', 'wt').write('some text')
6060
... assert os.path.isfile('test.txt')
6161
... assert os.path.isfile(os.path.join(tmpdir, 'test.txt'))
6262
>>> os.path.exists(tmpdir)

nibabel/trackvis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ def empty_header(endianness=None, version=2):
387387
>>> hdr = empty_header()
388388
>>> print hdr['version']
389389
2
390-
>>> np.asscalar(hdr['id_string']) #2to3: next; line.replace("'T", "b'T")
390+
>>> np.asscalar(hdr['id_string']) #2to3: next; bytes
391391
'TRACK'
392392
>>> endian_codes[hdr['version'].dtype.byteorder] == native_code
393393
True

nibabel/volumeutils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ def array_from_file(shape, in_dtype, infile, offset=0, order='F'):
395395
>>> np.all(arr == arr2)
396396
True
397397
>>> bio = BytesIO()
398-
>>> _ = bio.write(' ' * 10) #2to3: here; replace("' '", "b' '")
398+
>>> _ = bio.write(' ' * 10) #2to3: here; bytes
399399
>>> _ = bio.write(arr.tostring('F'))
400400
>>> arr2 = array_from_file((1,2,3), arr.dtype, bio, 10)
401401
>>> np.all(arr == arr2)

0 commit comments

Comments
 (0)