# HG changeset patch # User Augie Fackler # Date 1531777683 14400 # Node ID 858fe9625dab715dab21f909c0662429c37c2913 # Parent b95538a216136c4cd4eca24b6af9b51af044ad35 mail: fix _encode to be more correct on Python 3 This code appears to be on the wrong side of the law in Python 2, at least some of the time. In Python 3, it's definitely wrong in places, but fortunately that's easy to fix. Differential Revision: https://phab.mercurial-scm.org/D3953 diff -r b95538a21613 -r 858fe9625dab mercurial/mail.py --- a/mercurial/mail.py Mon Jul 16 14:14:27 2018 -0400 +++ b/mercurial/mail.py Mon Jul 16 17:48:03 2018 -0400 @@ -252,10 +252,27 @@ 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) @@ -263,7 +280,7 @@ continue for ocs in sendcharsets: try: - return u.encode(ocs), ocs + return u.encode(pycompat.sysstr(ocs)), ocs except UnicodeEncodeError: pass except LookupError: