From d262628e89115bbcc33c439c77d33733c4a23491 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Tue, 16 Oct 2012 18:33:08 +0100 Subject: DBusException: override both __str__ and __unicode__ Avoid chaining up to the superclass, because that behaves particularly oddly. This fixes regression test failures: str(some_dbus_exception) was no longer prefixed with the D-Bus error name under Python 2. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=55899 --- dbus/exceptions.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/dbus/exceptions.py b/dbus/exceptions.py index 21efb06..0930425 100644 --- a/dbus/exceptions.py +++ b/dbus/exceptions.py @@ -51,15 +51,21 @@ class DBusException(Exception): % ', '.join(kwargs.keys())) Exception.__init__(self, *args) - def __unicode_for_py2(self): + def __unicode__(self): """Return a unicode error""" - s = Exception.__unicode__(self) + # We can't just use Exception.__unicode__ because it chains up weirdly. + # https://code.launchpad.net/~mvo/ubuntu/quantal/dbus-python/lp846044/+merge/129214 + if len(self.args) > 1: + s = unicode(self.args) + else: + s = ''.join(self.args) + if self._dbus_error_name is not None: return '%s: %s' % (self._dbus_error_name, s) else: return s - def __str_for_py3(self): + def __str__(self): """Return a str error""" s = Exception.__str__(self) if self._dbus_error_name is not None: @@ -67,18 +73,18 @@ class DBusException(Exception): else: return s - if is_py3: - __str__ = __str_for_py3 - else: - __unicode__ = __unicode_for_py2 - def get_dbus_message(self): - if is_py3: - s = Exception.__str__(self) + if len(self.args) > 1: + if is_py3: + s = str(self.args) + else: + s = unicode(self.args) else: - s = Exception.__unicode__(self) + s = ''.join(self.args) + if isinstance(s, bytes): return s.decode('utf-8', 'replace') + return s def get_dbus_name(self): -- cgit v1.2.1