diff options
| author | Olivier Dony <odo@openerp.com> | 2012-03-16 15:40:02 +0100 |
|---|---|---|
| committer | Olivier Dony <odo@openerp.com> | 2012-03-16 15:40:02 +0100 |
| commit | 7f8965a00a9c229e80a3d319209b792aa392ec64 (patch) | |
| tree | a0c244344d2c6d53952a25b945bc4f9dfe2aab10 | |
| parent | 4af38cc64a07179a78b34d9cee55583714f106a0 (diff) | |
[IMP] tools.ustr: allow specific error-handling flags a la `unicode`
Also cleaned up docstrings
bzr revid: odo@openerp.com-20120316144002-og07it5u7x2azhu4
| -rw-r--r-- | openerp/loglevels.py | 37 |
1 files changed, 21 insertions, 16 deletions
diff --git a/openerp/loglevels.py b/openerp/loglevels.py index 29bd6e5dc18..8973453aabd 100644 --- a/openerp/loglevels.py +++ b/openerp/loglevels.py @@ -121,17 +121,22 @@ def get_encodings(hint_encoding='utf-8'): if prefenc: yield prefenc -def ustr(value, hint_encoding='utf-8'): - """This method is similar to the builtin `str` method, except - it will return unicode() string. - - @param value: the value to convert - @param hint_encoding: an optional encoding that was detected - upstream and should be tried first to - decode ``value``. - - @rtype: unicode - @return: unicode string +def ustr(value, hint_encoding='utf-8', errors='strict'): + """This method is similar to the builtin `unicode`, except + that it may try multiple encodings to find one that works + for decoding `value`, and defaults to 'utf-8' first. + + :param: value: the value to convert + :param: hint_encoding: an optional encoding that was detecte + upstream and should be tried first to decode ``value``. + :param str error: optional `errors` flag to pass to the unicode + built-in to indicate how illegal character values should be + treated: 'strict', 'ignore' or 'replace'. Passing anything + other than 'strict' means that the first encoding tried will + succeed, even if it's not the correct one to use, so be + careful! + :rtype: unicode + :raise: UnicodeError if value cannot be coerced to unicode """ if isinstance(value, Exception): return exception_to_unicode(value) @@ -141,25 +146,25 @@ def ustr(value, hint_encoding='utf-8'): if not isinstance(value, basestring): try: - return unicode(value) + return unicode(value, errors=errors) except Exception: raise UnicodeError('unable to convert %r' % (value,)) for ln in get_encodings(hint_encoding): try: - return unicode(value, ln) + return unicode(value, ln, errors=errors) except Exception: pass raise UnicodeError('unable to convert %r' % (value,)) -def exception_to_unicode(e): +def exception_to_unicode(e, errors='strict'): if (sys.version_info[:2] < (2,6)) and hasattr(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, errors=errors) for a in e.args)) try: - return unicode(e) + return unicode(e, errors=errors) except Exception: return u"Unknown message" |
