Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions Lib/email/_header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2792,6 +2792,10 @@ def _steal_trailing_WSP_if_exists(lines):
if lines and lines[-1] and lines[-1][-1] in WSP:
wsp = lines[-1][-1]
lines[-1] = lines[-1][:-1]
# FIX: If the line is now empty (it was only a space), remove it entirely
# to prevent creating an empty line (double newline) in the output.
if not lines[-1]:
lines.pop()
return wsp

def _refold_parse_tree(parse_tree, *, policy):
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_email/test_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,20 @@ def test_fold_zero_max_line_length(self):
self.assertEqual(p1.fold('Subject', msg['Subject']), expected)
self.assertEqual(p2.fold('Subject', msg['Subject']), expected)

def test_fold_address_list_with_stolen_whitespace(self):
long_address = (
"loooooooooooooooooooooooooooooooooooong@dooooooooooooomainname.example.com, "
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.example.com"
)
msg = email.message_from_string(f"To: {long_address}\n\n", policy=email.policy.default)

# This should not raise HeaderWriteError
output = msg.as_string()
# Verify the output format is correct and clean
self.assertIn(long_address.split(',')[0], output)
# Ensure header section doesn't have double newlines
self.assertNotIn('\n\n', output.split('\n\n')[0])

def test_register_defect(self):
class Dummy:
def __init__(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :exc:`email.errors.HeaderWriteError` in :mod:`email.policy.default` which created double newlines when folding long headers with list separators.
Loading