From ef7eeba5b920c451c381502a7061d369945a4778 Mon Sep 17 00:00:00 2001 From: Johannes Reiff Date: Sun, 26 Jul 2020 20:04:10 +0200 Subject: [PATCH 1/3] bpo-41402: Fix email ContentManager calling .encode() on bytes --- Lib/email/contentmanager.py | 4 +--- Lib/test/test_email/test_contentmanager.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Lib/email/contentmanager.py b/Lib/email/contentmanager.py index b91fb0e5bca7a8..3cf62dc8621cd9 100644 --- a/Lib/email/contentmanager.py +++ b/Lib/email/contentmanager.py @@ -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) diff --git a/Lib/test/test_email/test_contentmanager.py b/Lib/test/test_email/test_contentmanager.py index f4f6bb715acdce..694cef4ba7e413 100644 --- a/Lib/test/test_email/test_contentmanager.py +++ b/Lib/test/test_email/test_contentmanager.py @@ -776,6 +776,18 @@ def test_set_non_ascii_filename(self): 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',)), From d23010c5a4b9f45fc39c9e14a6eeb153183bba69 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 26 Jul 2020 18:17:31 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2020-07-26-18-17-30.bpo-41402.YRkVkp.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2020-07-26-18-17-30.bpo-41402.YRkVkp.rst 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 index 00000000000000..263358eaa73fbb --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-07-26-18-17-30.bpo-41402.YRkVkp.rst @@ -0,0 +1 @@ +Fix ``email.message.EmailMessage.set_content()`` when called with binary data and ``7bit`` content transfer encoding. \ No newline at end of file From 5d278aae9deeeafce385b57bb433ee8aa0b7a969 Mon Sep 17 00:00:00 2001 From: Johannes Reiff Date: Fri, 31 Jul 2020 08:27:41 +0200 Subject: [PATCH 3/3] bpo-41402: Fix reference to method in news snippet --- .../next/Library/2020-07-26-18-17-30.bpo-41402.YRkVkp.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 263358eaa73fbb..45585a469e7247 100644 --- 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 @@ -1 +1 @@ -Fix ``email.message.EmailMessage.set_content()`` when called with binary data and ``7bit`` content transfer encoding. \ No newline at end of file +Fix :meth:`email.message.EmailMessage.set_content` when called with binary data and ``7bit`` content transfer encoding.