summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-01-26 16:30:50 (GMT)
committerGitHub <noreply@github.com>2024-01-26 16:30:50 (GMT)
commitc09eae3e3823c8374204a796dae5b861cc835c2c (patch)
tree7a1a913d28d4c283690b1e79881ba08510a610d2
parentc4a616352d813d78795eb8cfb939d36d509ef012 (diff)
downloadcpython-c09eae3e3823c8374204a796dae5b861cc835c2c.zip
cpython-c09eae3e3823c8374204a796dae5b861cc835c2c.tar.gz
cpython-c09eae3e3823c8374204a796dae5b861cc835c2c.tar.bz2
[3.11] gh-77749: Fix inconsistent behavior of non-ASCII handling in EmailPolicy.fold() (GH-6986) (GH-114607)
It now always encodes non-ASCII characters in headers if utf8 is false. (cherry picked from commit 504334c7be5a56237df2598d338cd494a42fca4c) Co-authored-by: Rito Takeuchi <licht-t@outlook.jp> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
-rw-r--r--Lib/email/policy.py9
-rw-r--r--Lib/test/test_email/test_policy.py17
-rw-r--r--Misc/NEWS.d/next/Library/2024-01-26-16-46-21.gh-issue-77749.NY_7TS.rst2
3 files changed, 27 insertions, 1 deletions
diff --git a/Lib/email/policy.py b/Lib/email/policy.py
index 611deb5..8816c84 100644
--- a/Lib/email/policy.py
+++ b/Lib/email/policy.py
@@ -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
diff --git a/Lib/test/test_email/test_policy.py b/Lib/test/test_email/test_policy.py
index e87c275..c6b9c80 100644
--- a/Lib/test/test_email/test_policy.py
+++ b/Lib/test/test_email/test_policy.py
@@ -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'
diff --git a/Misc/NEWS.d/next/Library/2024-01-26-16-46-21.gh-issue-77749.NY_7TS.rst b/Misc/NEWS.d/next/Library/2024-01-26-16-46-21.gh-issue-77749.NY_7TS.rst
new file mode 100644
index 0000000..f1c99c0
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-01-26-16-46-21.gh-issue-77749.NY_7TS.rst
@@ -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.