Patchwork [3,of,4] mail: convert addr to str early in addrlistencode()

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

Comments

Denis Laxalde - Nov. 9, 2019, 4:06 p.m.
# HG changeset patch
# User Denis Laxalde <denis@laxalde.org>
# Date 1573314873 -3600
#      Sat Nov 09 16:54:33 2019 +0100
# Node ID 068618bc1ae197973b082742a3446aeaab02cad9
# Parent  36c4ef3894cdb62ae0a63b7b266f1b07df6dcb12
mail: convert addr to str early in addrlistencode()
Yuya Nishihara - Nov. 10, 2019, 8:16 a.m.
On Sat, 09 Nov 2019 17:06:42 +0100, Denis Laxalde wrote:
> # HG changeset patch
> # User Denis Laxalde <denis@laxalde.org>
> # Date 1573314873 -3600
> #      Sat Nov 09 16:54:33 2019 +0100
> # Node ID 068618bc1ae197973b082742a3446aeaab02cad9
> # Parent  36c4ef3894cdb62ae0a63b7b266f1b07df6dcb12
> mail: convert addr to str early in addrlistencode()
> 
> diff --git a/mercurial/mail.py b/mercurial/mail.py
> --- a/mercurial/mail.py
> +++ b/mercurial/mail.py
> @@ -400,15 +400,14 @@ def addrlistencode(ui, addrs, charsets=N
>      '''Turns a list of addresses into a list of RFC-2047 compliant headers.
>      A single element of input list may contain multiple addresses, but output
>      always has one address per item'''
> -    for a in addrs:
> +    for idx, a in enumerate(addrs):
>          assert isinstance(a, bytes), r'%r unexpectedly not a bytestr' % a
> +        addrs[idx] = encoding.strfromlocal(a)

Better to not mutate the argument.

Patch

diff --git a/mercurial/mail.py b/mercurial/mail.py
--- a/mercurial/mail.py
+++ b/mercurial/mail.py
@@ -400,15 +400,14 @@  def addrlistencode(ui, addrs, charsets=N
     '''Turns a list of addresses into a list of RFC-2047 compliant headers.
     A single element of input list may contain multiple addresses, but output
     always has one address per item'''
-    for a in addrs:
+    for idx, a in enumerate(addrs):
         assert isinstance(a, bytes), r'%r unexpectedly not a bytestr' % a
+        addrs[idx] = encoding.strfromlocal(a)
     if display:
-        return [encoding.strfromlocal(a.strip()) for a in addrs if a.strip()]
+        return [a.strip() for a in addrs if a.strip()]
 
     result = []
-    for name, addr in email.utils.getaddresses(
-        [encoding.strfromlocal(a) for a in addrs]
-    ):
+    for name, addr in email.utils.getaddresses(addrs):
         if name or addr:
             r = _addressencode(ui, name, encoding.strtolocal(addr), charsets)
             result.append(r)