changeset 78251:af2e044609ca 3.2

merge heads.
author Senthil Kumaran <senthil@uthcode.com>
date Sun, 22 Jul 2012 19:32:44 -0700
parents f6ce142c2fce (current diff) b97f65f2298d (diff)
children db0973994844 3e7b517e1b68
files Misc/NEWS
diffstat 3 files changed, 35 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/Lib/email/generator.py	Sun Jul 22 19:12:58 2012 -0700
+++ b/Lib/email/generator.py	Sun Jul 22 19:32:44 2012 -0700
@@ -233,7 +233,11 @@
             msg.set_boundary(boundary)
         # If there's a preamble, write it out, with a trailing CRLF
         if msg.preamble is not None:
-            self.write(msg.preamble + self._NL)
+            if self._mangle_from_:
+                preamble = fcre.sub('>From ', msg.preamble)
+            else:
+                preamble = msg.preamble
+            self.write(preamble + self._NL)
         # dash-boundary transport-padding CRLF
         self.write('--' + boundary + self._NL)
         # body-part
@@ -251,7 +255,11 @@
         self.write(self._NL + '--' + boundary + '--')
         if msg.epilogue is not None:
             self.write(self._NL)
-            self.write(msg.epilogue)
+            if self._mangle_from_:
+                epilogue = fcre.sub('>From ', msg.epilogue)
+            else:
+                epilogue = msg.epilogue
+            self.write(epilogue)
 
     def _handle_multipart_signed(self, msg):
         # The contents of signed parts has to stay unmodified in order to keep
--- a/Lib/email/test/test_email.py	Sun Jul 22 19:12:58 2012 -0700
+++ b/Lib/email/test/test_email.py	Sun Jul 22 19:32:44 2012 -0700
@@ -1275,6 +1275,28 @@
 Blah blah blah
 """)
 
+    def test_mangle_from_in_preamble_and_epilog(self):
+        s = StringIO()
+        g = Generator(s, mangle_from_=True)
+        msg = email.message_from_string(textwrap.dedent("""\
+            From: foo@bar.com
+            Mime-Version: 1.0
+            Content-Type: multipart/mixed; boundary=XXX
+
+            From somewhere unknown
+
+            --XXX
+            Content-Type: text/plain
+
+            foo
+
+            --XXX--
+
+            From somewhere unknowable
+            """))
+        g.flatten(msg)
+        self.assertEqual(len([1 for x in s.getvalue().split('\n')
+                                  if x.startswith('>From ')]), 2)
 
 
 # Test the basic MIMEAudio class
--- a/Misc/NEWS	Sun Jul 22 19:12:58 2012 -0700
+++ b/Misc/NEWS	Sun Jul 22 19:32:44 2012 -0700
@@ -98,6 +98,9 @@
 Library
 -------
 
+- Issue #15232: when mangle_from is True, email.Generator now correctly mangles
+  lines that start with 'From' that occur in a MIME preamble or epilogue.
+
 - Issue #13922: argparse no longer incorrectly strips '--'s that appear
   after the first one.