Skip to content

Commit

Permalink
bpo-40597: Allow email.contextmanager set_content() to set a null str…
Browse files Browse the repository at this point in the history
…ing. (GH-20542)

(cherry picked from commit 4fa61a7)

Co-authored-by: Mark Sapiro <mark@msapiro.net>
  • Loading branch information
miss-islington and msapiro committed Jul 8, 2020
1 parent c8b599f commit c1c5034
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Lib/email/contentmanager.py
Expand Up @@ -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) <= 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:
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_email/test_contentmanager.py
Expand Up @@ -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 = "<p>Simple message.</p>\n"
Expand Down
@@ -0,0 +1 @@
Fixed email.contentmanager to allow set_content() to set a null string.

0 comments on commit c1c5034

Please sign in to comment.