D3953: mail: fix _encode to be more correct on Python 3
durin42 (Augie Fackler)
phabricator at mercurial-scm.org
Thu Aug 9 22:59:13 EDT 2018
This revision was automatically updated to reflect the committed changes.
Closed by commit rHG858fe9625dab: mail: fix _encode to be more correct on Python 3 (authored by durin42, committed by ).
REPOSITORY
rHG Mercurial
CHANGES SINCE LAST UPDATE
https://phab.mercurial-scm.org/D3953?vs=9609&id=10245
REVISION DETAIL
https://phab.mercurial-scm.org/D3953
AFFECTED FILES
mercurial/mail.py
CHANGE DETAILS
diff --git a/mercurial/mail.py b/mercurial/mail.py
--- a/mercurial/mail.py
+++ b/mercurial/mail.py
@@ -252,18 +252,35 @@
order. Tries both encoding and fallbackencoding for input. Only as
last resort send as is in fake ascii.
Caveat: Do not use for mail parts containing patches!'''
+ sendcharsets = charsets or _charsets(ui)
+ if not isinstance(s, bytes):
+ # We have unicode data, which we need to try and encode to
+ # some reasonable-ish encoding. Try the encodings the user
+ # wants, and fall back to garbage-in-ascii.
+ for ocs in sendcharsets:
+ try:
+ return s.encode(pycompat.sysstr(ocs)), ocs
+ except UnicodeEncodeError:
+ pass
+ except LookupError:
+ ui.warn(_('ignoring invalid sendcharset: %s\n') % ocs)
+ else:
+ # Everything failed, ascii-armor what we've got and send it.
+ return s.encode('ascii', 'backslashreplace')
+ # We have a bytes of unknown encoding. We'll try and guess a valid
+ # encoding, falling back to pretending we had ascii even though we
+ # know that's wrong.
try:
s.decode('ascii')
except UnicodeDecodeError:
- sendcharsets = charsets or _charsets(ui)
for ics in (encoding.encoding, encoding.fallbackencoding):
try:
u = s.decode(ics)
except UnicodeDecodeError:
continue
for ocs in sendcharsets:
try:
- return u.encode(ocs), ocs
+ return u.encode(pycompat.sysstr(ocs)), ocs
except UnicodeEncodeError:
pass
except LookupError:
To: durin42, #hg-reviewers, indygreg
Cc: indygreg, mercurial-devel
More information about the Mercurial-devel
mailing list