Skip to content

Commit ede415d

Browse files
committed
fixes a corner case of tuple with len 1 encoding
1 parent ec7f12e commit ede415d

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

nipype/interfaces/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
nipype_version = LooseVersion(__version__)
4848
iflogger = logging.getLogger('interface')
4949

50+
FLOAT_FORMAT = '{:.10f}'.format
5051
PY35 = sys.version_info >= (3, 5)
5152
PY3 = sys.version_info[0] > 2
5253

@@ -619,7 +620,7 @@ def _get_sorteddict(self, theobject, dictwithhash=False, hash_method=None,
619620
else:
620621
out = hash
621622
elif isinstance(theobject, float):
622-
out = '%.10f' % theobject
623+
out = FLOAT_FORMAT(theobject)
623624
else:
624625
out = theobject
625626
return out

nipype/interfaces/tests/test_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,8 @@ def __init__(self, **inputs):
511511
tsthash2 = Registration(from_file=settings)
512512
yield assert_equal, {}, check_dict(data_dict, tsthash2.inputs.get_traitsfree())
513513

514-
hashed_inputs, hashvalue = tsthash.inputs.get_hashval(hash_method='timestamp')
515-
yield assert_equal, hashvalue, '9ab944cbccba61475becb9eac65052af'
514+
_, hashvalue = tsthash.inputs.get_hashval(hash_method='timestamp')
515+
yield assert_equal, 'ec5755e07287e04a4b409e03b77a517c', hashvalue
516516

517517
def test_input_version():
518518
class InputSpec(nib.TraitedSpec):

nipype/utils/filemanip.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,38 @@ def encode_dict(value):
8989
9090
"""
9191
if sys.version_info[0] > 2:
92-
return str(value)
92+
retval = str(value)
93+
else:
94+
retval = encode_dict_py27(value)
95+
return retval
9396

94-
if isinstance(value, str):
95-
value = value.encode()
97+
def encode_dict_py27(value):
98+
"""
99+
Encode dictionary for python 2
100+
"""
96101

97102
istuple = isinstance(value, tuple)
98-
if isinstance(value, list) or istuple:
103+
if isinstance(value, (tuple, list)):
99104
retval = '(' if istuple else '['
105+
nels = len(value)
100106
for i, v in enumerate(value):
101-
if i > 0:
107+
venc = encode_dict_py27(v)
108+
if venc.startswith("u'") or venc.startswith('u"'):
109+
venc = venc[1:]
110+
retval += venc
111+
112+
if i < nels - 1:
102113
retval += ', '
103-
retval += encode_dict(v)
114+
115+
if istuple and nels == 1:
116+
retval += ','
104117
retval += ')' if istuple else ']'
105118
return retval
106119

107-
return repr(value)
120+
retval = repr(value).decode()
121+
if retval.startswith("u'") or retval.startswith('u"'):
122+
retval = retval[1:]
123+
return retval
108124

109125
def fname_presuffix(fname, prefix='', suffix='', newpath=None, use_ext=True):
110126
"""Manipulates path and name of input filename

0 commit comments

Comments
 (0)