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