gh-76511: Fix email.Message.as_string() for non-ASCII message with ASCII charset (GH-116125)
This commit is contained in:
parent
df59401108
commit
f97f25ef5d
@ -243,7 +243,7 @@ class Generator:
|
|||||||
# existing message.
|
# existing message.
|
||||||
msg = deepcopy(msg)
|
msg = deepcopy(msg)
|
||||||
del msg['content-transfer-encoding']
|
del msg['content-transfer-encoding']
|
||||||
msg.set_payload(payload, charset)
|
msg.set_payload(msg._payload, charset)
|
||||||
payload = msg.get_payload()
|
payload = msg.get_payload()
|
||||||
self._munge_cte = (msg['content-transfer-encoding'],
|
self._munge_cte = (msg['content-transfer-encoding'],
|
||||||
msg['content-type'])
|
msg['content-type'])
|
||||||
|
@ -340,7 +340,7 @@ class Message:
|
|||||||
return
|
return
|
||||||
if not isinstance(charset, Charset):
|
if not isinstance(charset, Charset):
|
||||||
charset = Charset(charset)
|
charset = Charset(charset)
|
||||||
payload = payload.encode(charset.output_charset)
|
payload = payload.encode(charset.output_charset, 'surrogateescape')
|
||||||
if hasattr(payload, 'decode'):
|
if hasattr(payload, 'decode'):
|
||||||
self._payload = payload.decode('ascii', 'surrogateescape')
|
self._payload = payload.decode('ascii', 'surrogateescape')
|
||||||
else:
|
else:
|
||||||
|
@ -337,6 +337,21 @@ class TestMessageAPI(TestEmailBase):
|
|||||||
msg = email.message_from_bytes(source)
|
msg = email.message_from_bytes(source)
|
||||||
self.assertEqual(msg.as_string(), expected)
|
self.assertEqual(msg.as_string(), expected)
|
||||||
|
|
||||||
|
def test_nonascii_as_string_with_ascii_charset(self):
|
||||||
|
m = textwrap.dedent("""\
|
||||||
|
MIME-Version: 1.0
|
||||||
|
Content-type: text/plain; charset="us-ascii"
|
||||||
|
Content-Transfer-Encoding: 8bit
|
||||||
|
|
||||||
|
Test if non-ascii messages with no Content-Transfer-Encoding set
|
||||||
|
can be as_string'd:
|
||||||
|
Föö bär
|
||||||
|
""")
|
||||||
|
source = m.encode('iso-8859-1')
|
||||||
|
expected = source.decode('ascii', 'replace')
|
||||||
|
msg = email.message_from_bytes(source)
|
||||||
|
self.assertEqual(msg.as_string(), expected)
|
||||||
|
|
||||||
def test_nonascii_as_string_without_content_type_and_cte(self):
|
def test_nonascii_as_string_without_content_type_and_cte(self):
|
||||||
m = textwrap.dedent("""\
|
m = textwrap.dedent("""\
|
||||||
MIME-Version: 1.0
|
MIME-Version: 1.0
|
||||||
|
@ -0,0 +1,4 @@
|
|||||||
|
Fix UnicodeEncodeError in :meth:`email.Message.as_string` that results when
|
||||||
|
a message that claims to be in the ascii character set actually has non-ascii
|
||||||
|
characters. Non-ascii characters are now replaced with the U+FFFD replacement
|
||||||
|
character, like in the ``replace`` error handler.
|
Loading…
x
Reference in New Issue
Block a user