Skip to content

Commit ebde4e8

Browse files
committed
Fix additional refolding-encoding edge case.
In this case, the higher level syntactic unit fit on the remainder of the line in un-encoded format, so the encoding check never happened. To fix this, we look inside the current unit to see if has anything that was originally encoded.
1 parent c45e661 commit ebde4e8

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

Lib/email/_header_value_parser.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ def comments(self):
162162
comments.extend(token.comments)
163163
return comments
164164

165+
def has_token_type(self, *token_types):
166+
if self.token_type in token_types:
167+
return True
168+
for t in self:
169+
if t.has_token_type(*token_types):
170+
return True
171+
return False
172+
165173
def fold(self, *, policy):
166174
return _refold_parse_tree(self, policy=policy)
167175

@@ -922,6 +930,9 @@ def pop_trailing_ws(self):
922930
def comments(self):
923931
return []
924932

933+
def has_token_type(self, *token_types):
934+
return self.token_type in token_types
935+
925936
def __getnewargs__(self):
926937
return(str(self), self.token_type)
927938

@@ -2813,7 +2824,7 @@ def _refold_parse_tree(parse_tree, *, policy):
28132824
continue
28142825
tstr = str(part)
28152826
if not want_encoding:
2816-
if part.token_type in ('ptext', 'vtext'):
2827+
if part.token_type == 'ptext' or part.has_token_type('vtext'):
28172828
# Encode if tstr contains special characters.
28182829
want_encoding = not SPECIALSNL.isdisjoint(tstr)
28192830
else:

Lib/test/test_email/test__header_value_parser.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3096,6 +3096,11 @@ def test_address_list_with_specials_in_encoded_word(self):
30963096
'A =?utf-8?q?v=C3=A9ry?= long name\n'
30973097
' containing =?utf-8?q?a=2C?= comma\n'
30983098
3099+
('=?utf-8?Q?a=2C=20123456789012345678901234567890123456?='
3100+
3101+
'=?utf-8?q?a=2C?=\n'
3102+
' 123456789012345678901234567890123456\n'
3103+
30993104
]
31003105
for (to, folded) in cases:
31013106
with self.subTest(to=to):

0 commit comments

Comments
 (0)