From 10df01a9c2a63f161b6c5043b206ed3c05572b5f Mon Sep 17 00:00:00 2001 From: Dan O'Reilly Date: Tue, 23 Dec 2008 21:10:17 -0500 Subject: [PATCH] Tweak misc.to_unicode() so that it is more likely to encode to utf-8 correctly. --- wicd/misc.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/wicd/misc.py b/wicd/misc.py index d7e65e7..f3e2abe 100644 --- a/wicd/misc.py +++ b/wicd/misc.py @@ -302,13 +302,22 @@ def get_gettext(): def to_unicode(x): """ Attempts to convert a string to utf-8. """ # If this is a unicode string, encode it and return - if type(x) == unicode: + if type(x) not in [unicode, str]: + return x + if isinstance(x, unicode): return x.encode('utf-8') encoding = locale.getpreferredencoding() try: - ret = x.decode(encoding, 'replace').encode('utf-8') - except: - ret = x.decode('utf-8', 'replace').encode('utf-8') + ret = x.decode(encoding).encode('utf-8') + except UnicodeError: + try: + ret = x.decode('utf-8').encode('utf-8') + except UnicodeError: + try: + ret = x.decode('latin-1').encode('utf-8') + except UnicodeError: + ret = x.decode('utf-8', 'replace').encode('utf-8') + return ret def RenameProcess(new_name): @@ -509,8 +518,8 @@ def get_language_list_gui(): language['bad_pass'] = _('Connection Failed: Could not authenticate (bad password?)') language['done'] = _('Done connecting...') language['scanning'] = _('Scanning') - language['cannot_start_daemon'] = _("Unable to connect to wicd daemon DBus interface." + \ - "This typically means there was a problem starting the daemon." + \ + language['cannot_start_daemon'] = _("Unable to connect to wicd daemon DBus interface. " + \ + "This typically means there was a problem starting the daemon. " + \ "Check the wicd log for more info") language['lost_dbus'] = _("The wicd daemon has shut down, the UI will not function properly until it is restarted.")