##// END OF EJS Templates
mail: fix _encode to be more correct on Python 3...
Augie Fackler -
r39058:858fe962 default
parent child Browse files
Show More
@@ -252,10 +252,27 b' def _encode(ui, s, charsets):'
252 252 order. Tries both encoding and fallbackencoding for input. Only as
253 253 last resort send as is in fake ascii.
254 254 Caveat: Do not use for mail parts containing patches!'''
255 sendcharsets = charsets or _charsets(ui)
256 if not isinstance(s, bytes):
257 # We have unicode data, which we need to try and encode to
258 # some reasonable-ish encoding. Try the encodings the user
259 # wants, and fall back to garbage-in-ascii.
260 for ocs in sendcharsets:
261 try:
262 return s.encode(pycompat.sysstr(ocs)), ocs
263 except UnicodeEncodeError:
264 pass
265 except LookupError:
266 ui.warn(_('ignoring invalid sendcharset: %s\n') % ocs)
267 else:
268 # Everything failed, ascii-armor what we've got and send it.
269 return s.encode('ascii', 'backslashreplace')
270 # We have a bytes of unknown encoding. We'll try and guess a valid
271 # encoding, falling back to pretending we had ascii even though we
272 # know that's wrong.
255 273 try:
256 274 s.decode('ascii')
257 275 except UnicodeDecodeError:
258 sendcharsets = charsets or _charsets(ui)
259 276 for ics in (encoding.encoding, encoding.fallbackencoding):
260 277 try:
261 278 u = s.decode(ics)
@@ -263,7 +280,7 b' def _encode(ui, s, charsets):'
263 280 continue
264 281 for ocs in sendcharsets:
265 282 try:
266 return u.encode(ocs), ocs
283 return u.encode(pycompat.sysstr(ocs)), ocs
267 284 except UnicodeEncodeError:
268 285 pass
269 286 except LookupError:
General Comments 0
You need to be logged in to leave comments. Login now