[PATCH 06 of 10 STABLE] py3: fix headencode() with display=False

Denis Laxalde denis at laxalde.org
Thu Oct 24 11:30:37 EDT 2019


# HG changeset patch
# User Denis Laxalde <denis.laxalde at logilab.fr>
# Date 1571930203 -7200
#      Thu Oct 24 17:16:43 2019 +0200
# Branch stable
# Node ID aaacb57e24c54398dfdd56bf93b68b7bad510b4c
# Parent  d1382260a9c847b4650c95372161541ac82cb56b
py3: fix headencode() with display=False

We previously called str() on a email.header.Header object. On Python 2,
this returns a bytestring and the __str__ method is actually an alias to
.encode() method. On Python 3, __str__ does not perform encoding (and
returns a unicode string). To keep a consistent behavior across Python
versions, we explicitly use .encode() and we wrap the result with
encoding.strtolocal() to get a bytestring in all cases. As a side effect
of forcing bytes conversion, we need to decode back in _addressencode().

This is to make test-notify.t pass on Python 3.

Also note that headers are now encoded in some patchbomb tests; this is
because the charset is not always "us-ascii" ("iso-8859-1" otherwise) on
Python 3.

diff --git a/mercurial/mail.py b/mercurial/mail.py
--- a/mercurial/mail.py
+++ b/mercurial/mail.py
@@ -363,13 +363,13 @@ def headencode(ui, s, charsets=None, dis
     if not display:
         # split into words?
         s, cs = _encode(ui, s, charsets)
-        return str(email.header.Header(s, cs))
+        return encoding.strtolocal(email.header.Header(s, cs).encode())
     return s
 
 
 def _addressencode(ui, name, addr, charsets=None):
     assert isinstance(addr, bytes)
-    name = headencode(ui, name, charsets)
+    name = encoding.strfromlocal(headencode(ui, name, charsets))
     try:
         acc, dom = addr.split(b'@')
         acc.decode('ascii')
diff --git a/tests/test-patchbomb.t b/tests/test-patchbomb.t
--- a/tests/test-patchbomb.t
+++ b/tests/test-patchbomb.t
@@ -512,7 +512,8 @@ mime encoded mbox (base64):
   X-Mercurial-Series-Id: <909a00e13e9d78b575ae.240 at test-hostname>
   User-Agent: Mercurial-patchbomb/* (glob)
   Date: Thu, 01 Jan 1970 00:04:00 +0000
-  From: Q <quux>
+  From: Q <quux> (no-py3 !)
+  From: =?iso-8859-1?q?Q?= <quux> (py3 !)
   To: foo
   Cc: bar
   
@@ -2397,9 +2398,12 @@ test multi-address parsing:
   User-Agent: Mercurial-patchbomb/* (glob)
   Date: Tue, 01 Jan 1980 00:01:00 +0000
   From: quux
-  To: spam <spam>, eggs, toast
-  Cc: foo, bar at example.com, "A, B <>" <a at example.com>
-  Bcc: "Quux, A." <quux>
+  To: spam <spam>, eggs, toast (no-py3 !)
+  Cc: foo, bar at example.com, "A, B <>" <a at example.com> (no-py3 !)
+  Bcc: "Quux, A." <quux> (no-py3 !)
+  To: =?iso-8859-1?q?spam?= <spam>, eggs, toast (py3 !)
+  Cc: foo, bar at example.com, =?iso-8859-1?q?A=2C_B_=3C=3E?= <a at example.com> (py3 !)
+  Bcc: =?iso-8859-1?q?Quux=2C_A=2E?= <quux> (py3 !)
   
   # HG changeset patch
   # User test


More information about the Mercurial-devel mailing list