diff options
author | R David Murray <rdmurray@bitdance.com> | 2014-02-07 17:40:37 (GMT) |
---|---|---|
committer | R David Murray <rdmurray@bitdance.com> | 2014-02-07 17:40:37 (GMT) |
commit | 27e9de669be928b09d71fdd31f84da7b86f9a1fc (patch) | |
tree | 7a337c8b0a5b6f4ae0beb7c3e38b37aa61b21d33 /Lib/email | |
parent | 790202d61308efd3e7bc69cd61bff2306d789e57 (diff) | |
download | cpython-27e9de669be928b09d71fdd31f84da7b86f9a1fc.zip cpython-27e9de669be928b09d71fdd31f84da7b86f9a1fc.tar.gz cpython-27e9de669be928b09d71fdd31f84da7b86f9a1fc.tar.bz2 |
#20531: Revert e20f98a8ed71, the 3.4 version of the #19063 fix.
Diffstat (limited to 'Lib/email')
-rw-r--r-- | Lib/email/charset.py | 29 | ||||
-rw-r--r-- | Lib/email/message.py | 22 |
2 files changed, 22 insertions, 29 deletions
diff --git a/Lib/email/charset.py b/Lib/email/charset.py index 51f24b2..892bab5 100644 --- a/Lib/email/charset.py +++ b/Lib/email/charset.py @@ -378,19 +378,18 @@ class Charset: return None def body_encode(self, string): - """Body-encode a string, converting it first to bytes if needed. + """Body-encode a string by converting it first to bytes. The type of encoding (base64 or quoted-printable) will be based on - self.body_encoding. If body_encoding is None, we perform no CTE - encoding (the CTE will be either 7bit or 8bit), we just encode the - binary representation to ascii using the surrogateescape error handler, - which will enable the Generators to produce the correct output. + self.body_encoding. If body_encoding is None, we assume the + output charset is a 7bit encoding, so re-encoding the decoded + string using the ascii codec produces the correct string version + of the content. """ - if not string: - return string - if isinstance(string, str): - string = string.encode(self.output_charset) + # 7bit/8bit encodings return the string unchanged (module conversions) if self.body_encoding is BASE64: + if isinstance(string, str): + string = string.encode(self.output_charset) return email.base64mime.body_encode(string) elif self.body_encoding is QP: # quopromime.body_encode takes a string, but operates on it as if @@ -399,7 +398,15 @@ 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. - string = string.decode('latin1') + # + # 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') return email.quoprimime.body_encode(string) else: - return string.decode('ascii', 'surrogateescape') + if isinstance(string, str): + string = string.encode(self.output_charset).decode('ascii') + return string diff --git a/Lib/email/message.py b/Lib/email/message.py index 9d295fc..ce673b0 100644 --- a/Lib/email/message.py +++ b/Lib/email/message.py @@ -301,23 +301,9 @@ class Message: Optional charset sets the message's default character set. See set_charset() for details. """ - if hasattr(payload, 'encode'): - if charset is None: - try: - payload.encode('ascii', 'surrogateescape') - except UnicodeError: - raise TypeError("charset argument must be specified" - " when non-ASCII characters are used in the" - " payload") from None - 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 isinstance(payload, bytes): + payload = payload.decode('ascii', 'surrogateescape') + self._payload = payload if charset is not None: self.set_charset(charset) @@ -356,7 +342,7 @@ class Message: try: cte(self) except TypeError: - self._payload = charset.body_encode(self.get_payload(decode=True)) + self._payload = charset.body_encode(self._payload) self.add_header('Content-Transfer-Encoding', cte) def get_charset(self): |