telepathy-gabble: tests: don't crash in verbose mode on unicode in stanzas

Will Thompson wjt at kemper.freedesktop.org
Fri Dec 7 11:31:42 PST 2012


Module: telepathy-gabble
Branch: master
Commit: bf5dcce04b9063c9482def3041dd80b79d47f5b7
URL:    http://cgit.freedesktop.org/telepathy/telepathy-gabble/commit/?id=bf5dcce04b9063c9482def3041dd80b79d47f5b7

Author: Will Thompson <will.thompson at collabora.co.uk>
Date:   Fri Dec  7 16:13:16 2012 +0000

tests: don't crash in verbose mode on unicode in stanzas

We have this hack that sets __repr__ on domish.Element to call its
toXml() method, which ends up being used by the verbose logging code
when it dumps all of an event's attributes. Unfortunately, this blows up
if a stanza contains non-ASCII characters, because repr() tries to
convert unicode to str using .encode('ascii').

This made running jingle/test-send-file in verbose mode fail, because
the file being sent by the test has a non-ASCII filename. The fix is to
make the __repr__ hack escape non-ASCII characters using the
unicode-escape codec.

---

 tests/twisted/gabbletest.py |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)

diff --git a/tests/twisted/gabbletest.py b/tests/twisted/gabbletest.py
index 4baf924..8cc7d24 100644
--- a/tests/twisted/gabbletest.py
+++ b/tests/twisted/gabbletest.py
@@ -555,11 +555,19 @@ def disconnect_conn(q, conn, stream, expected_before=[], expected_after=[]):
 
     return before_events[:-2], after_events[:-1]
 
+def element_repr(element):
+    """__repr__ cannot safely return non-ASCII: see
+    <http://bugs.python.org/issue5876>. So we print non-ASCII characters as
+    \uXXXX escapes in debug output
+
+    """
+    return element.toXml().encode('unicode-escape')
+
 def exec_test_deferred(fun, params, protocol=None, timeout=None,
                         authenticator=None, num_instances=1,
                         do_connect=True):
     # hack to ease debugging
-    domish.Element.__repr__ = domish.Element.toXml
+    domish.Element.__repr__ = element_repr
     colourer = None
 
     if sys.stdout.isatty() or 'CHECK_FORCE_COLOR' in os.environ:



More information about the telepathy-commits mailing list