summaryrefslogtreecommitdiffstats
path: root/Lib/email
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2013-08-22 01:13:51 (GMT)
committerR David Murray <rdmurray@bitdance.com>2013-08-22 01:13:51 (GMT)
commitb8c537094d52dc07434df757c4c29c0f6c6e76d4 (patch)
tree749de3324b11d8af63dbda2e096b6e8a7a9dc329 /Lib/email
parentcba2e3c2e9f219043ddef0a896748f50691b3c47 (diff)
parent00ae435deef434f471e39bea3f3ab3a3e3cd90fe (diff)
downloadcpython-b8c537094d52dc07434df757c4c29c0f6c6e76d4.zip
cpython-b8c537094d52dc07434df757c4c29c0f6c6e76d4.tar.gz
cpython-b8c537094d52dc07434df757c4c29c0f6c6e76d4.tar.bz2
Merge #18324: set_payload now correctly handles binary input.
Diffstat (limited to 'Lib/email')
-rw-r--r--Lib/email/encoders.py20
-rw-r--r--Lib/email/message.py2
2 files changed, 6 insertions, 16 deletions
diff --git a/Lib/email/encoders.py b/Lib/email/encoders.py
index a0d062a..f9657f0 100644
--- a/Lib/email/encoders.py
+++ b/Lib/email/encoders.py
@@ -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'))
diff --git a/Lib/email/message.py b/Lib/email/message.py
index b5f7b3a..ebaf1c1 100644
--- a/Lib/email/message.py
+++ b/Lib/email/message.py
@@ -303,6 +303,8 @@ class Message:
Optional charset sets the message's default character set. See
set_charset() for details.
"""
+ if isinstance(payload, bytes):
+ payload = payload.decode('ascii', 'surrogateescape')
self._payload = payload
if charset is not None:
self.set_charset(charset)