summaryrefslogtreecommitdiffstats
path: root/Lib/email
diff options
context:
space:
mode:
authorMiss Skeleton (bot) <31488909+miss-islington@users.noreply.github.com>2020-10-19 23:07:20 (GMT)
committerGitHub <noreply@github.com>2020-10-19 23:07:20 (GMT)
commit371146a3f8a989964e2a9c0efc7d776815410fac (patch)
treeb9f9c702e614b015fafb175c1d034beebf6863b7 /Lib/email
parent609a38a36f3006ee00eccc18b25e3b2d577a3e56 (diff)
downloadcpython-371146a3f8a989964e2a9c0efc7d776815410fac.zip
cpython-371146a3f8a989964e2a9c0efc7d776815410fac.tar.gz
cpython-371146a3f8a989964e2a9c0efc7d776815410fac.tar.bz2
bpo-27321 Fix email.generator.py to not replace a non-existent header. (GH-18074)
This PR replaces GH-1977. The reason for the replacement is two-fold. The fix itself is different is that if the CTE header doesn't exist in the original message, it is inserted. This is important because the new CTE could be quoted-printable whereas the original is implicit 8bit. Also the tests are different. The test_nonascii_as_string_without_cte test in GH-1977 doesn't actually test the issue in that it passes without the fix. The test_nonascii_as_string_without_content_type_and_cte test is improved here, and even though it doesn't fail without the fix, it is included for completeness. Automerge-Triggered-By: @warsaw (cherry picked from commit bf838227c35212709dc43b3c3c57f8e1655c1d24) Co-authored-by: Mark Sapiro <mark@msapiro.net>
Diffstat (limited to 'Lib/email')
-rw-r--r--Lib/email/generator.py6
1 files changed, 5 insertions, 1 deletions
diff --git a/Lib/email/generator.py b/Lib/email/generator.py
index ae670c2..c9b1216 100644
--- a/Lib/email/generator.py
+++ b/Lib/email/generator.py
@@ -186,7 +186,11 @@ class Generator:
# If we munged the cte, copy the message again and re-fix the CTE.
if munge_cte:
msg = deepcopy(msg)
- msg.replace_header('content-transfer-encoding', munge_cte[0])
+ # Preserve the header order if the CTE header already exists.
+ if msg.get('content-transfer-encoding') is None:
+ msg['Content-Transfer-Encoding'] = munge_cte[0]
+ else:
+ msg.replace_header('content-transfer-encoding', munge_cte[0])
msg.replace_header('content-type', munge_cte[1])
# Write the headers. First we see if the message object wants to
# handle that itself. If not, we'll do it generically.