[Python-checkins] cpython (merge 3.2 -> 3.3): Merge: #16564: Fix regression in use of encoders.encode_noop with binary data.

r.david.murray python-checkins at python.org
Sat Feb 9 19:17:09 CET 2013


http://hg.python.org/cpython/rev/2b1edefc1e99
changeset:   82098:2b1edefc1e99
branch:      3.3
parent:      82093:f2c2846f0c2f
parent:      82097:a1a04f76d08c
user:        R David Murray <rdmurray at bitdance.com>
date:        Sat Feb 09 13:10:54 2013 -0500
summary:
  Merge: #16564: Fix regression in use of encoders.encode_noop with binary data.

files:
  Lib/email/encoders.py             |   6 ++++++
  Lib/email/generator.py            |   3 +++
  Lib/test/test_email/test_email.py |  16 ++++++++++++++++
  Misc/NEWS                         |   3 +++
  4 files changed, 28 insertions(+), 0 deletions(-)


diff --git a/Lib/email/encoders.py b/Lib/email/encoders.py
--- a/Lib/email/encoders.py
+++ b/Lib/email/encoders.py
@@ -76,3 +76,9 @@
 
 def encode_noop(msg):
     """Do nothing."""
+    # Well, not quite *nothing*: in Python3 we have to turn bytes into a string
+    # in our internal surrogateescaped form in order to keep the model
+    # consistent.
+    orig = msg.get_payload()
+    if not isinstance(orig, str):
+        msg.set_payload(orig.decode('ascii', 'surrogateescape'))
diff --git a/Lib/email/generator.py b/Lib/email/generator.py
--- a/Lib/email/generator.py
+++ b/Lib/email/generator.py
@@ -406,6 +406,9 @@
         else:
             super(BytesGenerator,self)._handle_text(msg)
 
+    # Default body handler
+    _writeBody = _handle_text
+
     @classmethod
     def _compile_re(cls, s, flags):
         return re.compile(s.encode('ascii'), flags)
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,6 +1440,22 @@
         eq(msg.get_payload().strip(), '+vv8/f7/')
         eq(msg.get_payload(decode=True), bytesdata)
 
+    def test_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.
+        bytesdata = b'\xfa\xfb\xfc\xfd\xfe\xff'
+        msg = MIMEApplication(bytesdata, _encoder=encoders.encode_noop)
+        # 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)
+        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)
 
 
 # Test the basic MIMEText class
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -169,6 +169,9 @@
 Library
 -------
 
+- Issue #16564: Fixed regression relative to Python2 in the operation of
+  email.encoders.encode_noop when used with binary data.
+
 - Issue #10355: In SpooledTemporaryFile class mode, name, encoding and
   newlines properties now work for unrolled files.  Obsoleted and never
   working on Python 3 xreadline method now removed.

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


More information about the Python-checkins mailing list