Skip to content

Commit 8b5ac2e

Browse files
author
Ben Cipollini
committed
FIX: Take care of byte vs. string / BytesIO vs. StringIO encodings for Python 3.
1 parent 060dcec commit 8b5ac2e

File tree

4 files changed

+12
-13
lines changed

4 files changed

+12
-13
lines changed

nibabel/cifti2/cifti2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from .. import xmlutils as xml
2525
from ..externals import inflection
2626
from ..externals.six import string_types
27-
from ..filebasedimages import FileBasedImage, FileBasedHeader
27+
from ..filebasedimages import FileBasedHeader, FileBasedImage
2828
from ..nifti2 import Nifti2Image
2929

3030

nibabel/cifti2/parse_cifti2_fast.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -413,43 +413,43 @@ def flush_chardata(self):
413413
self._char_blocks = None
414414
# Process data
415415
if self.write_to == 'Name':
416-
data = data.strip()
416+
data = data.strip() # .decode('utf-8')
417417
pair = self.struct_state[-1]
418418
pair[0] = data
419419
elif self.write_to == 'Value':
420-
data = data.strip()
420+
data = data.strip() # .decode('utf-8')
421421
pair = self.struct_state[-1]
422422
pair[1] = data
423423
elif self.write_to == 'Vertices':
424424
# conversion to numpy array
425-
c = BytesIO(data.strip().encode())
425+
c = BytesIO(data.strip().encode('utf-8'))
426426
vertices = self.struct_state[-1]
427427
vertices.vertices = np.genfromtxt(c, dtype=np.int)
428428
c.close()
429429
elif self.write_to == 'VoxelIndices':
430430
# conversion to numpy array
431-
c = BytesIO(data.strip().encode())
431+
c = BytesIO(data.strip().encode('utf-8'))
432432
parent = self.struct_state[-1]
433433
parent.voxel_indices_ijk.indices = np.genfromtxt(c, dtype=np.int)
434434
c.close()
435435
elif self.write_to == 'VertexIndices':
436436
# conversion to numpy array
437-
c = BytesIO(data.strip().encode())
437+
c = BytesIO(data.strip().encode('utf-8'))
438438
index = self.struct_state[-1]
439439
index.indices = np.genfromtxt(c, dtype=np.int)
440440
c.close()
441441
elif self.write_to == 'TransformMatrix':
442442
# conversion to numpy array
443-
c = BytesIO(data.strip().encode())
443+
c = BytesIO(data.strip().encode('utf-8'))
444444
transform = self.struct_state[-1]
445445
transform.matrix = np.genfromtxt(c, dtype=np.float)
446446
c.close()
447447
elif self.write_to == 'Label':
448448
label = self.struct_state[-1]
449-
label.label = data.strip()
449+
label.label = data.strip().encode('utf-8')
450450
elif self.write_to == 'MapName':
451451
named_map = self.struct_state[-1]
452-
named_map.map_name = data.strip()
452+
named_map.map_name = data.strip() # .decode('utf-8')
453453

454454
@property
455455
def pending_data(self):

nibabel/cifti2/tests/test_cifti2.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@ def test_cifti2_metadata():
3434
assert_equal(len(md.data), 1)
3535

3636
assert_raises(ValueError, md.remove_metadata, {'a': 'aval', 'd': 'dval'})
37-
38-
assert_equal(md.to_xml(),
37+
assert_equal(md.to_xml().decode('utf-8'),
3938
'<MetaData><MD><Name>b</Name><Value>bval</Value></MD></MetaData>')
4039

4140
md.remove_metadata(['b', 'bval'])
4241
assert_equal(len(md.data), 0)
43-
assert_equal(md.to_xml(), '<MetaData />')
42+
assert_equal(md.to_xml().decode('utf-8'), '<MetaData />')

nibabel/xmlutils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class XmlParser(object):
5050
'EndElementHandler',
5151
'CharacterDataHandler']
5252

53-
def __init__(self, encoding=None, buffer_size=35000000, verbose=0):
53+
def __init__(self, encoding='utf-8', buffer_size=35000000, verbose=0):
5454
"""
5555
Parameters
5656
----------

0 commit comments

Comments
 (0)