[FIX] correct ustr. avoid a crash if getlocale() return None

bzr revid: jvo@tinyerp.com-20100512104026-mttsmb49zx80yhpc
This commit is contained in:
Jay (Open ERP) 2010-05-12 16:10:26 +05:30
parent 773238132b
commit 8f81995ec8
1 changed files with 26 additions and 14 deletions

View File

@ -818,6 +818,22 @@ class cache(object):
def to_xml(s):
return s.replace('&','&amp;').replace('<','&lt;').replace('>','&gt;')
def get_encodings():
yield 'utf8'
from locale import getpreferredencoding
prefenc = getpreferredencoding()
if prefenc:
yield prefenc
prefenc = {
'latin1': 'latin9',
'iso-8859-1': 'iso8859-15',
'cp1252': '1252',
}.get(prefenc.lower())
if prefenc:
yield prefenc
def ustr(value):
"""This method is similar to the builtin `str` method, except
it will return Unicode string.
@ -827,29 +843,25 @@ def ustr(value):
@rtype: unicode
@return: unicode string
"""
orig = value
if isinstance(value, Exception):
return exception_to_unicode(value)
if isinstance(value, unicode):
return value
if hasattr(value, '__unicode__'):
try:
return unicode(value)
if not isinstance(value, str):
value = str(value)
try: # first try utf-8
return unicode(value, 'utf-8')
except:
pass
try: # then extened iso-8858
return unicode(value, 'iso-8859-15')
except:
pass
for ln in get_encodings():
try:
return unicode(value, ln)
except:
pass
raise UnicodeError('unable de to convert %r' % (orig,))
# else use default system locale
from locale import getlocale
return unicode(value, getlocale()[1])
def exception_to_unicode(e):
if (sys.version_info[:2] < (2,6)) and hasattr(e, 'message'):