Skip to content

Commit 2b6f035

Browse files
committed
gh-160302: Don't encode the payload twice in Message.set_payload()
When the input and output charsets differ, as for euc-jp and shift_jis, which are sent as iso-2022-jp, set_payload() encoded the text and set_charset() then encoded the result again. That only worked because the iso2022_jp encoder let ESC through. Leave the conversion to set_charset().
1 parent e7e54fb commit 2b6f035

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

‎Lib/email/message.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,11 @@ def set_payload(self, payload, charset=None):
352352
return
353353
if not isinstance(charset, Charset):
354354
charset = Charset(charset)
355-
payload = payload.encode(charset.output_charset, 'surrogateescape')
355+
# If the input and output charsets differ, set_charset()
356+
# converts the text, so don't encode it twice.
357+
if charset == charset.get_output_charset():
358+
payload = payload.encode(charset.output_charset,
359+
'surrogateescape')
356360
if hasattr(payload, 'decode'):
357361
self._payload = payload.decode('ascii', 'surrogateescape')
358362
else:

0 commit comments

Comments
 (0)