diff options
Diffstat (limited to 'Lib/email')
-rw-r--r-- | Lib/email/generator.py | 2 | ||||
-rw-r--r-- | Lib/email/test/test_email.py | 16 |
2 files changed, 17 insertions, 1 deletions
diff --git a/Lib/email/generator.py b/Lib/email/generator.py index 02487e3..c6bfb70 100644 --- a/Lib/email/generator.py +++ b/Lib/email/generator.py @@ -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) diff --git a/Lib/email/test/test_email.py b/Lib/email/test/test_email.py index 95dc4af..352b9b1 100644 --- a/Lib/email/test/test_email.py +++ b/Lib/email/test/test_email.py @@ -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): |