Skip to content

Commit af42a2a

Browse files
committed
Merge branch 'py3-fixes' into main-master
* py3-fixes: (25 commits) RF - refactored 2 to 3 doctest markup with more comprehensive tests RF - guard against syntax errors parsing input for strings DOC+RF - reorganizing comments and tests for python 2 skipping DOC - more py3builder doctest processor docstring BF - clear up remaining test and doctest failures in python 3 NF - added fancy syntax tree string find /replace DOC - mini-language doctest markup in action BF - more doctest 3 fixes BF - merge Stephans py3 port changes BF - add gifti data files to data files; continue to try to fix python3 doc tests NF - add doctest search replace and markup parser RF - add doctest conversion to 2to3 DOC - revert doc for reverted __eq__ to __cmp__ change RF+BF - non-doctests now all pass on py3k RF+BF - gradual cleanup of py3k test errors; still 10 E 15 F to fix RF - updated netcdf file from scipy distribution, adding py3k port stuff BF - further python 3k fixes RF+BF - trying to get strings, bytes, logs working RF - attempting to deal with string vs byte for python 3 RF - removed parametric testing patch, some test cleanup ... Conflicts: nibabel/tests/test_nifti1.py
2 parents 47f4bb7 + ff8238f commit af42a2a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2566
-1552
lines changed

doc/sphinxext/docscrape.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ def __init__(self,docstring):
112112
def __getitem__(self,key):
113113
return self._parsed_data[key]
114114

115-
def __setitem__(self,key,val):
116-
if not self._parsed_data.has_key(key):
115+
def __setitem__(self,key,val)
116+
if not key in self._parsed_data:
117117
warn("Unknown section %s" % key)
118118
else:
119119
self._parsed_data[key] = val
@@ -451,7 +451,7 @@ def __str__(self):
451451
'meth': 'method'}
452452

453453
if self._role:
454-
if not roles.has_key(self._role):
454+
if not self._role in roles:
455455
print "Warning: invalid role %s" % self._role
456456
out += '.. %s:: %s\n \n\n' % (roles.get(self._role,''),
457457
func_name)

nibabel/analyze.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@
141141
array_to_file, array_from_file, can_cast, \
142142
floating_point_types
143143

144-
from .spatialimages import HeaderDataError, HeaderTypeError, \
145-
ImageDataError, SpatialImage
144+
from .spatialimages import (HeaderDataError, HeaderTypeError,
145+
ImageDataError, SpatialImage)
146146

147147
from . import imageglobals as imageglobals
148148
from .fileholders import FileHolderError, copy_file_map
@@ -395,10 +395,10 @@ def from_fileobj(klass, fileobj, endianness=None, check=True):
395395
396396
Examples
397397
--------
398-
>>> import StringIO
398+
>>> from StringIO import StringIO #23dt : BytesIO
399399
>>> hdr = AnalyzeHeader()
400-
>>> fileobj = StringIO.StringIO(hdr.binaryblock)
401-
>>> fileobj.seek(0)
400+
>>> fileobj = StringIO(hdr.binaryblock) #23dt : BytesIO
401+
>>> _ = fileobj.seek(0) # returns 0 in python 3
402402
>>> hdr2 = AnalyzeHeader.from_fileobj(fileobj)
403403
>>> hdr2.binaryblock == hdr.binaryblock
404404
True
@@ -445,8 +445,8 @@ def write_to(self, fileobj):
445445
Examples
446446
--------
447447
>>> hdr = AnalyzeHeader()
448-
>>> import StringIO
449-
>>> str_io = StringIO.StringIO()
448+
>>> from StringIO import StringIO #23dt : BytesIO
449+
>>> str_io = StringIO() #23dt : BytesIO
450450
>>> hdr.write_to(str_io)
451451
>>> hdr.binaryblock == str_io.getvalue()
452452
True
@@ -523,10 +523,6 @@ def __eq__(self, other):
523523
return this_bb == other_bb
524524

525525
def __ne__(self, other):
526-
''' equality between two headers defined by ``header_data``
527-
528-
For examples, see ``__eq__`` method docstring
529-
'''
530526
return not self == other
531527

532528
def raw_data_from_fileobj(self, fileobj):
@@ -616,8 +612,8 @@ def data_to_fileobj(self, data, fileobj):
616612
>>> hdr = AnalyzeHeader()
617613
>>> hdr.set_data_shape((1, 2, 3))
618614
>>> hdr.set_data_dtype(np.float64)
619-
>>> from StringIO import StringIO
620-
>>> str_io = StringIO()
615+
>>> from StringIO import StringIO #23dt : BytesIO
616+
>>> str_io = StringIO() #23dt : BytesIO
621617
>>> data = np.arange(6).reshape(1,2,3)
622618
>>> hdr.data_to_fileobj(data, str_io)
623619
>>> data.astype(np.float64).tostring('F') == str_io.getvalue()
@@ -659,7 +655,7 @@ def __setitem__(self, item, value):
659655
--------
660656
>>> hdr = AnalyzeHeader()
661657
>>> hdr['descrip'] = 'description'
662-
>>> str(hdr['descrip'])
658+
>>> np.asscalar(hdr['descrip']) #23dt next : bytes
663659
'description'
664660
'''
665661
self._header_data[item] = value
@@ -834,15 +830,15 @@ def set_data_dtype(self, datatype):
834830
>>> hdr.set_data_dtype(np.dtype(np.uint8))
835831
>>> hdr.get_data_dtype()
836832
dtype('uint8')
837-
>>> hdr.set_data_dtype('implausible')
833+
>>> hdr.set_data_dtype('implausible') #doctest: +IGNORE_EXCEPTION_DETAIL
838834
Traceback (most recent call last):
839835
...
840836
HeaderDataError: data dtype "implausible" not recognized
841-
>>> hdr.set_data_dtype('none')
837+
>>> hdr.set_data_dtype('none') #doctest: +IGNORE_EXCEPTION_DETAIL
842838
Traceback (most recent call last):
843839
...
844840
HeaderDataError: data dtype "none" known but not supported
845-
>>> hdr.set_data_dtype(np.void)
841+
>>> hdr.set_data_dtype(np.void) #doctest: +IGNORE_EXCEPTION_DETAIL
846842
Traceback (most recent call last):
847843
...
848844
HeaderDataError: data dtype "<type 'numpy.void'>" known but not supported

nibabel/batteryrunners.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,17 @@ def __init__(self,
211211
self.problem_msg = problem_msg
212212
self.fix_msg = fix_msg
213213

214+
def __getstate__(self):
215+
""" State that defines object
216+
217+
Returns
218+
-------
219+
tup : tuple
220+
"""
221+
return self.error, self.problem_level, self.problem_msg, self.fix_msg
222+
214223
def __eq__(self, other):
215-
''' Test for equality
224+
''' are two BatteryRunner-like objects equal?
216225
217226
Parameters
218227
----------
@@ -229,19 +238,14 @@ def __eq__(self, other):
229238
>>> rep == rep3
230239
False
231240
'''
232-
return (self.__dict__ == other.__dict__)
241+
return self.__getstate__() == other.__getstate__()
233242

234243
def __ne__(self, other):
235-
''' Test for equality
236-
237-
Parameters
238-
----------
239-
other : object
240-
report-like object to test equality
244+
""" are two BatteryRunner-like objects not equal?
241245
242-
See __eq__ docstring for examples
243-
'''
244-
return (self.__dict__ != other.__dict__)
246+
See docstring for __eq__
247+
"""
248+
return not self == other
245249

246250
def __str__(self):
247251
''' Printable string for object '''

nibabel/data.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ def make_datasource(pkg_def, **kwargs):
279279
names = unix_relpath.split('/')
280280
try:
281281
pth = find_data_dir(data_path, *names)
282-
except DataError, exception:
282+
except DataError:
283+
exception = sys.exc_info()[1] # Python 2 and 3 compatibility
283284
pth = [pjoin(this_data_path, *names)
284285
for this_data_path in data_path]
285286
pkg_hint = pkg_def.get('install hint', DEFAULT_INSTALL_HINT)
@@ -337,7 +338,8 @@ def datasource_or_bomber(pkg_def, **options):
337338
sys_relpath = os.path.sep.join(names)
338339
try:
339340
ds = make_datasource(pkg_def, **options)
340-
except DataError, exception:
341+
except DataError:
342+
exception = sys.exc_info()[1] # python 2 and 3 compatibility
341343
return Bomber(sys_relpath, exception)
342344
# check version
343345
if (version is None or

nibabel/eulerangles.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@
8484
'''
8585

8686
import math
87+
from functools import reduce
88+
8789
import numpy as np
8890

8991

0 commit comments

Comments
 (0)