mail: pass in addr to _addressencode() in bytes
authorYuya Nishihara <yuya@tcha.org>
Sun, 12 Aug 2018 12:11:36 +0900
changeset 39106 ebf54a34b7b7
parent 39105 f68ad9b4a43b
child 39107 c2327bb3505d
mail: pass in addr to _addressencode() in bytes _addressencode() doesn't process a unicode addr well. For example, acc.encode('ascii') would raise UnicodeEncodeError if acc was a unicode. This patch temporarily restores the pre-952bf3c948f0 _addressencode(). Several tests would be broken on Python 3.
mercurial/mail.py
--- a/mercurial/mail.py	Sun Aug 12 12:01:34 2018 +0900
+++ b/mercurial/mail.py	Sun Aug 12 12:11:36 2018 +0900
@@ -308,13 +308,12 @@
     return s
 
 def _addressencode(ui, name, addr, charsets=None):
+    assert isinstance(addr, bytes)
     name = headencode(ui, name, charsets)
     try:
-        acc, dom = addr.split(r'@')
+        acc, dom = addr.split('@')
         acc = acc.encode('ascii')
-        if isinstance(dom, bytes):
-            dom = dom.decode(encoding.encoding)
-        dom = dom.encode('idna')
+        dom = dom.decode(encoding.encoding).encode('idna')
         addr = '%s@%s' % (acc, dom)
     except UnicodeDecodeError:
         raise error.Abort(_('invalid email address: %s') % addr)
@@ -332,7 +331,7 @@
     if display or not address:
         return address or ''
     name, addr = email.utils.parseaddr(encoding.strfromlocal(address))
-    return _addressencode(ui, name, addr, charsets)
+    return _addressencode(ui, name, encoding.strtolocal(addr), charsets)
 
 def addrlistencode(ui, addrs, charsets=None, display=False):
     '''Turns a list of addresses into a list of RFC-2047 compliant headers.
@@ -347,7 +346,8 @@
     for name, addr in email.utils.getaddresses(
             [encoding.strfromlocal(a) for a in addrs]):
         if name or addr:
-            result.append(_addressencode(ui, name, addr, charsets))
+            r = _addressencode(ui, name, encoding.strtolocal(addr), charsets)
+            result.append(r)
     return result
 
 def mimeencode(ui, s, charsets=None, display=False):