Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-76511: Fix email.Message.as_string() for non-ASCII message with ASCII charset #116125

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/email/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def _handle_text(self, msg):
# existing message.
msg = deepcopy(msg)
del msg['content-transfer-encoding']
msg.set_payload(payload, charset)
msg.set_payload(msg._payload, charset)
payload = msg.get_payload()
self._munge_cte = (msg['content-transfer-encoding'],
msg['content-type'])
Expand Down
2 changes: 1 addition & 1 deletion Lib/email/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def set_payload(self, payload, charset=None):
return
if not isinstance(charset, Charset):
charset = Charset(charset)
payload = payload.encode(charset.output_charset)
payload = payload.encode(charset.output_charset, 'surrogateescape')
if hasattr(payload, 'decode'):
self._payload = payload.decode('ascii', 'surrogateescape')
else:
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_email/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,21 @@ def test_nonascii_as_string_without_cte(self):
msg = email.message_from_bytes(source)
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):
m = textwrap.dedent("""\
MIME-Version: 1.0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :meth:`email.Message.as_string` for non-ASCII message with ASCII
charset.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't really describe the change very clearly. I would say something like "Fix exception in message_as_string that results when a message that claims to be in the ascii character set actually has non-ascii characters by replacing the non-ascii characters with ?, like the 'replace' parameter of encode does." Or something like that.

I don't want this comment to be a blocker, though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your suggestion @bitdancer.

Loading