gh-135307: Fix email error when policy max_line_length is set to 0 or… · python/cpython@6d45cd8 · GitHub
Skip to content

Commit 6d45cd8

Browse files
authored
gh-135307: Fix email error when policy max_line_length is set to 0 or None (#135367)
RDM: Like the change made in a earlier PR to the folder, we can/must use 'maxlen' as a stand in for 'unlimited' when computing line lengths when max_line_length is 0 or None; otherwise the computation results in a traceback.
1 parent 173cc53 commit 6d45cd8

3 files changed

Lines changed: 35 additions & 5 deletions

File tree

Lib/email/contentmanager.py

Lines changed: 7 additions & 5 deletions

Lib/test/test_email/test_message.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,32 @@ def test_folding_with_long_nospace_http_policy_1(self):
10041004
parsed_msg = message_from_bytes(m.as_bytes(), policy=policy.default)
10051005
self.assertEqual(parsed_msg['Message-ID'], m['Message-ID'])
10061006

1007+
def test_no_wrapping_max_line_length(self):
1008+
# Test that falsey 'max_line_length' are converted to sys.maxsize.
1009+
for n in [0, None]:
1010+
with self.subTest(max_line_length=n):
1011+
self.do_test_no_wrapping_max_line_length(n)
1012+
1013+
def do_test_no_wrapping_max_line_length(self, falsey):
1014+
self.assertFalse(falsey)
1015+
pol = policy.default.clone(max_line_length=falsey)
1016+
subj = "S" * 100
1017+
body = "B" * 100
1018+
msg = EmailMessage(policy=pol)
1019+
msg["From"] = "a@ex.com"
1020+
msg["To"] = "b@ex.com"
1021+
msg["Subject"] = subj
1022+
msg.set_content(body)
1023+
1024+
raw = msg.as_bytes()
1025+
self.assertNotIn(b"=\n", raw,
1026+
"Found fold indicator; wrapping not disabled")
1027+
1028+
parsed = message_from_bytes(raw, policy=policy.default)
1029+
self.assertEqual(parsed["Subject"], subj)
1030+
parsed_body = parsed.get_body().get_content().rstrip('\n')
1031+
self.assertEqual(parsed_body, body)
1032+
10071033
def test_invalid_header_names(self):
10081034
invalid_headers = [
10091035
('Invalid Header', 'contains space'),
Lines changed: 2 additions & 0 deletions

0 commit comments

Comments
 (0)