summaryrefslogtreecommitdiff
path: root/dbus
diff options
context:
space:
mode:
authorMichael Vogt <mvo@ubuntu.com>2012-10-12 13:37:51 +0200
committerSimon McVittie <simon.mcvittie@collabora.co.uk>2012-10-16 17:43:49 +0100
commit09b540bd55fb2fca14d4df7e0c520b8ba0ce4646 (patch)
treecf52df2ab0492255f75226ecaa70f833d3ba4d4d /dbus
parent5558ee11313fd929ed6aeb22228e89de2263e520 (diff)
Support unicode messages for DBusException in Python 2
[commit message amended -smcv] Bug: https://bugs.freedesktop.org/show_bug.cgi?id=55899 Reviewed-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Diffstat (limited to 'dbus')
-rw-r--r--dbus/exceptions.py23
1 files changed, 21 insertions, 2 deletions
diff --git a/dbus/exceptions.py b/dbus/exceptions.py
index 5283369..21efb06 100644
--- a/dbus/exceptions.py
+++ b/dbus/exceptions.py
@@ -27,6 +27,8 @@ __all__ = ('DBusException', 'MissingErrorHandlerException',
'IntrospectionParserException', 'UnknownMethodException',
'NameExistsException')
+from dbus._compat import is_py3
+
class DBusException(Exception):
@@ -49,15 +51,32 @@ class DBusException(Exception):
% ', '.join(kwargs.keys()))
Exception.__init__(self, *args)
- def __str__(self):
+ def __unicode_for_py2(self):
+ """Return a unicode error"""
+ s = Exception.__unicode__(self)
+ if self._dbus_error_name is not None:
+ return '%s: %s' % (self._dbus_error_name, s)
+ else:
+ return s
+
+ def __str_for_py3(self):
+ """Return a str error"""
s = Exception.__str__(self)
if self._dbus_error_name is not None:
return '%s: %s' % (self._dbus_error_name, s)
else:
return s
+ if is_py3:
+ __str__ = __str_for_py3
+ else:
+ __unicode__ = __unicode_for_py2
+
def get_dbus_message(self):
- s = Exception.__str__(self)
+ if is_py3:
+ s = Exception.__str__(self)
+ else:
+ s = Exception.__unicode__(self)
if isinstance(s, bytes):
return s.decode('utf-8', 'replace')
return s