From 48d185fcc42374cd7158c3e28f29ff4b38b03869 Mon Sep 17 00:00:00 2001 From: Karthikeyan Singaravelan Date: Mon, 6 Jul 2020 12:49:31 +0000 Subject: [PATCH 1/2] bpo-41206: Fix EmailMessage.set_content for empty content. --- Lib/email/contentmanager.py | 3 +++ Lib/test/test_email/test_contentmanager.py | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/Lib/email/contentmanager.py b/Lib/email/contentmanager.py index 2b4b8757f46f62..a0046f880e274e 100644 --- a/Lib/email/contentmanager.py +++ b/Lib/email/contentmanager.py @@ -146,6 +146,9 @@ 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 not lines: + return '7bit', normal_body(lines).decode('ascii') + if max(len(x) for x in lines) <= policy.max_line_length: try: return '7bit', normal_body(lines).decode('ascii') diff --git a/Lib/test/test_email/test_contentmanager.py b/Lib/test/test_email/test_contentmanager.py index 64dca2d017e629..0eca6d0dfe34d0 100644 --- a/Lib/test/test_email/test_contentmanager.py +++ b/Lib/test/test_email/test_contentmanager.py @@ -344,6 +344,19 @@ def test_set_text_plain_long_line_heuristics(self): self.assertEqual(m.get_payload(decode=True).decode('utf-8'), content) self.assertEqual(m.get_content(), content) + def test_set_text_plain_empty_content_heuristics(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_short_line_minimal_non_ascii_heuristics(self): m = self._make_message() content = "et là il est monté sur moi et il commence à m'éto.\n" From 2c041a39ca3176f29956b4e0c88c326cd0bd9aeb Mon Sep 17 00:00:00 2001 From: Karthikeyan Singaravelan Date: Mon, 6 Jul 2020 12:56:26 +0000 Subject: [PATCH 2/2] bpo-41206: Add NEWS entry. --- .../next/Library/2020-07-06-12-56-16.bpo-41206.i-tXA-.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2020-07-06-12-56-16.bpo-41206.i-tXA-.rst diff --git a/Misc/NEWS.d/next/Library/2020-07-06-12-56-16.bpo-41206.i-tXA-.rst b/Misc/NEWS.d/next/Library/2020-07-06-12-56-16.bpo-41206.i-tXA-.rst new file mode 100644 index 00000000000000..2f2c018f6b5cc5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-07-06-12-56-16.bpo-41206.i-tXA-.rst @@ -0,0 +1,2 @@ +Fix :exc:`ValueError` caused due to empty content passed to +:meth:`EmailMessage.set_content`.