Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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/_header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2792,6 +2792,9 @@ 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]
# gh-142006: if the line is now empty, remove it entirely.
if not lines[-1]:
lines.pop()
return wsp

def _refold_parse_tree(parse_tree, *, policy):
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_email/test__header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3255,5 +3255,18 @@ def test_long_filename_attachment(self):
" filename*1*=_TEST_TES.txt\n",
)

def test_fold_unfoldable_element_stealing_whitespace(self):
# gh-142006: When an element is too long to fit on the current line
# the previous line's trailing whitespace
policy = self.policy.clone(max_line_length=10)

# "a," fits. The space after it should wrap to the next line.
# The "long" part (20 chars) forces the wrap because 20 > 10.
text = "a, " + ("b" * 20)
expected = "a,\n " + ("b" * 20) + "\n"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd drop the blank lines. They don't add anything and most other tests don't have internal blank lines.

token = parser.get_address_list(text)[0]
self._test(token, expected, policy=policy)

if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :exc:`email.errors.HeaderWriteError` in :mod:`email.policy.default` which incorrectly added double newlines when folding lines containing unfoldable syntactic elements wider than max_line_len.
Loading