File tree Expand file tree Collapse file tree 4 files changed +21
-1
lines changed
Expand file tree Collapse file tree 4 files changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -400,6 +400,8 @@ def _handle_text(self, msg):
400400 if msg ._payload is None :
401401 return
402402 if _has_surrogates (msg ._payload ) and not self .policy .cte_type == '7bit' :
403+ if self ._mangle_from_ :
404+ msg ._payload = fcre .sub (">From " , msg ._payload )
403405 self .write (msg ._payload )
404406 else :
405407 super (BytesGenerator ,self )._handle_text (msg )
Original file line number Diff line number Diff line change 2121from email .charset import Charset
2222from email .header import Header , decode_header , make_header
2323from email .parser import Parser , HeaderParser
24- from email .generator import Generator , DecodedGenerator
24+ from email .generator import Generator , DecodedGenerator , BytesGenerator
2525from email .message import Message
2626from email .mime .application import MIMEApplication
2727from email .mime .audio import MIMEAudio
@@ -1306,6 +1306,20 @@ def test_mangle_from_in_preamble_and_epilog(self):
13061306 self .assertEqual (len ([1 for x in s .getvalue ().split ('\n ' )
13071307 if x .startswith ('>From ' )]), 2 )
13081308
1309+ def test_mangled_from_with_bad_bytes (self ):
1310+ source = textwrap .dedent ("""\
1311+ Content-Type: text/plain; charset="utf-8"
1312+ MIME-Version: 1.0
1313+ Content-Transfer-Encoding: 8bit
1314+ From: aaa@bbb.org
1315+
1316+ """ ).encode ('utf-8' )
1317+ msg = email .message_from_bytes (source + b'From R\xc3 \xb6 lli\n ' )
1318+ b = BytesIO ()
1319+ g = BytesGenerator (b , mangle_from_ = True )
1320+ g .flatten (msg )
1321+ self .assertEqual (b .getvalue (), source + b'>From R\xc3 \xb6 lli\n ' )
1322+
13091323
13101324# Test the basic MIMEAudio class
13111325class TestMIMEAudio (unittest .TestCase ):
Original file line number Diff line number Diff line change @@ -1020,6 +1020,7 @@ Ken Stox
10201020Dan Stromberg
10211021Daniel Stutzbach
10221022Andreas Stührk
1023+ Colin Su
10231024Pal Subbiah
10241025Nathan Sullivan
10251026Mark Summerfield
Original file line number Diff line number Diff line change @@ -29,6 +29,9 @@ Core and Builtins
2929Library
3030-------
3131
32+ - Issue #15249: BytesGenerator now correctly mangles From lines (when
33+ requested) even if the body contains undecodable bytes.
34+
3235- Issue #15777: Fix a refleak in _posixsubprocess.
3336
3437- Issue ##665194: Update email.utils.localtime to use datetime.astimezone and
You can’t perform that action at this time.
0 commit comments