diff options
author | R David Murray <rdmurray@bitdance.com> | 2013-12-11 21:52:11 (GMT) |
---|---|---|
committer | R David Murray <rdmurray@bitdance.com> | 2013-12-11 21:52:11 (GMT) |
commit | 50bfbb9903171c96c19359fd50fb6a5570128ef6 (patch) | |
tree | e275522a82b7244c17ffa19ef23e52d688ac6bc8 /Lib/email/message.py | |
parent | 34bd9fc59a36f9803777f0446bc461083bdec78e (diff) | |
download | cpython-50bfbb9903171c96c19359fd50fb6a5570128ef6.zip cpython-50bfbb9903171c96c19359fd50fb6a5570128ef6.tar.gz cpython-50bfbb9903171c96c19359fd50fb6a5570128ef6.tar.bz2 |
#19063: fix set_payload handling of non-ASCII string input.
This version of the fix raises an error instead of accepting the invalid
input (ie: if a non-ASCII string is used but no charset is specified).
Diffstat (limited to 'Lib/email/message.py')
-rw-r--r-- | Lib/email/message.py | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/Lib/email/message.py b/Lib/email/message.py index ce673b0..9d295fc 100644 --- a/Lib/email/message.py +++ b/Lib/email/message.py @@ -301,9 +301,23 @@ 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: + 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 charset is not None: self.set_charset(charset) @@ -342,7 +356,7 @@ class Message: try: cte(self) except TypeError: - self._payload = charset.body_encode(self._payload) + self._payload = charset.body_encode(self.get_payload(decode=True)) self.add_header('Content-Transfer-Encoding', cte) def get_charset(self): |