[FIX] tools.ustr: corrected backwards compatibility for conversion of non-string objects

bzr revid: odo@openerp.com-20100702153357-5qc6y03lxudynpyz
This commit is contained in:
Olivier Dony 2010-07-02 17:33:57 +02:00
parent d19ade2298
commit 59fdce2fe2
1 changed files with 7 additions and 2 deletions

View File

@ -857,19 +857,24 @@ def ustr(value, hint_encoding='utf-8'):
@rtype: unicode
@return: unicode string
"""
orig = value
if isinstance(value, Exception):
return exception_to_unicode(value)
if isinstance(value, unicode):
return value
if not isinstance(value, basestring):
try:
return unicode(value)
except Exception:
raise UnicodeError('unable de to convert %r' % (value,))
for ln in get_encodings(hint_encoding):
try:
return unicode(value, ln)
except Exception:
pass
raise UnicodeError('unable de to convert %r' % (orig,))
raise UnicodeError('unable de to convert %r' % (value,))
def exception_to_unicode(e):