mirror of
https://github.com/python/cpython.git
synced 2025-10-09 08:31:26 +00:00
#18324: set_payload now correctly handles binary input.
This also backs out the previous fixes for for #14360, #1717, and #16564. Those bugs were actually caused by the fact that set_payload didn't decode to str, thus rendering the model inconsistent. This fix does mean the data processed by the encoder functions goes through an extra encode/decode cycle, but it means the model is always consistent. Future API updates will provide a better way to encode payloads, which will bypass this minor de-optimization. Tests by Vajrasky Kok.
This commit is contained in:
parent
0b169125ca
commit
00ae435dee
4 changed files with 45 additions and 16 deletions
|
@ -28,7 +28,7 @@ def encode_base64(msg):
|
|||
|
||||
Also, add an appropriate Content-Transfer-Encoding header.
|
||||
"""
|
||||
orig = msg.get_payload()
|
||||
orig = msg.get_payload(decode=True)
|
||||
encdata = str(_bencode(orig), 'ascii')
|
||||
msg.set_payload(encdata)
|
||||
msg['Content-Transfer-Encoding'] = 'base64'
|
||||
|
@ -40,20 +40,16 @@ def encode_quopri(msg):
|
|||
|
||||
Also, add an appropriate Content-Transfer-Encoding header.
|
||||
"""
|
||||
orig = msg.get_payload()
|
||||
if isinstance(orig, str):
|
||||
# If it is a string, the model data may have binary data encoded in via
|
||||
# surrogateescape. Convert back to bytes so we can CTE encode it.
|
||||
orig = orig.encode('ascii', 'surrogateescape')
|
||||
orig = msg.get_payload(decode=True)
|
||||
encdata = _qencode(orig)
|
||||
msg.set_payload(encdata.decode('ascii', 'surrogateescape'))
|
||||
msg.set_payload(encdata)
|
||||
msg['Content-Transfer-Encoding'] = 'quoted-printable'
|
||||
|
||||
|
||||
|
||||
def encode_7or8bit(msg):
|
||||
"""Set the Content-Transfer-Encoding header to 7bit or 8bit."""
|
||||
orig = msg.get_payload()
|
||||
orig = msg.get_payload(decode=True)
|
||||
if orig is None:
|
||||
# There's no payload. For backwards compatibility we use 7bit
|
||||
msg['Content-Transfer-Encoding'] = '7bit'
|
||||
|
@ -75,16 +71,8 @@ def encode_7or8bit(msg):
|
|||
msg['Content-Transfer-Encoding'] = '8bit'
|
||||
else:
|
||||
msg['Content-Transfer-Encoding'] = '7bit'
|
||||
if not isinstance(orig, str):
|
||||
msg.set_payload(orig.decode('ascii', 'surrogateescape'))
|
||||
|
||||
|
||||
|
||||
def encode_noop(msg):
|
||||
"""Do nothing."""
|
||||
# Well, not quite *nothing*: in Python3 we have to turn bytes into a string
|
||||
# in our internal surrogateescaped form in order to keep the model
|
||||
# consistent.
|
||||
orig = msg.get_payload()
|
||||
if not isinstance(orig, str):
|
||||
msg.set_payload(orig.decode('ascii', 'surrogateescape'))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue