diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2020-05-18 00:57:42 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-18 00:57:42 (GMT) |
commit | c1f1ddf30a595c2bfa3c06e54fb03fa212cd28b5 (patch) | |
tree | eaf147b7a9a36163917ece7bfbdefe50c9778d6c /Lib/email | |
parent | 7a3522d478d456b38ef5e647c21595904bea79df (diff) | |
download | cpython-c1f1ddf30a595c2bfa3c06e54fb03fa212cd28b5.zip cpython-c1f1ddf30a595c2bfa3c06e54fb03fa212cd28b5.tar.gz cpython-c1f1ddf30a595c2bfa3c06e54fb03fa212cd28b5.tar.bz2 |
bpo-40597: email: Use CTE if lines are longer than max_line_length consistently (gh-20038) (gh-20084)
raw_data_manager (default for EmailPolicy, EmailMessage)
does correct wrapping of 'text' parts as long as the message contains
characters outside of 7bit US-ASCII set: base64 or qp
Content-Transfer-Encoding is applied if the lines would be too long
without it. It did not, however, do this for ascii-only text,
which could result in lines that were longer than
policy.max_line_length or even the rfc 998 maximum.
This changeset fixes the heuristic so that if lines are longer than
policy.max_line_length, it will always apply a
content-transfer-encoding so that the lines are wrapped correctly.
(cherry picked from commit 6f2f475d5a2cd7675dce844f3af436ba919ef92b)
Co-authored-by: Arkadiusz Hiler <arek.l1@gmail.com>
Diffstat (limited to 'Lib/email')
-rw-r--r-- | Lib/email/contentmanager.py | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/Lib/email/contentmanager.py b/Lib/email/contentmanager.py index b904ded..2b4b875 100644 --- a/Lib/email/contentmanager.py +++ b/Lib/email/contentmanager.py @@ -146,13 +146,13 @@ def _encode_text(string, charset, cte, policy): def normal_body(lines): return b'\n'.join(lines) + b'\n' if cte==None: # Use heuristics to decide on the "best" encoding. - try: - return '7bit', normal_body(lines).decode('ascii') - except UnicodeDecodeError: - pass - if (policy.cte_type == '8bit' and - max(len(x) for x in lines) <= policy.max_line_length): - return '8bit', normal_body(lines).decode('ascii', 'surrogateescape') + if max(len(x) for x in lines) <= policy.max_line_length: + try: + return '7bit', normal_body(lines).decode('ascii') + except UnicodeDecodeError: + pass + if policy.cte_type == '8bit': + return '8bit', normal_body(lines).decode('ascii', 'surrogateescape') sniff = embedded_body(lines[:10]) sniff_qp = quoprimime.body_encode(sniff.decode('latin-1'), policy.max_line_length) |