Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit feac6cd

Browse files
maxkingbitdancer
authored andcommittedMay 17, 2019
bpo-33524: Fix the folding of email header when max_line_length is 0 or None (python#13391)
and there are non-ascii characters in the header.
1 parent cbe72d8 commit feac6cd

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed
 

‎Lib/email/_header_value_parser.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"""
6969

7070
import re
71+
import sys
7172
import urllib # For urllib.parse.unquote
7273
from string import hexdigits
7374
from operator import itemgetter
@@ -2590,7 +2591,7 @@ def _refold_parse_tree(parse_tree, *, policy):
25902591
25912592
"""
25922593
# max_line_length 0/None means no limit, ie: infinitely long.
2593-
maxlen = policy.max_line_length or float("+inf")
2594+
maxlen = policy.max_line_length or sys.maxsize
25942595
encoding = 'utf-8' if policy.utf8 else 'us-ascii'
25952596
lines = ['']
25962597
last_ew = None

‎Lib/email/policy.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import re
6+
import sys
67
from email._policybase import Policy, Compat32, compat32, _extend_docstrings
78
from email.utils import _has_surrogates
89
from email.headerregistry import HeaderRegistry as HeaderRegistry
@@ -203,7 +204,7 @@ def fold_binary(self, name, value):
203204
def _fold(self, name, value, refold_binary=False):
204205
if hasattr(value, 'name'):
205206
return value.fold(policy=self)
206-
maxlen = self.max_line_length if self.max_line_length else float('inf')
207+
maxlen = self.max_line_length if self.max_line_length else sys.maxsize
207208
lines = value.splitlines()
208209
refold = (self.refold_source == 'all' or
209210
self.refold_source == 'long' and

‎Lib/test/test_email/test_policy.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import io
2+
import sys
23
import types
34
import textwrap
45
import unittest
@@ -134,6 +135,18 @@ def test_policy_addition(self):
134135
for attr, value in expected.items():
135136
self.assertEqual(getattr(added, attr), value)
136137

138+
def test_fold_zero_max_line_length(self):
139+
expected = 'Subject: =?utf-8?q?=C3=A1?=\n'
140+
141+
msg = email.message.EmailMessage()
142+
msg['Subject'] = 'á'
143+
144+
p1 = email.policy.default.clone(max_line_length=0)
145+
p2 = email.policy.default.clone(max_line_length=None)
146+
147+
self.assertEqual(p1.fold('Subject', msg['Subject']), expected)
148+
self.assertEqual(p2.fold('Subject', msg['Subject']), expected)
149+
137150
def test_register_defect(self):
138151
class Dummy:
139152
def __init__(self):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix the folding of email header when the max_line_length is 0 or None and the
2+
header contains non-ascii characters. Contributed by Licht Takeuchi
3+
(@Licht-T).

0 commit comments

Comments
 (0)
Please sign in to comment.