]> granicus.if.org Git - python/commitdiff
#17171: fix email.encoders.encode_7or8bit when applied to binary data.
authorR David Murray <rdmurray@bitdance.com>
Mon, 11 Feb 2013 15:51:28 +0000 (10:51 -0500)
committerR David Murray <rdmurray@bitdance.com>
Mon, 11 Feb 2013 15:51:28 +0000 (10:51 -0500)
Lib/email/encoders.py
Lib/email/test/test_email.py
Misc/NEWS

index 88b2f57d092e55d651b2bf0b1e2d98f8c7db83d1..82a28cf3445c4bc2de7eb4d5257a2cce543f9091 100644 (file)
@@ -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'))
 
 
 \f
index e66a410fee7128e0e9f7b41b75e207aacd75af55..daed3b0d63f6d7a9fb78efab95030e4fa5d839e8 100644 (file)
@@ -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.
index 1f772b724127564034f16b80f2b9a51c91c69260..dc2e4ecf68674fa17b99f2ccd6d73f61000b02b0 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -221,6 +221,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #16564: Fixed regression relative to Python2 in the operation of
+  email.encoders.encode_7or8bit when used with binary data.
+
 - Issue #17052: unittest discovery should use self.testLoader.
 
 - Issue #17141: random.vonmisesvariate() no more hangs for large kappas.