]> granicus.if.org Git - python/commitdiff
#15249: Mangle From lines correctly when body contains invalid bytes.
authorR David Murray <rdmurray@bitdance.com>
Fri, 24 Aug 2012 15:14:13 +0000 (11:14 -0400)
committerR David Murray <rdmurray@bitdance.com>
Fri, 24 Aug 2012 15:14:13 +0000 (11:14 -0400)
Fix by Colin Su.  Test by me, based on a test written by Petri Lehtinen.

Lib/email/generator.py
Lib/email/test/test_email.py
Misc/ACKS
Misc/NEWS

index 02487e3082571bb0803eb6a9a0ae3a1354571c18..c6bfb704f53f458d9688a01f52237ea2a2578de0 100644 (file)
@@ -391,6 +391,8 @@ class BytesGenerator(Generator):
         if msg._payload is None:
             return
         if _has_surrogates(msg._payload):
+            if self._mangle_from_:
+                msg._payload = fcre.sub(">From ", msg._payload)
             self.write(msg._payload)
         else:
             super(BytesGenerator,self)._handle_text(msg)
index 95dc4af7c35fe59f74c031c53e30f2735f2efeba..352b9b1d9a0ba6a7143b1b0bae0657555d4d907c 100644 (file)
@@ -20,7 +20,7 @@ import email
 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
@@ -1298,6 +1298,20 @@ Blah blah blah
         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@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):
index 7f223607a99c91029730d36e5bd61cce56b8bef4..a46ee843e5d78cab38f9524b2c1cc49a0856e08e 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -915,6 +915,7 @@ Ken Stox
 Dan Stromberg
 Daniel Stutzbach
 Andreas Stührk
+Colin Su
 Pal Subbiah
 Nathan Sullivan
 Mark Summerfield
index 0d76a6e3552da71e11c23d6e724e99b49f9a4bf9..73f058c7b8bf83514c8b4c3b3c4fc13f46237f4e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -109,6 +109,9 @@ Core and Builtins
 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 #15199: Fix JavaScript's default MIME type to application/javascript.