@@ -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.
0 commit comments