Patchwork [3,of,5] mail: pass in addr to _addressencode() in bytes

login
register
mail settings
Submitter Yuya Nishihara
Date Aug. 12, 2018, 3:27 a.m.
Message ID <6f357c717fa658015f5f.1534044440@mimosa>
Download mbox | patch
Permalink /patch/33688/
State Accepted
Headers show

Comments

Yuya Nishihara - Aug. 12, 2018, 3:27 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1534043496 -32400
#      Sun Aug 12 12:11:36 2018 +0900
# Node ID 6f357c717fa658015f5fd9c1be8e1ad9633ecddf
# Parent  90759135ec282bc5f2b847b2a4747cd452b2f4d9
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.

Patch

diff --git a/mercurial/mail.py b/mercurial/mail.py
--- a/mercurial/mail.py
+++ b/mercurial/mail.py
@@ -308,13 +308,12 @@  def headencode(ui, s, charsets=None, dis
     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 @@  def addressencode(ui, address, charsets=
     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 @@  def addrlistencode(ui, addrs, charsets=N
     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):