summaryrefslogtreecommitdiff
path: root/Lib
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2013-02-11 10:51:28 -0500
committerR David Murray <rdmurray@bitdance.com>2013-02-11 10:51:28 -0500
commit14218f095cfa05243dd8b92320c05cd6bc23dbfa (patch)
tree0bc214482f26e64432ed4795cd64c8f70d4656c6 /Lib
parent8cbab76af9157f0b9e9069054e35b223b229d71d (diff)
downloadcpython-14218f095cfa05243dd8b92320c05cd6bc23dbfa.tar.gz
#17171: fix email.encoders.encode_7or8bit when applied to binary data.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/email/encoders.py4
-rw-r--r--Lib/email/test/test_email.py19
2 files changed, 21 insertions, 2 deletions
diff --git a/Lib/email/encoders.py b/Lib/email/encoders.py
index 88b2f57d09..82a28cf344 100644
--- a/Lib/email/encoders.py
+++ b/Lib/email/encoders.py
@@ -62,15 +62,17 @@ def encode_7or8bit(msg):
else:
orig.decode('ascii')
except UnicodeError:
- # iso-2022-* is non-ASCII but still 7-bit
charset = msg.get_charset()
output_cset = charset and charset.output_charset
+ # iso-2022-* is non-ASCII but encodes to a 7-bit representation
if output_cset and output_cset.lower().startswith('iso-2022-'):
msg['Content-Transfer-Encoding'] = '7bit'
else:
msg['Content-Transfer-Encoding'] = '8bit'
else:
msg['Content-Transfer-Encoding'] = '7bit'
+ if not isinstance(orig, str):
+ msg.set_payload(orig.decode('ascii', 'surrogateescape'))
diff --git a/Lib/email/test/test_email.py b/Lib/email/test/test_email.py
index e66a410fee..daed3b0d63 100644
--- a/Lib/email/test/test_email.py
+++ b/Lib/email/test/test_email.py
@@ -1438,7 +1438,24 @@ class TestMIMEApplication(unittest.TestCase):
eq(msg.get_payload().strip(), '+vv8/f7/')
eq(msg.get_payload(decode=True), bytesdata)
- def test_body_with_encode_noop(self):
+ def test_binary_body_with_encode_7or8bit(self):
+ # Issue 17171.
+ bytesdata = b'\xfa\xfb\xfc\xfd\xfe\xff'
+ msg = MIMEApplication(bytesdata, _encoder=encoders.encode_7or8bit)
+ # Treated as a string, this will be invalid code points.
+ self.assertEqual(msg.get_payload(), '\uFFFD' * len(bytesdata))
+ self.assertEqual(msg.get_payload(decode=True), bytesdata)
+ self.assertEqual(msg['Content-Transfer-Encoding'], '8bit')
+ s = BytesIO()
+ g = BytesGenerator(s)
+ g.flatten(msg)
+ wireform = s.getvalue()
+ msg2 = email.message_from_bytes(wireform)
+ self.assertEqual(msg.get_payload(), '\uFFFD' * len(bytesdata))
+ self.assertEqual(msg2.get_payload(decode=True), bytesdata)
+ self.assertEqual(msg2['Content-Transfer-Encoding'], '8bit')
+
+ def test_binary_body_with_encode_noop(self):
# Issue 16564: This does not produce an RFC valid message, since to be
# valid it should have a CTE of binary. But the below works in
# Python2, and is documented as working this way.