summaryrefslogtreecommitdiffstats
path: root/Lib/email/message.py
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2014-02-07 17:46:17 (GMT)
committerR David Murray <rdmurray@bitdance.com>2014-02-07 17:46:17 (GMT)
commit15a693a6f8f5986c512557747112331ceb5c0b8c (patch)
treee85910ccacdbd73c4aabb05b2b2bef3c2311c92d /Lib/email/message.py
parent27e9de669be928b09d71fdd31f84da7b86f9a1fc (diff)
downloadcpython-15a693a6f8f5986c512557747112331ceb5c0b8c.zip
cpython-15a693a6f8f5986c512557747112331ceb5c0b8c.tar.gz
cpython-15a693a6f8f5986c512557747112331ceb5c0b8c.tar.bz2
#20531: Apply the 3.3 version of the #19063 fix.
So passing unicode to set_payload works again (but still doesn't do what you want when the message is serialized).
Diffstat (limited to 'Lib/email/message.py')
-rw-r--r--Lib/email/message.py26
1 files changed, 22 insertions, 4 deletions
diff --git a/Lib/email/message.py b/Lib/email/message.py
index ce673b0..9350615 100644
--- a/Lib/email/message.py
+++ b/Lib/email/message.py
@@ -301,9 +301,19 @@ 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 hasattr(payload, 'encode'):
+ if charset is None:
+ # We should check for ASCII-only here, but we can't do that
+ # for backward compatibility reasons. Fixed in 3.4.
+ self._payload = payload
+ return
+ if not isinstance(charset, Charset):
+ charset = Charset(charset)
+ payload = payload.encode(charset.output_charset)
+ if hasattr(payload, 'decode'):
+ self._payload = payload.decode('ascii', 'surrogateescape')
+ else:
+ self._payload = payload
if charset is not None:
self.set_charset(charset)
@@ -342,7 +352,15 @@ class Message:
try:
cte(self)
except TypeError:
- self._payload = charset.body_encode(self._payload)
+ # This if is for backward compatibility and will be removed
+ # in 3.4 when the ascii check is added to set_payload.
+ payload = self._payload
+ if payload:
+ try:
+ payload = payload.encode('ascii', 'surrogateescape')
+ except UnicodeError:
+ payload = payload.encode(charset.output_charset)
+ self._payload = charset.body_encode(payload)
self.add_header('Content-Transfer-Encoding', cte)
def get_charset(self):