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