gh-77749: Fix inconsistent behavior of non-ASCII handling in EmailPolicy.fold() (GH-6986)

It now always encodes non-ASCII characters in headers if utf8 is false.

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Rito Takeuchi 2024-01-27 00:19:41 +09:00 committed by GitHub
parent 0bd8297a22
commit 504334c7be
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 1 deletions

View File

@ -210,8 +210,15 @@ class EmailPolicy(Policy):
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

View File

@ -135,6 +135,23 @@ class PolicyAPITests(unittest.TestCase):
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'

View File

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