[Python-checkins] cpython (merge 3.3 -> default): Merge: #17171: fix email.encoders.encode_7or8bit when applied to binary data.

r.david.murray python-checkins at python.org
Mon Feb 11 17:20:02 CET 2013


http://hg.python.org/cpython/rev/a80b67611c6d
changeset:   82164:a80b67611c6d
parent:      82160:aa15df77e58f
parent:      82163:cabcddbed377
user:        R David Murray <rdmurray at bitdance.com>
date:        Mon Feb 11 10:54:22 2013 -0500
summary:
  Merge: #17171: fix email.encoders.encode_7or8bit when applied to binary data.

files:
  Lib/email/encoders.py             |   4 +++-
  Lib/test/test_email/test_email.py |  19 ++++++++++++++++++-
  Misc/NEWS                         |   3 +++
  3 files changed, 24 insertions(+), 2 deletions(-)


diff --git a/Lib/email/encoders.py b/Lib/email/encoders.py
--- a/Lib/email/encoders.py
+++ b/Lib/email/encoders.py
@@ -62,15 +62,17 @@
         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/test/test_email/test_email.py b/Lib/test/test_email/test_email.py
--- a/Lib/test/test_email/test_email.py
+++ b/Lib/test/test_email/test_email.py
@@ -1440,7 +1440,24 @@
         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.
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -250,6 +250,9 @@
 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 #4591: Uid and gid values larger than 2**31 are supported now.

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list