Skip to content

bpo-41206: Fix EmailMessage.set_content for empty content. #21349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Lib/email/contentmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_email/test_contentmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :exc:`ValueError` caused due to empty content passed to
:meth:`EmailMessage.set_content`.