Skip to content

Commit 4762b36

Browse files
[3.11] gh-118643: Fix AttributeError in the email module (GH-119099) (#119393)
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. (cherry picked from commit 858b9e8)
1 parent ba43157 commit 4762b36

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

Lib/email/_header_value_parser.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,7 @@ class _InvalidEwError(errors.HeaderParseError):
950950
DOT = ValueTerminal('.', 'dot')
951951
ListSeparator = ValueTerminal(',', 'list-separator')
952952
ListSeparator.as_ew_allowed = False
953+
ListSeparator.syntactic_break = False
953954
RouteComponentMarker = ValueTerminal('@', 'route-component-marker')
954955

955956
#
@@ -2821,7 +2822,9 @@ def _refold_parse_tree(parse_tree, *, policy):
28212822
if not hasattr(part, 'encode'):
28222823
# It's not a Terminal, do each piece individually.
28232824
parts = list(part) + parts
2824-
else:
2825+
want_encoding = False
2826+
continue
2827+
elif part.as_ew_allowed:
28252828
# It's a terminal, wrap it as an encoded word, possibly
28262829
# combining it with previously encoded words if allowed.
28272830
if (last_ew is not None and
@@ -2832,8 +2835,15 @@ def _refold_parse_tree(parse_tree, *, policy):
28322835
last_ew = _fold_as_ew(tstr, lines, maxlen, last_ew,
28332836
part.ew_combine_allowed, charset)
28342837
last_charset = charset
2835-
want_encoding = False
2836-
continue
2838+
want_encoding = False
2839+
continue
2840+
else:
2841+
# It's a terminal which should be kept non-encoded
2842+
# (e.g. a ListSeparator).
2843+
last_ew = None
2844+
want_encoding = False
2845+
# fall through
2846+
28372847
if len(tstr) <= maxlen - len(lines[-1]):
28382848
lines[-1] += tstr
28392849
continue

Lib/test/test_email/test__header_value_parser.py

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

29882988
def test_address_list_with_list_separator_after_fold(self):
2989-
to = '0123456789' * 8 + '@foo, ä <foo@bar>'
2989+
a = 'x' * 66 + '@example.com'
2990+
to = f'{a}, "Hübsch Kaktus" <[email protected]>'
29902991
self._test(parser.get_address_list(to)[0],
2991-
'0123456789' * 8 + '@foo,\n =?utf-8?q?=C3=A4?= <foo@bar>\n')
2992+
f'{a},\n =?utf-8?q?H=C3=BCbsch?= Kaktus <[email protected]>\n')
2993+
2994+
a = '.' * 79
2995+
to = f'"{a}" <[email protected]>, "Hübsch Kaktus" <[email protected]>'
2996+
self._test(parser.get_address_list(to)[0],
2997+
f'{a}\n'
2998+
' <[email protected]>, =?utf-8?q?H=C3=BCbsch?= Kaktus '
2999+
29923000

29933001
# XXX Need tests with comments on various sides of a unicode token,
29943002
# 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)