bpo-41402: Fix email ContentManager calling .encode() on bytes (GH-21631)

This commit is contained in:
Johannes Reiff 2021-08-09 18:45:41 +02:00 committed by GitHub
parent d097876111
commit b33186bc43
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 3 deletions

View file

@ -238,9 +238,7 @@ def set_bytes_content(msg, data, maintype, subtype, cte='base64',
data = binascii.b2a_qp(data, istext=False, header=False, quotetabs=True)
data = data.decode('ascii')
elif cte == '7bit':
# Make sure it really is only ASCII. The early warning here seems
# worth the overhead...if you care write your own content manager :).
data.encode('ascii')
data = data.decode('ascii')
elif cte in ('8bit', 'binary'):
data = data.decode('ascii', 'surrogateescape')
msg.set_payload(data)

View file

@ -776,6 +776,18 @@ class TestRawDataManager(TestEmailBase):
foo
""").encode('ascii'))
def test_set_content_bytes_cte_7bit(self):
m = self._make_message()
m.set_content(b'ASCII-only message.\n',
maintype='application', subtype='octet-stream', cte='7bit')
self.assertEqual(str(m), textwrap.dedent("""\
Content-Type: application/octet-stream
Content-Transfer-Encoding: 7bit
MIME-Version: 1.0
ASCII-only message.
"""))
content_object_params = {
'text_plain': ('content', ()),
'text_html': ('content', ('html',)),

View file

@ -0,0 +1 @@
Fix :meth:`email.message.EmailMessage.set_content` when called with binary data and ``7bit`` content transfer encoding.