From 8f81995ec84899292709608945287d8a3faf1b6d Mon Sep 17 00:00:00 2001 From: "Jay (Open ERP)" Date: Wed, 12 May 2010 16:10:26 +0530 Subject: [FIX] correct ustr. avoid a crash if getlocale() return None bzr revid: jvo@tinyerp.com-20100512104026-mttsmb49zx80yhpc --- bin/tools/misc.py | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) (limited to 'bin/tools/misc.py') diff --git a/bin/tools/misc.py b/bin/tools/misc.py index 7aee1aed1b0..6bd100028be 100644 --- a/bin/tools/misc.py +++ b/bin/tools/misc.py @@ -818,6 +818,22 @@ class cache(object): def to_xml(s): return s.replace('&','&').replace('<','<').replace('>','>') +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'): -- cgit v1.2.3 From 8972e42f8a86c5741d34daead7baafc5491e3626 Mon Sep 17 00:00:00 2001 From: "Jay (Open ERP)" Date: Wed, 12 May 2010 16:37:58 +0530 Subject: Merged odo's branch about bug #495948 bzr revid: jvo@tinyerp.com-20100512110758-2dqrz2w00iq9brwq --- bin/tools/misc.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'bin/tools/misc.py') diff --git a/bin/tools/misc.py b/bin/tools/misc.py index 6bd100028be..68ace693765 100644 --- a/bin/tools/misc.py +++ b/bin/tools/misc.py @@ -924,7 +924,7 @@ def get_languages(): 'fr_BE': u'French (BE) / Français (BE)', 'fr_CH': u'French (CH) / Français (CH)', 'fr_FR': u'French / Français', - 'gl_ES': u'Galician / Spain', + 'gl_ES': u'Galician / Galego', 'gu_IN': u'Gujarati / India', 'hi_IN': u'Hindi / India', 'hr_HR': u'Croatian / hrvatski jezik', @@ -949,8 +949,8 @@ def get_languages(): 'ro_RO': u'Romanian / limba română', 'ru_RU': u'Russian / русский язык', 'si_LK': u'Sinhalese / Sri Lanka', - 'sk_SK': u'Slovak / Slovakia', - 'sl_SL': u'Slovenian / slovenščina', + 'sl_SI': u'Slovenian / slovenščina', + 'sk_SK': u'Slovak / Slovenský jazyk', 'sq_AL': u'Albanian / Shqipëri', 'sr_RS': u'Serbian / Serbia', 'sv_SE': u'Swedish / svenska', -- cgit v1.2.3