diff --git a/Lib/email/test/test_email.py b/Lib/email/test/test_email.py index 65b3ebd..8f4ef20 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 @@ -1276,6 +1276,51 @@ Blah blah blah """) +class TestFromManglingBytes(unittest.TestCase): + # See issue #15249 + + def setUp(self): + # Don't use MIMEText, because it tries to encode the payload + # automatically. We want the surrogates to emulate what + # BytesParser would have produced. + self.msg = Message() + self.msg['Content-Type'] = 'text/plain; charset="utf-8"' + self.msg['MIME-Version'] = '1.0' + self.msg['Content-Transfer-Encoding'] = '8bit' + self.msg['From'] = 'aaa@bbb.org' + + # Escape non-ASCII with surrogates. This is what BytesParser + # does. + text = b'From R\xc3\xb6lli\n'.decode('ascii', 'surrogateescape') + self.msg.set_payload(text) + + def test_mangled_from(self): + b = BytesIO() + g = BytesGenerator(b, mangle_from_=True) + g.flatten(self.msg) + self.assertEqual(b.getvalue(), b'''\ +Content-Type: text/plain; charset="utf-8" +MIME-Version: 1.0 +Content-Transfer-Encoding: 8bit +From: aaa@bbb.org + +>From R\xc3\xb6lli +''') + + def test_dont_mangle_from(self): + b = BytesIO() + g = BytesGenerator(b, mangle_from_=False) + g.flatten(self.msg) + self.assertEqual(b.getvalue(), b'''\ +Content-Type: text/plain; charset="utf-8" +MIME-Version: 1.0 +Content-Transfer-Encoding: 8bit +From: aaa@bbb.org + +From R\xc3\xb6lli +''') + + # Test the basic MIMEAudio class class TestMIMEAudio(unittest.TestCase):