[3.12] gh-118643: Fix AttributeError in the email module (GH-119099) (GH-119390)

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 858b9e85fc)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Miss Islington (bot) 2024-05-22 13:08:05 +02:00 committed by GitHub
parent 2c7e9de98b
commit 8c96850161
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 24 additions and 5 deletions

View file

@ -956,6 +956,7 @@ class _InvalidEwError(errors.HeaderParseError):
DOT = ValueTerminal('.', 'dot') DOT = ValueTerminal('.', 'dot')
ListSeparator = ValueTerminal(',', 'list-separator') ListSeparator = ValueTerminal(',', 'list-separator')
ListSeparator.as_ew_allowed = False ListSeparator.as_ew_allowed = False
ListSeparator.syntactic_break = False
RouteComponentMarker = ValueTerminal('@', 'route-component-marker') RouteComponentMarker = ValueTerminal('@', 'route-component-marker')
# #
@ -2844,7 +2845,9 @@ def _refold_parse_tree(parse_tree, *, policy):
if not hasattr(part, 'encode'): if not hasattr(part, 'encode'):
# It's not a Terminal, do each piece individually. # It's not a Terminal, do each piece individually.
parts = list(part) + parts 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 # It's a terminal, wrap it as an encoded word, possibly
# combining it with previously encoded words if allowed. # combining it with previously encoded words if allowed.
if (last_ew is not None and if (last_ew is not None and
@ -2858,8 +2861,14 @@ def _refold_parse_tree(parse_tree, *, policy):
# so clear it now. # so clear it now.
leading_whitespace = '' leading_whitespace = ''
last_charset = charset last_charset = charset
want_encoding = False want_encoding = False
continue continue
else:
# It's a terminal which should be kept non-encoded
# (e.g. a ListSeparator).
last_ew = None
want_encoding = False
# fall through
if len(tstr) <= maxlen - len(lines[-1]): if len(tstr) <= maxlen - len(lines[-1]):
lines[-1] += tstr lines[-1] += tstr

View file

@ -3077,9 +3077,17 @@ class TestFolding(TestEmailBase):
' =?utf-8?q?bei=C3=9Ft_bei=C3=9Ft?= <biter@example.com>\n') ' =?utf-8?q?bei=C3=9Ft_bei=C3=9Ft?= <biter@example.com>\n')
def test_address_list_with_list_separator_after_fold(self): 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], 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, # XXX Need tests with comments on various sides of a unicode token,
# and with unicode tokens in the comments. Spaces inside the quotes # and with unicode tokens in the comments. Spaces inside the quotes

View file

@ -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.