[IMP] tools.ustr: fix possible stack overflow for exception_to_unicode + tabs->spaces

bzr revid: odo@openerp.com-20111007144806-lodke0woag32sin5
This commit is contained in:
Olivier Dony 2011-10-07 16:48:06 +02:00
parent 5d8e9b91e5
commit 5aca6a91fa
1 changed files with 28 additions and 28 deletions

View File

@ -108,27 +108,27 @@ class Logger(object):
def get_encodings(hint_encoding='utf-8'):
fallbacks = {
'latin1': 'latin9',
'iso-8859-1': 'iso8859-15',
'cp1252': '1252',
'latin1': 'latin9',
'iso-8859-1': 'iso8859-15',
'cp1252': '1252',
}
if hint_encoding:
yield hint_encoding
if hint_encoding.lower() in fallbacks:
yield fallbacks[hint_encoding.lower()]
yield hint_encoding
if hint_encoding.lower() in fallbacks:
yield fallbacks[hint_encoding.lower()]
# some defaults (also taking care of pure ASCII)
for charset in ['utf8','latin1']:
if not (hint_encoding) or (charset.lower() != hint_encoding.lower()):
yield charset
if not (hint_encoding) or (charset.lower() != hint_encoding.lower()):
yield charset
from locale import getpreferredencoding
prefenc = getpreferredencoding()
if prefenc and prefenc.lower() != 'utf-8':
yield prefenc
prefenc = fallbacks.get(prefenc.lower())
if prefenc:
yield prefenc
yield prefenc
prefenc = fallbacks.get(prefenc.lower())
if prefenc:
yield prefenc
def ustr(value, hint_encoding='utf-8'):
"""This method is similar to the builtin `str` method, except
@ -136,40 +136,40 @@ def ustr(value, hint_encoding='utf-8'):
@param value: the value to convert
@param hint_encoding: an optional encoding that was detected
upstream and should be tried first to
decode ``value``.
upstream and should be tried first to
decode ``value``.
@rtype: unicode
@return: unicode string
"""
if isinstance(value, Exception):
return exception_to_unicode(value)
return exception_to_unicode(value)
if isinstance(value, unicode):
return value
return value
if not isinstance(value, basestring):
try:
return unicode(value)
except Exception:
raise UnicodeError('unable to convert %r' % (value,))
try:
return unicode(value)
except Exception:
raise UnicodeError('unable to convert %r' % (value,))
for ln in get_encodings(hint_encoding):
try:
return unicode(value, ln)
except Exception:
pass
try:
return unicode(value, ln)
except Exception:
pass
raise UnicodeError('unable to convert %r' % (value,))
def exception_to_unicode(e):
if (sys.version_info[:2] < (2,6)) and hasattr(e, 'message'):
return ustr(e.message)
return ustr(e.message)
if hasattr(e, 'args'):
return "\n".join((ustr(a) for a in e.args))
return "\n".join((ustr(a) for a in e.args))
try:
return ustr(e)
return unicode(e)
except Exception:
return u"Unknown message"
return u"Unknown message"
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: