[PATCH] mail: Take --encoding and HGENCODING into account
Yuya Nishihara
yuya at tcha.org
Fri Oct 7 04:48:21 UTC 2016
On Wed, 05 Oct 2016 13:46:15 +0200, Gábor Stefanik wrote:
> # 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 85a6a72d4868c20c066ae4add380b70d13c1c2df
> # Parent 1779dde4c9ef97cb242f8d501655f236f66e5439
> 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 -r 1779dde4c9ef -r 85a6a72d4868 mercurial/mail.py
> --- a/mercurial/mail.py Sun Oct 02 22:34:40 2016 -0700
> +++ b/mercurial/mail.py Wed Oct 05 13:45:22 2016 +0200
> @@ -205,9 +205,17 @@
>
> 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 as us-ascii or utf-8, falling back to hg's
> + current encoding if neither of those works.
> Transfer encodings will be used if necessary.'''
>
> + def codec2iana(encoding):
> + encoding = email.charset.Charset(encoding).input_charset
> +
> + if encoding.startswith("iso") and not encoding.startswith("iso-"):
> + return "iso-" + encoding[3:]
> + return encoding
> +
> cs = 'us-ascii'
> if not display:
> try:
> @@ -217,10 +225,10 @@
> s.decode('utf-8')
> cs = 'utf-8'
> except UnicodeDecodeError:
> - # We'll go with us-ascii as a fallback.
> - pass
> + # We'll go with HGENCODING as a fallback.
> + cs = encoding.encoding
>
> - return mimetextqp(s, subtype, cs)
> + return mimetextqp(s, subtype, codec2iana(cs))
If 's' contained only a patch header, encoding.encoding would be fine. But
it's wrong to use encoding.encoding as the character set of patch body.
I think that's why the docstring says "(possibly fake) us-ascii."
More information about the Mercurial-devel
mailing list