From 5386aaf07835889e90fb33e95b6d37197f8cfea0 Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Fri, 17 May 2019 13:47:12 -0700 Subject: [PATCH] bpo-33524: Fix the folding of email header when max_line_length is 0 or None (GH-13391) and there are non-ascii characters in the header. (cherry picked from commit feac6cd7753425fba006e97e2d9b74a0c0c75894) Co-authored-by: Abhilash Raj --- Lib/email/_header_value_parser.py | 3 ++- Lib/email/policy.py | 3 ++- Lib/test/test_email/test_policy.py | 13 +++++++++++++ .../2019-05-17-11-44-21.bpo-33524.8y_xUU.rst | 3 +++ 4 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2019-05-17-11-44-21.bpo-33524.8y_xUU.rst diff --git a/Lib/email/_header_value_parser.py b/Lib/email/_header_value_parser.py index faf8755171e3a8..958ef5018c2599 100644 --- a/Lib/email/_header_value_parser.py +++ b/Lib/email/_header_value_parser.py @@ -68,6 +68,7 @@ """ import re +import sys import urllib # For urllib.parse.unquote from string import hexdigits from collections import OrderedDict @@ -2591,7 +2592,7 @@ def _refold_parse_tree(parse_tree, *, policy): """ # max_line_length 0/None means no limit, ie: infinitely long. - maxlen = policy.max_line_length or float("+inf") + maxlen = policy.max_line_length or sys.maxsize encoding = 'utf-8' if policy.utf8 else 'us-ascii' lines = [''] last_ew = None diff --git a/Lib/email/policy.py b/Lib/email/policy.py index 5131311ac5ef76..611deb50bb5290 100644 --- a/Lib/email/policy.py +++ b/Lib/email/policy.py @@ -3,6 +3,7 @@ """ import re +import sys from email._policybase import Policy, Compat32, compat32, _extend_docstrings from email.utils import _has_surrogates from email.headerregistry import HeaderRegistry as HeaderRegistry @@ -203,7 +204,7 @@ def fold_binary(self, name, value): def _fold(self, name, value, refold_binary=False): if hasattr(value, 'name'): return value.fold(policy=self) - maxlen = self.max_line_length if self.max_line_length else float('inf') + maxlen = self.max_line_length if self.max_line_length else sys.maxsize lines = value.splitlines() refold = (self.refold_source == 'all' or self.refold_source == 'long' and diff --git a/Lib/test/test_email/test_policy.py b/Lib/test/test_email/test_policy.py index c2c437e6ac2695..0aea934df4348e 100644 --- a/Lib/test/test_email/test_policy.py +++ b/Lib/test/test_email/test_policy.py @@ -1,4 +1,5 @@ import io +import sys import types import textwrap import unittest @@ -134,6 +135,18 @@ def test_policy_addition(self): for attr, value in expected.items(): self.assertEqual(getattr(added, attr), value) + def test_fold_zero_max_line_length(self): + expected = 'Subject: =?utf-8?q?=C3=A1?=\n' + + msg = email.message.EmailMessage() + msg['Subject'] = 'รก' + + p1 = email.policy.default.clone(max_line_length=0) + p2 = email.policy.default.clone(max_line_length=None) + + self.assertEqual(p1.fold('Subject', msg['Subject']), expected) + self.assertEqual(p2.fold('Subject', msg['Subject']), expected) + def test_register_defect(self): class Dummy: def __init__(self): diff --git a/Misc/NEWS.d/next/Library/2019-05-17-11-44-21.bpo-33524.8y_xUU.rst b/Misc/NEWS.d/next/Library/2019-05-17-11-44-21.bpo-33524.8y_xUU.rst new file mode 100644 index 00000000000000..bfeab7231bdb79 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-05-17-11-44-21.bpo-33524.8y_xUU.rst @@ -0,0 +1,3 @@ +Fix the folding of email header when the max_line_length is 0 or None and the +header contains non-ascii characters. Contributed by Licht Takeuchi +(@Licht-T).