Patchwork [2,of,4] mail: let addressencode() / addrlistencode() return native strings

login
register
mail settings
Submitter Denis Laxalde
Date Nov. 9, 2019, 4:06 p.m.
Message ID <36c4ef3894cdb62ae0a6.1573315601@marimba>
Download mbox | patch
Permalink /patch/43100/
State Accepted
Headers show

Comments

Denis Laxalde - Nov. 9, 2019, 4:06 p.m.
# HG changeset patch
# User Denis Laxalde <denis@laxalde.org>
# Date 1573309012 -3600
#      Sat Nov 09 15:16:52 2019 +0100
# Node ID 36c4ef3894cdb62ae0a63b7b266f1b07df6dcb12
# Parent  1c7a66bf3315f69036420e2b07b584abacfef881
mail: let addressencode() / addrlistencode() return native strings

Avoids conversion to "str" on py3.

Patch

diff --git a/hgext/notify.py b/hgext/notify.py
--- a/hgext/notify.py
+++ b/hgext/notify.py
@@ -430,14 +430,14 @@  class notifier(object):
             sender = self.ui.config(b'email', b'from') or self.ui.username()
         if b'@' not in sender or b'@localhost' in sender:
             sender = self.fixmail(sender)
-        msg[r'From'] = encoding.strfromlocal(
-            mail.addressencode(self.ui, sender, self.charsets, self.test)
+        msg[r'From'] = mail.addressencode(
+            self.ui, sender, self.charsets, self.test
         )
 
         msg[r'X-Hg-Notification'] = r'changeset %s' % ctx
         if not msg[r'Message-Id']:
             msg[r'Message-Id'] = messageid(ctx, self.domain, self.messageidseed)
-        msg[r'To'] = encoding.strfromlocal(b', '.join(sorted(subs)))
+        msg[r'To'] = ', '.join(sorted(subs))
 
         msgtext = msg.as_bytes() if pycompat.ispy3 else msg.as_string()
         if self.test:
diff --git a/hgext/patchbomb.py b/hgext/patchbomb.py
--- a/hgext/patchbomb.py
+++ b/hgext/patchbomb.py
@@ -943,13 +943,13 @@  def email(ui, repo, *revs, **opts):
 
         start_time = (start_time[0] + 1, start_time[1])
         m[b'From'] = sender
-        m[b'To'] = b', '.join(to)
+        m[b'To'] = ', '.join(to)
         if cc:
-            m[b'Cc'] = b', '.join(cc)
+            m[b'Cc'] = ', '.join(cc)
         if bcc:
-            m[b'Bcc'] = b', '.join(bcc)
+            m[b'Bcc'] = ', '.join(bcc)
         if replyto:
-            m[b'Reply-To'] = b', '.join(replyto)
+            m[b'Reply-To'] = ', '.join(replyto)
         # Fix up all headers to be native strings.
         # TODO(durin42): this should probably be cleaned up above in the future.
         if pycompat.ispy3:
@@ -992,7 +992,6 @@  def email(ui, repo, *revs, **opts):
             generator = mail.Generator(fp, mangle_from_=False)
             generator.flatten(m, 0)
             alldests = to + bcc + cc
-            alldests = [encoding.strfromlocal(d) for d in alldests]
             sendmail(sender_addr, alldests, fp.getvalue())
 
     progress.complete()
diff --git a/mercurial/mail.py b/mercurial/mail.py
--- a/mercurial/mail.py
+++ b/mercurial/mail.py
@@ -385,15 +385,13 @@  def _addressencode(ui, name, addr, chars
             addr.decode('ascii')
         except UnicodeDecodeError:
             raise error.Abort(_(b'invalid local address: %s') % addr)
-    return pycompat.bytesurl(
-        email.utils.formataddr((name, encoding.strfromlocal(addr)))
-    )
+    return email.utils.formataddr((name, encoding.strfromlocal(addr)))
 
 
 def addressencode(ui, address, charsets=None, display=False):
     '''Turns address into RFC-2047 compliant header.'''
     if display or not address:
-        return address or b''
+        return encoding.strfromlocal(address or b'')
     name, addr = email.utils.parseaddr(encoding.strfromlocal(address))
     return _addressencode(ui, name, encoding.strtolocal(addr), charsets)
 
@@ -405,7 +403,7 @@  def addrlistencode(ui, addrs, charsets=N
     for a in addrs:
         assert isinstance(a, bytes), r'%r unexpectedly not a bytestr' % a
     if display:
-        return [a.strip() for a in addrs if a.strip()]
+        return [encoding.strfromlocal(a.strip()) for a in addrs if a.strip()]
 
     result = []
     for name, addr in email.utils.getaddresses(