X Tutup
Skip to content

Commit ad2a7d5

Browse files
committed
Merge python#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.
2 parents 4784e02 + 638d40b commit ad2a7d5

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

Lib/email/generator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff 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)

Lib/test/test_email/test_email.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from email.charset import Charset
2222
from email.header import Header, decode_header, make_header
2323
from email.parser import Parser, HeaderParser
24-
from email.generator import Generator, DecodedGenerator
24+
from email.generator import Generator, DecodedGenerator, BytesGenerator
2525
from email.message import Message
2626
from email.mime.application import MIMEApplication
2727
from 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\xb6lli\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\xb6lli\n')
1322+
13091323

13101324
# Test the basic MIMEAudio class
13111325
class TestMIMEAudio(unittest.TestCase):

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,7 @@ Ken Stox
10201020
Dan Stromberg
10211021
Daniel Stutzbach
10221022
Andreas Stührk
1023+
Colin Su
10231024
Pal Subbiah
10241025
Nathan Sullivan
10251026
Mark Summerfield

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ Core and Builtins
2929
Library
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

0 commit comments

Comments
 (0)
X Tutup