make to_unicode attempt utf8 fallback on non-string types

This commit is contained in:
Tim Cuthbertson
2014-09-14 10:57:03 +10:00
parent ee24a96758
commit 111c2b60b5

View File

@@ -50,13 +50,15 @@ if PY3:
else:
def to_unicode(s):
try:
if isinstance(s, unicode):
return s
if isinstance(s, str):
return unicode(s, 'UTF-8')
return unicode(s)
except UnicodeDecodeError:
return unicode(repr(str(s)))
s = str(s)
try:
# try utf-8, the most likely case
return unicode(s, 'UTF-8')
except UnicodeDecodeError:
# Can't decode, just use `repr`
return unicode(repr(s))
BLACKLISTED_WRITERS = [
'nose[\\/]result\\.pyc?$',