Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[3.11] gh-77749: Fix inconsistent behavior of non-ASCII handling in EmailPolicy.fold() (GH-6986) #114607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 8 additions & 1 deletion Lib/email/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,15 @@ def _fold(self, name, value, refold_binary=False):
self.refold_source == 'long' and
(lines and len(lines[0])+len(name)+2 > maxlen or
any(len(x) > maxlen for x in lines[1:])))
if refold or refold_binary and _has_surrogates(value):

if not refold:
if not self.utf8:
refold = not value.isascii()
elif refold_binary:
refold = _has_surrogates(value)
if refold:
return self.header_factory(name, ''.join(lines)).fold(policy=self)

return name + ': ' + self.linesep.join(lines) + self.linesep


Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_email/test_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,23 @@ def test_policy_addition(self):
for attr, value in expected.items():
self.assertEqual(getattr(added, attr), value)

def test_fold_utf8(self):
expected_ascii = 'Subject: =?utf-8?q?=C3=A1?=\n'
expected_utf8 = 'Subject: á\n'

msg = email.message.EmailMessage()
s = 'á'
msg['Subject'] = s

p_ascii = email.policy.default.clone()
p_utf8 = email.policy.default.clone(utf8=True)

self.assertEqual(p_ascii.fold('Subject', msg['Subject']), expected_ascii)
self.assertEqual(p_utf8.fold('Subject', msg['Subject']), expected_utf8)

self.assertEqual(p_ascii.fold('Subject', s), expected_ascii)
self.assertEqual(p_utf8.fold('Subject', s), expected_utf8)

def test_fold_zero_max_line_length(self):
expected = 'Subject: =?utf-8?q?=C3=A1?=\n'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:meth:`email.policy.EmailPolicy.fold` now always encodes non-ASCII characters
in headers if :attr:`~email.policy.EmailPolicy.utf8` is false.