diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2020-07-08 21:21:00 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-08 21:21:00 (GMT) |
commit | e68978978f49cfc489fa0f888281ce84460818c0 (patch) | |
tree | 3a2299bf88bb61a49131a32b877cb3d335acf399 | |
parent | fbd71f66843aea71c09656f17a196d29d5d484af (diff) | |
download | cpython-e68978978f49cfc489fa0f888281ce84460818c0.zip cpython-e68978978f49cfc489fa0f888281ce84460818c0.tar.gz cpython-e68978978f49cfc489fa0f888281ce84460818c0.tar.bz2 |
bpo-40597: Allow email.contextmanager set_content() to set a null string. (GH-20542)
(cherry picked from commit 4fa61a7732923f92de0f7830c12da48c4cec937f)
Co-authored-by: Mark Sapiro <mark@msapiro.net>
-rw-r--r-- | Lib/email/contentmanager.py | 2 | ||||
-rw-r--r-- | Lib/test/test_email/test_contentmanager.py | 13 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2020-05-30-12-44-29.bpo-39384.Iqxy3q.rst | 1 |
3 files changed, 15 insertions, 1 deletions
diff --git a/Lib/email/contentmanager.py b/Lib/email/contentmanager.py index 2b4b875..b91fb0e 100644 --- a/Lib/email/contentmanager.py +++ b/Lib/email/contentmanager.py @@ -146,7 +146,7 @@ def _encode_text(string, charset, cte, policy): def normal_body(lines): return b'\n'.join(lines) + b'\n' if cte==None: # Use heuristics to decide on the "best" encoding. - if max(len(x) for x in lines) <= policy.max_line_length: + if max((len(x) for x in lines), default=0) <= policy.max_line_length: try: return '7bit', normal_body(lines).decode('ascii') except UnicodeDecodeError: diff --git a/Lib/test/test_email/test_contentmanager.py b/Lib/test/test_email/test_contentmanager.py index 64dca2d..f4f6bb7 100644 --- a/Lib/test/test_email/test_contentmanager.py +++ b/Lib/test/test_email/test_contentmanager.py @@ -303,6 +303,19 @@ class TestRawDataManager(TestEmailBase): self.assertEqual(m.get_payload(decode=True).decode('utf-8'), content) self.assertEqual(m.get_content(), content) + def test_set_text_plain_null(self): + m = self._make_message() + content = '' + raw_data_manager.set_content(m, content) + self.assertEqual(str(m), textwrap.dedent("""\ + Content-Type: text/plain; charset="utf-8" + Content-Transfer-Encoding: 7bit + + + """)) + self.assertEqual(m.get_payload(decode=True).decode('utf-8'), '\n') + self.assertEqual(m.get_content(), '\n') + def test_set_text_html(self): m = self._make_message() content = "<p>Simple message.</p>\n" diff --git a/Misc/NEWS.d/next/Library/2020-05-30-12-44-29.bpo-39384.Iqxy3q.rst b/Misc/NEWS.d/next/Library/2020-05-30-12-44-29.bpo-39384.Iqxy3q.rst new file mode 100644 index 0000000..482ae62 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-05-30-12-44-29.bpo-39384.Iqxy3q.rst @@ -0,0 +1 @@ +Fixed email.contentmanager to allow set_content() to set a null string. |