Skip to content

Commit d7ea9e1

Browse files
committed
BF - py3k fixes from recent edits
Bytes vs strings Different level of exception detail in doctests Change to default floating point division
1 parent cd69895 commit d7ea9e1

File tree

8 files changed

+26
-25
lines changed

8 files changed

+26
-25
lines changed

nibabel/casting.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def as_int(x, check=True):
213213
2
214214
>>> as_int(-2.0)
215215
-2
216-
>>> as_int(2.1)
216+
>>> as_int(2.1) #doctest: +IGNORE_EXCEPTION_DETAIL
217217
Traceback (most recent call last):
218218
...
219219
FloatingError: Not an integer: 2.1
@@ -359,5 +359,5 @@ def floor_log2(x):
359359
rem = abs(x)
360360
while rem>=2:
361361
ip += 1
362-
rem /= 2
362+
rem //= 2
363363
return ip

nibabel/dft.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
import os
1212
from os.path import join as pjoin
1313
import tempfile
14-
import StringIO
1514
import getpass
1615
import warnings
1716
import sqlite3
1817

1918
import numpy
2019

20+
from .py3k import BytesIO
21+
2122
from .nifti1 import Nifti1Header
2223

2324
# Shield optional dicom import
@@ -128,7 +129,7 @@ def as_png(self, index=None, scale_to_slice=True):
128129
data = data * 255 / (max - min)
129130
data = data.astype(numpy.uint8)
130131
im = PIL.Image.fromstring('L', (self.rows, self.columns), data.tostring())
131-
s = StringIO.StringIO()
132+
s = BytesIO()
132133
im.save(s, 'PNG')
133134
return s.getvalue()
134135

@@ -191,7 +192,7 @@ def as_nifti(self):
191192
hdr.set_data_dtype(numpy.int16)
192193
hdr.set_data_shape((self.columns, self.rows, len(self.storage_instances)))
193194

194-
s = StringIO.StringIO()
195+
s = BytesIO()
195196
hdr.write_to(s)
196197

197198
return s.getvalue() + data.tostring()

nibabel/ecat.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,11 +335,11 @@ def __getitem__(self, item):
335335
Examples
336336
--------
337337
>>> hdr = EcatHeader()
338-
>>> hdr['magic_number'] == 'MATRIX72'
339-
True
338+
>>> hdr['magic_number'] #23dt next : bytes
339+
'MATRIX72'
340340
'''
341341
return self._header_data[item].item()
342-
342+
343343
def __setitem__(self, item, value):
344344
''' Set values in header data
345345

nibabel/optpkg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
else:
88
have_nose = True
99

10-
from .tripwire import TripWire, is_tripwire
10+
from .tripwire import TripWire
1111

1212
def optional_package(name, trip_msg=None):
1313
""" Return package-like thing and module setup for package `name`
@@ -47,7 +47,7 @@ def optional_package(name, trip_msg=None):
4747
4848
and
4949
50-
>>> pkg.some_function()
50+
>>> pkg.some_function() #doctest: +IGNORE_EXCEPTION_DETAIL
5151
Traceback (most recent call last):
5252
...
5353
TripWireError: We need package not_a_package for these functions, but ``import not_a_package`` raised an ImportError

nibabel/tests/test_analyze.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
import os
1616
import re
1717
import logging
18-
from StringIO import StringIO
1918

2019
import numpy as np
2120

22-
from ..py3k import BytesIO, asbytes
21+
from ..py3k import BytesIO, StringIO, asbytes
2322
from ..volumeutils import array_to_file
2423
from ..spatialimages import (HeaderDataError, HeaderTypeError)
2524
from ..analyze import AnalyzeHeader, AnalyzeImage
@@ -449,7 +448,7 @@ def test_scaling():
449448
assert_array_almost_equal(data, rdata)
450449
# Writing to integer datatype does, and raises an error
451450
hdr.set_data_dtype(np.int32)
452-
assert_raises(HeaderTypeError, hdr.data_to_fileobj, data, StringIO())
451+
assert_raises(HeaderTypeError, hdr.data_to_fileobj, data, BytesIO())
453452
# unless we aren't scaling, in which case we convert the floats to
454453
# integers and write
455454
_write_data(hdr, data, S)

nibabel/tests/test_ecat.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
import numpy as np
1212

13+
from ..py3k import asbytes
14+
1315
from ..volumeutils import native_code, swapped_code
1416
from ..ecat import EcatHeader, EcatMlist, EcatSubHeader, EcatImage
1517

@@ -34,20 +36,20 @@ def test_header_size(self):
3436
def test_empty(self):
3537
hdr = self.header_class()
3638
assert_true(len(hdr.binaryblock) == 502)
37-
assert_true(hdr['magic_number'] == 'MATRIX72')
39+
assert_true(hdr['magic_number'] == asbytes('MATRIX72'))
3840
assert_true(hdr['sw_version'] == 74)
3941
assert_true(hdr['num_frames'] == 0)
4042
assert_true(hdr['file_type'] == 0)
4143
assert_true(hdr['ecat_calibration_factor'] == 1.0)
42-
44+
4345
def test_dtype(self):
4446
#dtype not specified in header, only in subheaders
4547
hdr = self.header_class()
4648
assert_raises(NotImplementedError,
4749
hdr.get_data_dtype)
48-
50+
4951
def test_header_codes(self):
50-
fid = open(ecat_file)
52+
fid = open(ecat_file, 'rb')
5153
hdr = self.header_class()
5254
newhdr = hdr.from_fileobj(fid)
5355
fid.close()
@@ -61,7 +63,7 @@ def test_copy(self):
6163
assert_true(hdr == hdr2)
6264
assert_true(not hdr.binaryblock == hdr2._header_data.byteswap().tostring())
6365
assert_true(hdr.keys() == hdr2.keys())
64-
66+
6567
def test_update(self):
6668
hdr = self.header_class()
6769
assert_true(hdr['num_frames'] == 0)
@@ -76,7 +78,7 @@ def test_endianness(self):
7678
swapped_hdr = self.header_class(endianness=swapped_code)
7779
assert_true(swapped_hdr.endianness == swapped_code)
7880
# Example header is big-endian
79-
fid = open(ecat_file)
81+
fid = open(ecat_file, 'rb')
8082
file_hdr = native_hdr.from_fileobj(fid)
8183
fid.close()
8284
assert_true(file_hdr.endianness == '>')

nibabel/tests/test_wrapstruct.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@
2424
_field_recoders -> field_recoders
2525
'''
2626
import logging
27-
from StringIO import StringIO
2827

2928
import numpy as np
3029

3130
from ..wrapstruct import WrapStructError, WrapStruct
3231
from ..batteryrunners import Report
3332

34-
from ..py3k import BytesIO, ZEROB
33+
from ..py3k import BytesIO, StringIO, asbytes, ZEROB
3534
from ..volumeutils import swapped_code, native_code, Recoder
3635
from ..spatialimages import HeaderDataError
3736
from .. import imageglobals
@@ -59,7 +58,7 @@ def guessed_endian(klass, hdr):
5958
def default_structarr(klass, endianness=None):
6059
structarr = super(MyWrapStruct, klass).default_structarr(endianness)
6160
structarr['an_integer'] = 1
62-
structarr['a_str'] = 'a string'
61+
structarr['a_str'] = asbytes('a string')
6362
return structarr
6463

6564
@classmethod
@@ -145,7 +144,7 @@ def test__eq__(self):
145144
def test_to_from_fileobj(self):
146145
# Successful write using write_to
147146
hdr = self.header_class()
148-
str_io = StringIO()
147+
str_io = BytesIO()
149148
hdr.write_to(str_io)
150149
str_io.seek(0)
151150
hdr2 = self.header_class.from_fileobj(str_io)
@@ -347,7 +346,7 @@ def test_empty(self):
347346
# Test contents of default header
348347
hdr = self.header_class()
349348
assert_equal(hdr['an_integer'], 1)
350-
assert_equal(hdr['a_str'], 'a string')
349+
assert_equal(hdr['a_str'], asbytes('a string'))
351350

352351
def test_str(self):
353352
hdr = self.header_class()

nibabel/tripwire.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class TripWire(object):
3535
... import silly_module_name
3636
... except ImportError:
3737
... silly_module_name = TripWire('We do not have silly_module_name')
38-
>>> silly_module_name.do_silly_thing('with silly string')
38+
>>> silly_module_name.do_silly_thing('with silly string') #doctest: +IGNORE_EXCEPTION_DETAIL
3939
Traceback (most recent call last):
4040
...
4141
TripWireError: We do not have silly_module_name

0 commit comments

Comments
 (0)