File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -76,3 +76,9 @@ def encode_7or8bit(msg):
7676
7777def encode_noop (msg ):
7878 """Do nothing."""
79+ # Well, not quite *nothing*: in Python3 we have to turn bytes into a string
80+ # in our internal surrogateescaped form in order to keep the model
81+ # consistent.
82+ orig = msg .get_payload ()
83+ if not isinstance (orig , str ):
84+ msg .set_payload (orig .decode ('ascii' , 'surrogateescape' ))
Original file line number Diff line number Diff line change @@ -397,6 +397,9 @@ def _handle_text(self, msg):
397397 else :
398398 super (BytesGenerator ,self )._handle_text (msg )
399399
400+ # Default body handler
401+ _writeBody = _handle_text
402+
400403 @classmethod
401404 def _compile_re (cls , s , flags ):
402405 return re .compile (s .encode ('ascii' ), flags )
Original file line number Diff line number Diff line change @@ -1438,6 +1438,22 @@ 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 ):
1442+ # Issue 16564: This does not produce an RFC valid message, since to be
1443+ # valid it should have a CTE of binary. But the below works in
1444+ # Python2, and is documented as working this way.
1445+ bytesdata = b'\xfa \xfb \xfc \xfd \xfe \xff '
1446+ msg = MIMEApplication (bytesdata , _encoder = encoders .encode_noop )
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+ s = BytesIO ()
1451+ g = BytesGenerator (s )
1452+ g .flatten (msg )
1453+ wireform = s .getvalue ()
1454+ msg2 = email .message_from_bytes (wireform )
1455+ self .assertEqual (msg .get_payload (), '\uFFFD ' * len (bytesdata ))
1456+ self .assertEqual (msg2 .get_payload (decode = True ), bytesdata )
14411457
14421458
14431459# Test the basic MIMEText class
Original file line number Diff line number Diff line change @@ -215,6 +215,9 @@ Core and Builtins
215215Library
216216-------
217217
218+ - Issue #16564: Fixed regression relative to Python2 in the operation of
219+ email.encoders.encode_noop when used with binary data.
220+
218221- Issue #10355: In SpooledTemporaryFile class mode, name, encoding and
219222 newlines properties now work for unrolled files. Obsoleted and never
220223 working on Python 3 xreadline method now removed.
You can’t perform that action at this time.
0 commit comments