Patchwork [v2] mail: take --encoding and HGENCODING into account

login
register
mail settings
Submitter Gábor Stefanik
Date Oct. 8, 2016, 10:05 a.m.
Message ID <c03ad1ac8772ffe8cd62.1475921130@waste.org>
Download mbox | patch
Permalink /patch/16913/
State Superseded
Headers show

Comments

Gábor Stefanik - Oct. 8, 2016, 10:05 a.m.
# HG changeset patch
# User Gábor Stefanik <gabor.stefanik@nng.com>
# Date 1475667922 -7200
#      Wed Oct 05 13:45:22 2016 +0200
# Node ID c03ad1ac8772ffe8cd626c70ea0f544298fdcf9c
# Parent  91a3c58ecf938ed675f5364b88f0d663f12b0047
mail: take --encoding and HGENCODING into account

Fall back to our encoding strategy for sending MIME text
that's neither ASCII nor UTF-8.

Patch

diff --git a/mercurial/mail.py b/mercurial/mail.py
--- a/mercurial/mail.py
+++ b/mercurial/mail.py
@@ -8,6 +8,7 @@ 
 from __future__ import absolute_import, print_function
 
 import email
+import email.charset
 import os
 import quopri
 import smtplib
@@ -203,24 +204,33 @@ 
             raise error.Abort(_('%r specified as email transport, '
                                'but not in PATH') % method)
 
+def codec2iana(cs):
+    ''''''
+    cs = email.charset.Charset(cs).input_charset.lower()
+
+    # "latin1" normalizes to "iso8859-1", standard calls for "iso-8859-1"
+    if cs.startswith("iso") and not cs.startswith("iso-"):
+        return "iso-" + cs[3:]
+    return cs
+
 def mimetextpatch(s, subtype='plain', display=False):
     '''Return MIME message suitable for a patch.
-    Charset will be detected as utf-8 or (possibly fake) us-ascii.
+    Charset will be detected by first trying to decode as us-ascii, then utf-8,
+    and finally the global encodings. If all those fail, fall back to
+    ISO-8859-1, an encoding with that allows all byte sequences.
     Transfer encodings will be used if necessary.'''
-
-    cs = 'us-ascii'
-    if not display:
+    
+    cs = ['us-ascii', 'utf-8', encoding.encoding, encoding.fallbackencoding]
+    if display:
+        return mimetextqp(s, subtype, None)
+    for charset in cs:
         try:
-            s.decode('us-ascii')
+            s.decode(charset)
+            return mimetextqp(s, subtype, codec2iana(charset))
         except UnicodeDecodeError:
-            try:
-                s.decode('utf-8')
-                cs = 'utf-8'
-            except UnicodeDecodeError:
-                # We'll go with us-ascii as a fallback.
-                pass
+            pass
 
-    return mimetextqp(s, subtype, cs)
+    return mimetextqp(s, subtype, "iso-8859-1")
 
 def mimetextqp(body, subtype, charset):
     '''Return MIME message.