]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Normalise to unicode in Address.__str__
authorMenno Smits <menno@freshfoo.com>
Mon, 23 Nov 2015 22:49:28 +0000 (11:49 +1300)
committerMenno Smits <menno@freshfoo.com>
Mon, 23 Nov 2015 22:49:28 +0000 (11:49 +1300)
__str__ was failing when the address fields were bytes (which the
typically are).

Fixes #186.

imapclient/response_types.py
imapclient/test/test_response_parser.py

index 8038db20d3d46310b4be9e36413e3356d668925f..166c20c3d1881dc7408fa5e38c412ea0b27f22fa 100644 (file)
@@ -2,11 +2,15 @@
 # Released subject to the New BSD License
 # Please see http://en.wikipedia.org/wiki/BSD_licenses
 
+from __future__ import unicode_literals
+
 from collections import namedtuple
 from email.utils import formataddr
 
 import six
 
+from .util import to_unicode
+
 
 class Envelope(namedtuple("Envelope", "date subject from_ sender reply_to to " +
                           "cc bcc in_reply_to message_id")):
@@ -50,7 +54,9 @@ class Address(namedtuple("Address", "name route mailbox host")):
     """
 
     def __str__(self):
-        return formataddr((self.name, self.mailbox + '@' + self.host))
+        return formataddr((
+            to_unicode(self.name),
+            to_unicode(self.mailbox) + '@' + to_unicode(self.host)))
 
 
 class SearchIds(list):
index 2b13486dff8f85e76eaccd0bedab69c757770ff6..3c13534e680e0e3d485a8e4c10b1c24f66e52eb5 100644 (file)
@@ -484,6 +484,14 @@ class TestParseFetchResponse(unittest.TestCase):
             }
         })
 
+    def test_Address_str(self):
+        self.assertEqual(str(Address(b"Mary Jane", None, b"mary", b"jane.org")),
+                         "Mary Jane <mary@jane.org>")
+
+        self.assertEqual(str(Address("Mary Jane", None, "mary", "jane.org")),
+                         "Mary Jane <mary@jane.org>")
+
+
 
 def add_crlf(text):
     return CRLF.join(text.splitlines()) + CRLF