From b290dd83fb2a90585b99cace25359ba1b44d182c Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Sun, 25 Apr 2010 13:49:31 +0100 Subject: [PATCH] Cope with non-ASCII Python exceptions of type "str" Python's IOException has 'str' type, but may contain UTF-8 sequences. This causes an error when trying to report the error if the message template is unicode. --- zeroinstall/injector/model.py | 12 +++++++++++- zeroinstall/injector/solver.py | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/zeroinstall/injector/model.py b/zeroinstall/injector/model.py index b0233a9..47c41dc 100644 --- a/zeroinstall/injector/model.py +++ b/zeroinstall/injector/model.py @@ -39,7 +39,17 @@ class InvalidInterface(SafeException): """Raised when parsing an invalid feed.""" def __init__(self, message, ex = None): if ex: - message += "\n\n(exact error: %s)" % ex + try: + message += "\n\n(exact error: %s)" % ex + except: + # Some Python messages have type str but contain UTF-8 sequences. + # (e.g. IOException). Adding these to a Unicode 'message' (e.g. + # after gettext translation) will cause an error. + import codecs + decoder = codecs.lookup('utf-8') + decex = decoder.decode(str(ex), errors = 'replace')[0] + message += "\n\n(exact error: %s)" % decex + SafeException.__init__(self, message) def _split_arch(arch): diff --git a/zeroinstall/injector/solver.py b/zeroinstall/injector/solver.py index b14d0ea..da96a29 100644 --- a/zeroinstall/injector/solver.py +++ b/zeroinstall/injector/solver.py @@ -290,7 +290,7 @@ class SATSolver(Solver): if feed.implementations: impls.extend(feed.implementations.values()) except Exception, ex: - warn(_("Failed to load feed %(feed)s for %(interface)s: %(exception)s"), {'feed': f, 'interface': iface, 'exception': str(ex)}) + warn(_("Failed to load feed %(feed)s for %(interface)s: %(exception)s"), {'feed': f, 'interface': iface, 'exception': ex}) impls.sort(lambda a, b: self.compare(iface, a, b, arch)) -- 2.11.4.GIT