From 53382b4d08b27e53db882c925cf48660f74d09d2 Mon Sep 17 00:00:00 2001 From: Mark Sapiro Date: Sat, 30 May 2020 12:47:01 -0700 Subject: [PATCH 1/3] Allow email.contextmanager set_content() to set a null string. --- Lib/email/contentmanager.py | 3 ++- Lib/test/test_email/test_contentmanager.py | 13 +++++++++++++ .../2020-05-30-12-44-29.bpo-39384.Iqxy3q.rst | 1 + 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2020-05-30-12-44-29.bpo-39384.Iqxy3q.rst diff --git a/Lib/email/contentmanager.py b/Lib/email/contentmanager.py index 2b4b8757f46f622..fc8f2ba5e88e857 100644 --- a/Lib/email/contentmanager.py +++ b/Lib/email/contentmanager.py @@ -146,7 +146,8 @@ def embedded_body(lines): return linesep.join(lines) + linesep 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 (len(lines) == 0 or + max(len(x) for x in lines) <= 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 64dca2d017e629f..f4f6bb715acdce6 100644 --- a/Lib/test/test_email/test_contentmanager.py +++ b/Lib/test/test_email/test_contentmanager.py @@ -303,6 +303,19 @@ def test_set_text_plain(self): 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 = "

Simple message.

\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 000000000000000..482ae624da079de --- /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. From 1475b354cab040d4ae3a8b59c6b90bf15eab32dc Mon Sep 17 00:00:00 2001 From: Mark Sapiro Date: Fri, 5 Jun 2020 16:24:26 -0700 Subject: [PATCH 2/3] Use default arg to max instead of a separate condition. --- Lib/email/contentmanager.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/email/contentmanager.py b/Lib/email/contentmanager.py index fc8f2ba5e88e857..500182b65e23a0f 100644 --- a/Lib/email/contentmanager.py +++ b/Lib/email/contentmanager.py @@ -146,8 +146,7 @@ def embedded_body(lines): return linesep.join(lines) + linesep def normal_body(lines): return b'\n'.join(lines) + b'\n' if cte==None: # Use heuristics to decide on the "best" encoding. - if (len(lines) == 0 or - 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: From deb7d25ee47393668e932b5109dae48f0a5b3b05 Mon Sep 17 00:00:00 2001 From: Mark Sapiro Date: Fri, 5 Jun 2020 16:52:55 -0700 Subject: [PATCH 3/3] Fix pep8 lapse. --- Lib/email/contentmanager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/email/contentmanager.py b/Lib/email/contentmanager.py index 500182b65e23a0f..b91fb0e5bca7a88 100644 --- a/Lib/email/contentmanager.py +++ b/Lib/email/contentmanager.py @@ -146,7 +146,7 @@ def embedded_body(lines): return linesep.join(lines) + linesep 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), default = 0) <= 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: