]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-41402: Fix email ContentManager calling .encode() on bytes (GH-21631) (GH-27687)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 9 Aug 2021 22:34:58 +0000 (15:34 -0700)
committerGitHub <noreply@github.com>
Mon, 9 Aug 2021 22:34:58 +0000 (00:34 +0200)
(cherry picked from commit b33186bc43bb5aaf652dd9d093a08fdde796d499)

Co-authored-by: Johannes Reiff <mail@jreiff.de>
Lib/email/contentmanager.py
Lib/test/test_email/test_contentmanager.py
Misc/NEWS.d/next/Library/2020-07-26-18-17-30.bpo-41402.YRkVkp.rst [new file with mode: 0644]

index b91fb0e5bca7a88b25d9738fbef8a24cc799ec32..3cf62dc8621cd9da3f0a49de740fca1d88c887e8 100644 (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)
index f4f6bb715acdce68f2a2f79fab7ad2d0b81be3d7..694cef4ba7e41302f6185f87ca7e6611b130642c 100644 (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',)),
diff --git a/Misc/NEWS.d/next/Library/2020-07-26-18-17-30.bpo-41402.YRkVkp.rst b/Misc/NEWS.d/next/Library/2020-07-26-18-17-30.bpo-41402.YRkVkp.rst
new file mode 100644 (file)
index 0000000..45585a4
--- /dev/null
@@ -0,0 +1 @@
+Fix :meth:`email.message.EmailMessage.set_content` when called with binary data and ``7bit`` content transfer encoding.