summaryrefslogtreecommitdiff
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
parent5558ee11313fd929ed6aeb22228e89de2263e520 (diff)
downloaddbus-python-09b540bd55fb2fca14d4df7e0c520b8ba0ce4646.tar.gz
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>
-rw-r--r--dbus/exceptions.py23
-rwxr-xr-xtest/run-test.sh6
-rw-r--r--test/test-exception-py2.py58
-rw-r--r--test/test-exception-py3.py31
4 files changed, 116 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
diff --git a/test/run-test.sh b/test/run-test.sh
index 5648179..76bd075 100755
--- a/test/run-test.sh
+++ b/test/run-test.sh
@@ -67,6 +67,12 @@ echo "DBUS_TOP_BUILDDIR=$DBUS_TOP_BUILDDIR"
echo "PYTHONPATH=$PYTHONPATH"
echo "PYTHON=$PYTHON"
+# the exceptions handling test is version specific
+PYMAJOR=$($PYTHON -c 'import sys;print(sys.version_info[0])')
+PYTEST=test-exception-py$PYMAJOR.py
+echo "running $PYTEST"
+$PYTHON "$DBUS_TOP_SRCDIR"/test/$PYTEST || die "$PYTEST failed"
+
echo "running test-standalone.py"
$PYTHON "$DBUS_TOP_SRCDIR"/test/test-standalone.py || die "test-standalone.py failed"
diff --git a/test/test-exception-py2.py b/test/test-exception-py2.py
new file mode 100644
index 0000000..3a8026c
--- /dev/null
+++ b/test/test-exception-py2.py
@@ -0,0 +1,58 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+import unittest
+
+import dbus
+
+# from test-service.py
+class ServiceError(dbus.DBusException):
+ """Exception representing a normal "environmental" error"""
+ include_traceback = False
+ _dbus_error_name = 'com.example.Networking.ServerError'
+
+
+class DBusExceptionTestCase(unittest.TestCase):
+ """Test the DBusException str/unicode behavior with py2"""
+
+ def test_dbus_exception_normal(self):
+ """Test the normal Exception behavior"""
+ e = dbus.exceptions.DBusException("baaa")
+ msg = e.get_dbus_message()
+ self.assertEqual(msg, u"baaa")
+
+ def test_dbus_exception_unicode(self):
+ """Test that DBusExceptions that take a py2 unicode work"""
+ e = dbus.exceptions.DBusException(u"bäää")
+ msg = e.get_dbus_message()
+ self.assertEqual(msg, u"bäää")
+
+ def test_dbus_exception_convert_str(self):
+ """Test that converting a DBusException to str() works as expected"""
+ e = dbus.exceptions.DBusException(u"bxxx")
+ self.assertEqual(str(e), "bxxx")
+
+ def test_dbus_exception_convert_str_fail(self):
+ """Test that a non-ascii Exception fails to convert to str"""
+ with self.assertRaises(ValueError):
+ e = dbus.exceptions.DBusException(u"bä")
+ print str(e)
+
+ def test_dbus_exception_convert_unicode(self):
+ """Test that converting a DBusEception to unicode works"""
+ e = dbus.exceptions.DBusException(u"bäää")
+ self.assertEqual(e.get_dbus_message(), u"bäää")
+ self.assertEqual(e.__unicode__(), u"bäää")
+ self.assertEqual(unicode(e), u"bäää")
+
+ def test_subclass_exception_unicode(self):
+ """Test that DBusExceptions that take a py2 unicode work"""
+ e = ServiceError(u"bäää")
+ msg = e.get_dbus_message()
+ self.assertEqual(msg, u"bäää")
+ self.assertEqual(
+ unicode(e), u"com.example.Networking.ServerError: bäää")
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/test/test-exception-py3.py b/test/test-exception-py3.py
new file mode 100644
index 0000000..eddb45a
--- /dev/null
+++ b/test/test-exception-py3.py
@@ -0,0 +1,31 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+import unittest
+
+import dbus
+
+# from test-service.py
+class ServiceError(dbus.DBusException):
+ """Exception representing a normal "environmental" error"""
+ include_traceback = False
+ _dbus_error_name = 'com.example.Networking.ServerError'
+
+
+class DBusExceptionTestCase(unittest.TestCase):
+
+ def test_dbus_exception(self):
+ e = dbus.exceptions.DBusException("bäää")
+ msg = e.get_dbus_message()
+ self.assertEqual(msg, "bäää")
+ self.assertEqual(str(e), "bäää")
+
+ def test_subclass_exception(self):
+ e = ServiceError("bäää")
+ msg = e.get_dbus_message()
+ self.assertEqual(msg, "bäää")
+ self.assertEqual(str(e), "com.example.Networking.ServerError: bäää")
+
+
+if __name__ == "__main__":
+ unittest.main()