Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
gh-118643: Fix AttributeError in the email module
Fix regression introduced in gh-100884: AttributeError when re-fold a long
address list.

Also fix more cases of incorrect encoding of the address separator in the
address list missed in gh-100884.
  • Loading branch information
serhiy-storchaka committed May 16, 2024
commit ad85b834dda671a8ff7942258cece9275bb0817b
15 changes: 12 additions & 3 deletions Lib/email/_header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,7 @@ class _InvalidEwError(errors.HeaderParseError):
DOT = ValueTerminal('.', 'dot')
ListSeparator = ValueTerminal(',', 'list-separator')
ListSeparator.as_ew_allowed = False
ListSeparator.syntactic_break = False
RouteComponentMarker = ValueTerminal('@', 'route-component-marker')

#
Expand Down Expand Up @@ -2838,7 +2839,9 @@ def _refold_parse_tree(parse_tree, *, policy):
if not hasattr(part, 'encode'):
# It's not a Terminal, do each piece individually.
parts = list(part) + parts
else:
want_encoding = False
continue
elif part.as_ew_allowed:
# It's a terminal, wrap it as an encoded word, possibly
# combining it with previously encoded words if allowed.
if (last_ew is not None and
Expand All @@ -2849,8 +2852,14 @@ def _refold_parse_tree(parse_tree, *, policy):
last_ew = _fold_as_ew(tstr, lines, maxlen, last_ew,
part.ew_combine_allowed, charset)
last_charset = charset
want_encoding = False
continue
want_encoding = False
continue
else:
# It's a terminal which should be kept non-encoded
# (e.g. a ListSeparator).
last_ew = None
want_encoding = False
pass
Comment thread
serhiy-storchaka marked this conversation as resolved.
Outdated
if len(tstr) <= maxlen - len(lines[-1]):
lines[-1] += tstr
continue
Expand Down
12 changes: 10 additions & 2 deletions Lib/test/test_email/test__header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3077,9 +3077,17 @@ def test_address_list_with_unicode_names_in_quotes(self):
' =?utf-8?q?bei=C3=9Ft_bei=C3=9Ft?= <biter@example.com>\n')

def test_address_list_with_list_separator_after_fold(self):
to = '0123456789' * 8 + '@foo, ä <foo@bar>'
a = 'x' * 66 + '@example.com'
to = f'{a}, "Hübsch Kaktus" <beautiful@example.com>'
self._test(parser.get_address_list(to)[0],
'0123456789' * 8 + '@foo,\n =?utf-8?q?=C3=A4?= <foo@bar>\n')
f'{a},\n =?utf-8?q?H=C3=BCbsch?= Kaktus <beautiful@example.com>\n')

a = '.' * 79
to = f'"{a}" <xyz@example.com>, "Hübsch Kaktus" <beautiful@example.com>'
self._test(parser.get_address_list(to)[0],
f'{a}\n'
' <xyz@example.com>, =?utf-8?q?H=C3=BCbsch?= Kaktus '
'<beautiful@example.com>\n')

# XXX Need tests with comments on various sides of a unicode token,
# and with unicode tokens in the comments. Spaces inside the quotes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix an AttributeError in the :mod:`email` module when re-fold a long address
list. Also fix more cases of incorrect encoding of the address separator in the address list.