bpo-33524: Fix the folding of email header when max_line_length is 0 or None (#13391)
authorAbhilash Raj <maxking@users.noreply.github.com>
Fri, 17 May 2019 19:28:44 +0000 (12:28 -0700)
committerR. David Murray <rdmurray@bitdance.com>
Fri, 17 May 2019 19:28:44 +0000 (15:28 -0400)
and there are non-ascii characters in the header.

Lib/email/_header_value_parser.py
Lib/email/policy.py
Lib/test/test_email/test_policy.py
Misc/NEWS.d/next/Library/2019-05-17-11-44-21.bpo-33524.8y_xUU.rst [new file with mode: 0644]

index 60d0d32059f04a6591b54be945a719d30e2a7f4b..649f1539fa02ab0934d55ff9be4eee95ab97fa2b 100644 (file)
@@ -68,6 +68,7 @@ XXX: provide complete list of token types.
 """
 
 import re
+import sys
 import urllib   # For urllib.parse.unquote
 from string import hexdigits
 from operator import itemgetter
@@ -2590,7 +2591,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
index 5131311ac5ef762080a66b6ca03fc1945448e8a5..611deb50bb5290e193942236f691be6979a1f73c 100644 (file)
@@ -3,6 +3,7 @@ code that adds all the email6 features.
 """
 
 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 @@ class EmailPolicy(Policy):
     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
index c2c437e6ac269520a1c4a72e79f759f184ece4dd..0aea934df4348efa7d993b5d45dc5e4375dcfd6b 100644 (file)
@@ -1,4 +1,5 @@
 import io
+import sys
 import types
 import textwrap
 import unittest
@@ -134,6 +135,18 @@ class PolicyAPITests(unittest.TestCase):
         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 (file)
index 0000000..bfeab72
--- /dev/null
@@ -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).