summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2012-08-24 15:23:50 (GMT)
committerR David Murray <rdmurray@bitdance.com>2012-08-24 15:23:50 (GMT)
commitad2a7d528acbb94bd4def418201d808226cb9f38 (patch)
treebb89cf1c60d3ed576302ad517502f3cea06e0d5f /Lib
parent4784e0267e1a7debce8c0f62ce245ea2e680f733 (diff)
parent638d40b4337808f5e7c3f415f49270185c893321 (diff)
downloadcpython-ad2a7d528acbb94bd4def418201d808226cb9f38.zip
cpython-ad2a7d528acbb94bd4def418201d808226cb9f38.tar.gz
cpython-ad2a7d528acbb94bd4def418201d808226cb9f38.tar.bz2
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.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/email/generator.py2
-rw-r--r--Lib/test/test_email/test_email.py16
2 files changed, 17 insertions, 1 deletions
diff --git a/Lib/email/generator.py b/Lib/email/generator.py
index 8413f3b..899adbc 100644
--- a/Lib/email/generator.py
+++ b/Lib/email/generator.py
@@ -400,6 +400,8 @@ class BytesGenerator(Generator):
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
index 1c3775f..5cc6d04 100644
--- a/Lib/test/test_email/test_email.py
+++ b/Lib/test/test_email/test_email.py
@@ -21,7 +21,7 @@ import email.policy
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 @@ 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):