diff options
author | R David Murray <rdmurray@bitdance.com> | 2014-02-07 17:46:17 (GMT) |
---|---|---|
committer | R David Murray <rdmurray@bitdance.com> | 2014-02-07 17:46:17 (GMT) |
commit | 15a693a6f8f5986c512557747112331ceb5c0b8c (patch) | |
tree | e85910ccacdbd73c4aabb05b2b2bef3c2311c92d /Lib/email | |
parent | 27e9de669be928b09d71fdd31f84da7b86f9a1fc (diff) | |
download | cpython-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')
-rw-r--r-- | Lib/email/charset.py | 11 | ||||
-rw-r--r-- | Lib/email/message.py | 26 |
2 files changed, 26 insertions, 11 deletions
diff --git a/Lib/email/charset.py b/Lib/email/charset.py index 892bab5..e999472 100644 --- a/Lib/email/charset.py +++ b/Lib/email/charset.py @@ -386,7 +386,8 @@ class Charset: string using the ascii codec produces the correct string version of the content. """ - # 7bit/8bit encodings return the string unchanged (module conversions) + if not string: + return string if self.body_encoding is BASE64: if isinstance(string, str): string = string.encode(self.output_charset) @@ -398,13 +399,9 @@ class Charset: # character set, then, we must turn it into pseudo bytes via the # latin1 charset, which will encode any byte as a single code point # between 0 and 255, which is what body_encode is expecting. - # - # Note that this clause doesn't handle the case of a _payload that - # is already bytes. It never did, and the semantics of _payload - # being bytes has never been nailed down, so fixing that is a - # longer term TODO. if isinstance(string, str): - string = string.encode(self.output_charset).decode('latin1') + string = string.encode(self.output_charset) + string = string.decode('latin1') return email.quoprimime.body_encode(string) else: if isinstance(string, str): 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): |