Skip to content

Commit ad85b83

Browse files
pythongh-118643: Fix AttributeError in the email module
Fix regression introduced in pythongh-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 pythongh-100884.
1 parent 17cba55 commit ad85b83

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

Lib/email/_header_value_parser.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,7 @@ class _InvalidEwError(errors.HeaderParseError):
956956
DOT = ValueTerminal('.', 'dot')
957957
ListSeparator = ValueTerminal(',', 'list-separator')
958958
ListSeparator.as_ew_allowed = False
959+
ListSeparator.syntactic_break = False
959960
RouteComponentMarker = ValueTerminal('@', 'route-component-marker')
960961

961962
#
@@ -2838,7 +2839,9 @@ def _refold_parse_tree(parse_tree, *, policy):
28382839
if not hasattr(part, 'encode'):
28392840
# It's not a Terminal, do each piece individually.
28402841
parts = list(part) + parts
2841-
else:
2842+
want_encoding = False
2843+
continue
2844+
elif part.as_ew_allowed:
28422845
# It's a terminal, wrap it as an encoded word, possibly
28432846
# combining it with previously encoded words if allowed.
28442847
if (last_ew is not None and
@@ -2849,8 +2852,14 @@ def _refold_parse_tree(parse_tree, *, policy):
28492852
last_ew = _fold_as_ew(tstr, lines, maxlen, last_ew,
28502853
part.ew_combine_allowed, charset)
28512854
last_charset = charset
2852-
want_encoding = False
2853-
continue
2855+
want_encoding = False
2856+
continue
2857+
else:
2858+
# It's a terminal which should be kept non-encoded
2859+
# (e.g. a ListSeparator).
2860+
last_ew = None
2861+
want_encoding = False
2862+
pass
28542863
if len(tstr) <= maxlen - len(lines[-1]):
28552864
lines[-1] += tstr
28562865
continue

Lib/test/test_email/test__header_value_parser.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3077,9 +3077,17 @@ def test_address_list_with_unicode_names_in_quotes(self):
30773077
' =?utf-8?q?bei=C3=9Ft_bei=C3=9Ft?= <[email protected]>\n')
30783078

30793079
def test_address_list_with_list_separator_after_fold(self):
3080-
to = '0123456789' * 8 + '@foo, ä <foo@bar>'
3080+
a = 'x' * 66 + '@example.com'
3081+
to = f'{a}, "Hübsch Kaktus" <[email protected]>'
30813082
self._test(parser.get_address_list(to)[0],
3082-
'0123456789' * 8 + '@foo,\n =?utf-8?q?=C3=A4?= <foo@bar>\n')
3083+
f'{a},\n =?utf-8?q?H=C3=BCbsch?= Kaktus <[email protected]>\n')
3084+
3085+
a = '.' * 79
3086+
to = f'"{a}" <[email protected]>, "Hübsch Kaktus" <[email protected]>'
3087+
self._test(parser.get_address_list(to)[0],
3088+
f'{a}\n'
3089+
' <[email protected]>, =?utf-8?q?H=C3=BCbsch?= Kaktus '
3090+
30833091

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

0 commit comments

Comments
 (0)