Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 85c32ad

Browse filesBrowse files
[3.12] gh-76511: Fix email.Message.as_string() for non-ASCII message with ASCII charset (GH-116125) (GH-116364)
(cherry picked from commit f97f25e) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 34efd49 commit 85c32ad
Copy full SHA for 85c32ad

File tree

Expand file treeCollapse file tree

4 files changed

+21
-2
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+21
-2
lines changed

‎Lib/email/generator.py

Copy file name to clipboardExpand all lines: Lib/email/generator.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def _handle_text(self, msg):
243243
# existing message.
244244
msg = deepcopy(msg)
245245
del msg['content-transfer-encoding']
246-
msg.set_payload(payload, charset)
246+
msg.set_payload(msg._payload, charset)
247247
payload = msg.get_payload()
248248
self._munge_cte = (msg['content-transfer-encoding'],
249249
msg['content-type'])

‎Lib/email/message.py

Copy file name to clipboardExpand all lines: Lib/email/message.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ def set_payload(self, payload, charset=None):
340340
return
341341
if not isinstance(charset, Charset):
342342
charset = Charset(charset)
343-
payload = payload.encode(charset.output_charset)
343+
payload = payload.encode(charset.output_charset, 'surrogateescape')
344344
if hasattr(payload, 'decode'):
345345
self._payload = payload.decode('ascii', 'surrogateescape')
346346
else:

‎Lib/test/test_email/test_email.py

Copy file name to clipboardExpand all lines: Lib/test/test_email/test_email.py
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,21 @@ def test_nonascii_as_string_without_cte(self):
336336
msg = email.message_from_bytes(source)
337337
self.assertEqual(msg.as_string(), expected)
338338

339+
def test_nonascii_as_string_with_ascii_charset(self):
340+
m = textwrap.dedent("""\
341+
MIME-Version: 1.0
342+
Content-type: text/plain; charset="us-ascii"
343+
Content-Transfer-Encoding: 8bit
344+
345+
Test if non-ascii messages with no Content-Transfer-Encoding set
346+
can be as_string'd:
347+
Föö bär
348+
""")
349+
source = m.encode('iso-8859-1')
350+
expected = source.decode('ascii', 'replace')
351+
msg = email.message_from_bytes(source)
352+
self.assertEqual(msg.as_string(), expected)
353+
339354
def test_nonascii_as_string_without_content_type_and_cte(self):
340355
m = textwrap.dedent("""\
341356
MIME-Version: 1.0
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix UnicodeEncodeError in :meth:`email.Message.as_string` that results when
2+
a message that claims to be in the ascii character set actually has non-ascii
3+
characters. Non-ascii characters are now replaced with the U+FFFD replacement
4+
character, like in the ``replace`` error handler.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.