[Python-checkins] cpython (merge 3.2 -> default): Merge #15249: Mangle From lines correctly when body contains invalid bytes.

r.david.murray python-checkins at python.org
Fri Aug 24 17:31:43 CEST 2012


http://hg.python.org/cpython/rev/b6ee4e8c7a77
changeset:   78732:b6ee4e8c7a77
parent:      78730:46e83012c413
parent:      78731:119c645f310e
user:        R David Murray <rdmurray at bitdance.com>
date:        Fri Aug 24 11:23:50 2012 -0400
summary:
  Merge #15249: Mangle From lines correctly when body contains invalid bytes.

Fix by Colin Su.  Test by me, based on a test written by Petri Lehtinen.

files:
  Lib/email/generator.py            |   2 ++
  Lib/test/test_email/test_email.py |  16 +++++++++++++++-
  Misc/ACKS                         |   1 +
  Misc/NEWS                         |   3 +++
  4 files changed, 21 insertions(+), 1 deletions(-)


diff --git a/Lib/email/generator.py b/Lib/email/generator.py
--- a/Lib/email/generator.py
+++ b/Lib/email/generator.py
@@ -400,6 +400,8 @@
         if msg._payload is None:
             return
         if _has_surrogates(msg._payload) and not self.policy.cte_type=='7bit':
+            if self._mangle_from_:
+                msg._payload = fcre.sub(">From ", msg._payload)
             self.write(msg._payload)
         else:
             super(BytesGenerator,self)._handle_text(msg)
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
@@ -21,7 +21,7 @@
 from email.charset import Charset
 from email.header import Header, decode_header, make_header
 from email.parser import Parser, HeaderParser
-from email.generator import Generator, DecodedGenerator
+from email.generator import Generator, DecodedGenerator, BytesGenerator
 from email.message import Message
 from email.mime.application import MIMEApplication
 from email.mime.audio import MIMEAudio
@@ -1306,6 +1306,20 @@
         self.assertEqual(len([1 for x in s.getvalue().split('\n')
                                   if x.startswith('>From ')]), 2)
 
+    def test_mangled_from_with_bad_bytes(self):
+        source = textwrap.dedent("""\
+            Content-Type: text/plain; charset="utf-8"
+            MIME-Version: 1.0
+            Content-Transfer-Encoding: 8bit
+            From: aaa at bbb.org
+
+        """).encode('utf-8')
+        msg = email.message_from_bytes(source + b'From R\xc3\xb6lli\n')
+        b = BytesIO()
+        g = BytesGenerator(b, mangle_from_=True)
+        g.flatten(msg)
+        self.assertEqual(b.getvalue(), source + b'>From R\xc3\xb6lli\n')
+
 
 # Test the basic MIMEAudio class
 class TestMIMEAudio(unittest.TestCase):
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1020,6 +1020,7 @@
 Dan Stromberg
 Daniel Stutzbach
 Andreas Stührk
+Colin Su
 Pal Subbiah
 Nathan Sullivan
 Mark Summerfield
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -29,6 +29,9 @@
 Library
 -------
 
+- Issue #15249: BytesGenerator now correctly mangles From lines (when
+  requested) even if the body contains undecodable bytes.
+
 - Issue #15777: Fix a refleak in _posixsubprocess.
 
 - Issue ##665194: Update email.utils.localtime to use datetime.astimezone and

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


More information about the Python-checkins mailing list