Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 66383b2

Browse files
committed
Merge: #17171: fix email.encoders.encode_7or8bit when applied to binary data.
2 parents 0b61d3c + ec317a8 commit 66383b2

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

Lib/email/encoders.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,17 @@ def encode_7or8bit(msg):
6262
else:
6363
orig.decode('ascii')
6464
except UnicodeError:
65-
# iso-2022-* is non-ASCII but still 7-bit
6665
charset = msg.get_charset()
6766
output_cset = charset and charset.output_charset
67+
# iso-2022-* is non-ASCII but encodes to a 7-bit representation
6868
if output_cset and output_cset.lower().startswith('iso-2022-'):
6969
msg['Content-Transfer-Encoding'] = '7bit'
7070
else:
7171
msg['Content-Transfer-Encoding'] = '8bit'
7272
else:
7373
msg['Content-Transfer-Encoding'] = '7bit'
74+
if not isinstance(orig, str):
75+
msg.set_payload(orig.decode('ascii', 'surrogateescape'))
7476

7577

7678

Lib/test/test_email/test_email.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,24 @@ def test_body(self):
14401440
eq(msg.get_payload().strip(), '+vv8/f7/')
14411441
eq(msg.get_payload(decode=True), bytesdata)
14421442

1443-
def test_body_with_encode_noop(self):
1443+
def test_binary_body_with_encode_7or8bit(self):
1444+
# Issue 17171.
1445+
bytesdata = b'\xfa\xfb\xfc\xfd\xfe\xff'
1446+
msg = MIMEApplication(bytesdata, _encoder=encoders.encode_7or8bit)
1447+
# Treated as a string, this will be invalid code points.
1448+
self.assertEqual(msg.get_payload(), '\uFFFD' * len(bytesdata))
1449+
self.assertEqual(msg.get_payload(decode=True), bytesdata)
1450+
self.assertEqual(msg['Content-Transfer-Encoding'], '8bit')
1451+
s = BytesIO()
1452+
g = BytesGenerator(s)
1453+
g.flatten(msg)
1454+
wireform = s.getvalue()
1455+
msg2 = email.message_from_bytes(wireform)
1456+
self.assertEqual(msg.get_payload(), '\uFFFD' * len(bytesdata))
1457+
self.assertEqual(msg2.get_payload(decode=True), bytesdata)
1458+
self.assertEqual(msg2['Content-Transfer-Encoding'], '8bit')
1459+
1460+
def test_binary_body_with_encode_noop(self):
14441461
# Issue 16564: This does not produce an RFC valid message, since to be
14451462
# valid it should have a CTE of binary. But the below works in
14461463
# Python2, and is documented as working this way.

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ Core and Builtins
175175
Library
176176
-------
177177

178+
- Issue #16564: Fixed regression relative to Python2 in the operation of
179+
email.encoders.encode_7or8bit when used with binary data.
180+
178181
- Issue #17052: unittest discovery should use self.testLoader.
179182

180183
- Issue #4591: Uid and gid values larger than 2**31 are supported now.

0 commit comments

Comments
 (0)