Show More
@@ -8,6 +8,7 b'' | |||||
8 | from __future__ import absolute_import, print_function |
|
8 | from __future__ import absolute_import, print_function | |
9 |
|
9 | |||
10 | import email |
|
10 | import email | |
|
11 | import email.charset | |||
11 | import email.header |
|
12 | import email.header | |
12 | import os |
|
13 | import os | |
13 | import quopri |
|
14 | import quopri | |
@@ -204,24 +205,33 b' def validateconfig(ui):' | |||||
204 | raise error.Abort(_('%r specified as email transport, ' |
|
205 | raise error.Abort(_('%r specified as email transport, ' | |
205 | 'but not in PATH') % method) |
|
206 | 'but not in PATH') % method) | |
206 |
|
207 | |||
|
208 | def codec2iana(cs): | |||
|
209 | '''''' | |||
|
210 | cs = email.charset.Charset(cs).input_charset.lower() | |||
|
211 | ||||
|
212 | # "latin1" normalizes to "iso8859-1", standard calls for "iso-8859-1" | |||
|
213 | if cs.startswith("iso") and not cs.startswith("iso-"): | |||
|
214 | return "iso-" + cs[3:] | |||
|
215 | return cs | |||
|
216 | ||||
207 | def mimetextpatch(s, subtype='plain', display=False): |
|
217 | def mimetextpatch(s, subtype='plain', display=False): | |
208 | '''Return MIME message suitable for a patch. |
|
218 | '''Return MIME message suitable for a patch. | |
209 | Charset will be detected as utf-8 or (possibly fake) us-ascii. |
|
219 | Charset will be detected by first trying to decode as us-ascii, then utf-8, | |
|
220 | and finally the global encodings. If all those fail, fall back to | |||
|
221 | ISO-8859-1, an encoding with that allows all byte sequences. | |||
210 | Transfer encodings will be used if necessary.''' |
|
222 | Transfer encodings will be used if necessary.''' | |
211 |
|
223 | |||
212 | cs = 'us-ascii' |
|
224 | cs = ['us-ascii', 'utf-8', encoding.encoding, encoding.fallbackencoding] | |
213 |
if |
|
225 | if display: | |
|
226 | return mimetextqp(s, subtype, 'us-ascii') | |||
|
227 | for charset in cs: | |||
214 | try: |
|
228 | try: | |
215 |
s.decode( |
|
229 | s.decode(charset) | |
|
230 | return mimetextqp(s, subtype, codec2iana(charset)) | |||
216 | except UnicodeDecodeError: |
|
231 | except UnicodeDecodeError: | |
217 | try: |
|
|||
218 | s.decode('utf-8') |
|
|||
219 | cs = 'utf-8' |
|
|||
220 | except UnicodeDecodeError: |
|
|||
221 | # We'll go with us-ascii as a fallback. |
|
|||
222 |
|
|
232 | pass | |
223 |
|
233 | |||
224 |
return mimetextqp(s, subtype, |
|
234 | return mimetextqp(s, subtype, "iso-8859-1") | |
225 |
|
235 | |||
226 | def mimetextqp(body, subtype, charset): |
|
236 | def mimetextqp(body, subtype, charset): | |
227 | '''Return MIME message. |
|
237 | '''Return MIME message. |
@@ -632,7 +632,7 b' iso-8859-1 patch:' | |||||
632 | $ hg commit -A -d '5 0' -m 'isolatin 8-bit encoding' |
|
632 | $ hg commit -A -d '5 0' -m 'isolatin 8-bit encoding' | |
633 | adding isolatin |
|
633 | adding isolatin | |
634 |
|
634 | |||
635 | fake ascii mbox: |
|
635 | iso-8859-1 mbox: | |
636 | $ hg email --date '1970-1-1 0:5' -f quux -t foo -c bar -r tip -m mbox |
|
636 | $ hg email --date '1970-1-1 0:5' -f quux -t foo -c bar -r tip -m mbox | |
637 | this patch series consists of 1 patches. |
|
637 | this patch series consists of 1 patches. | |
638 |
|
638 | |||
@@ -640,9 +640,9 b' fake ascii mbox:' | |||||
640 | sending [PATCH] isolatin 8-bit encoding ... |
|
640 | sending [PATCH] isolatin 8-bit encoding ... | |
641 | $ cat mbox |
|
641 | $ cat mbox | |
642 | From quux ... ... .. ..:..:.. .... (re) |
|
642 | From quux ... ... .. ..:..:.. .... (re) | |
643 |
Content-Type: text/plain; charset=" |
|
643 | Content-Type: text/plain; charset="iso-8859-1" | |
644 | MIME-Version: 1.0 |
|
644 | MIME-Version: 1.0 | |
645 |
Content-Transfer-Encoding: |
|
645 | Content-Transfer-Encoding: quoted-printable | |
646 | Subject: [PATCH] isolatin 8-bit encoding |
|
646 | Subject: [PATCH] isolatin 8-bit encoding | |
647 | X-Mercurial-Node: 240fb913fc1b7ff15ddb9f33e73d82bf5277c720 |
|
647 | X-Mercurial-Node: 240fb913fc1b7ff15ddb9f33e73d82bf5277c720 | |
648 | X-Mercurial-Series-Index: 1 |
|
648 | X-Mercurial-Series-Index: 1 | |
@@ -667,7 +667,7 b' fake ascii mbox:' | |||||
667 | --- /dev/null Thu Jan 01 00:00:00 1970 +0000 |
|
667 | --- /dev/null Thu Jan 01 00:00:00 1970 +0000 | |
668 | +++ b/isolatin Thu Jan 01 00:00:05 1970 +0000 |
|
668 | +++ b/isolatin Thu Jan 01 00:00:05 1970 +0000 | |
669 | @@ -0,0 +1,1 @@ |
|
669 | @@ -0,0 +1,1 @@ | |
670 |
+h |
|
670 | +h=F6mma! | |
671 |
|
671 | |||
672 |
|
672 | |||
673 |
|
673 |
General Comments 0
You need to be logged in to leave comments.
Login now