[PATCH] mail: take --encoding and HGENCODING into account
Gábor Stefanik
gabor.stefanik at nng.com
Fri Oct 7 10:56:10 EDT 2016
# HG changeset patch
# User Gábor Stefanik <gabor.stefanik at nng.com>
# Date 1475667922 -7200
# Wed Oct 05 13:45:22 2016 +0200
# Node ID 31350841be0c6af1c335fb02b28b8fd1f79089b9
# 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.
diff --git a/mercurial/mail.py b/mercurial/mail.py
--- a/mercurial/mail.py
+++ b/mercurial/mail.py
@@ -205,22 +205,40 @@
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'
+ def codec2iana(encoding):
+ encoding = email.charset.Charset(encoding).input_charset.lower()
+
+ if encoding.startswith("iso") and not encoding.startswith("iso-"):
+ return "iso-" + encoding[3:]
+ return encoding
+
+ cs = "iso-8859-1" # a "safe" encoding with no invalid byte sequences
if not display:
try:
s.decode('us-ascii')
+ cs = 'us-ascii'
except UnicodeDecodeError:
try:
s.decode('utf-8')
cs = 'utf-8'
except UnicodeDecodeError:
- # We'll go with us-ascii as a fallback.
- pass
+ try:
+ s.decode(encoding.encoding)
+ cs = encoding.encoding
+ except UnicodeDecodeError:
+ try:
+ s.decode(encoding.fallbackencoding)
+ cs = encoding.fallbackencoding
+ except UnicodeDecodeError
+ # fall back to ISO-8859-1
+ pass
- return mimetextqp(s, subtype, cs)
+ return mimetextqp(s, subtype, codec2iana(cs))
def mimetextqp(body, subtype, charset):
'''Return MIME message.
More information about the Mercurial-devel
mailing list