Skip to content

Commit b66c850

Browse files
committed
fix: unicode/str accounting on py3/2
1 parent 60b36a2 commit b66c850

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

nipype/utils/provenance.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from future import standard_library
22
standard_library.install_aliases()
3-
from builtins import object
3+
from builtins import object, str
44

55
from copy import deepcopy
66
from pickle import dumps
@@ -140,7 +140,10 @@ def safe_encode(x, as_literal=True):
140140
value = x
141141
if not as_literal:
142142
return value
143-
return pm.Literal(text_type(value, 'utf-8'), pm.XSD['string'])
143+
if isinstance(value, str):
144+
return pm.Literal(value, pm.XSD['string'])
145+
else:
146+
return pm.Literal(text_type(value, 'utf-8'), pm.XSD['string'])
144147
if isinstance(x, int):
145148
if not as_literal:
146149
return x
@@ -183,7 +186,7 @@ def safe_encode(x, as_literal=True):
183186
return pm.Literal(dumps(x), nipype_ns['pickle'])
184187
except TypeError as e:
185188
iflogger.info(e)
186-
value = "Could not encode: " + text_type(str(e), 'utf-8')
189+
value = "Could not encode: " + str(e)
187190
if not as_literal:
188191
return value
189192
return pm.Literal(value, pm.XSD['string'])

nipype/utils/tests/test_provenance.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# vi: set ft=python sts=4 ts=4 sw=4 et:
33
from future import standard_library
44
standard_library.install_aliases()
5+
from builtins import str
6+
7+
import sys
58

69
from ...testing import assert_equal, assert_true, assert_false
710

@@ -20,4 +23,6 @@ def test_provenance():
2023
def test_safe_encode():
2124
a = '\xc3\xa9lg'
2225
out = safe_encode(a)
23-
yield assert_equal, out.value, text_type(a, 'utf-8')
26+
if not isinstance(a, str):
27+
a = text_type(a, 'utf-8')
28+
yield assert_equal, out.value, a

0 commit comments

Comments
 (0)