Skip to content

[3.12] gh-80361: Fix TypeError in email.Message.get_payload() (GH-117994) #117998

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

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def get_payload(self, i=None, decode=False):
try:
bpayload = payload.encode('ascii', 'surrogateescape')
try:
payload = bpayload.decode(self.get_param('charset', 'ascii'), 'replace')
payload = bpayload.decode(self.get_content_charset('ascii'), 'replace')
except LookupError:
payload = bpayload.decode('ascii', 'replace')
except UnicodeEncodeError:
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 @@ -4010,6 +4010,21 @@ def test_8bit_in_uuencode_body(self):
self.assertEqual(msg.get_payload(decode=True),
'<,.V<W1A; á \n'.encode('utf-8'))

def test_rfc2231_charset_8bit_CTE(self):
m = textwrap.dedent("""\
From: foo@bar.com
To: baz
Mime-Version: 1.0
Content-Type: text/plain; charset*=ansi-x3.4-1968''utf-8
Content-Transfer-Encoding: 8bit

pöstal
""").encode('utf-8')
msg = email.message_from_bytes(m)
self.assertEqual(msg.get_payload(), "pöstal\n")
self.assertEqual(msg.get_payload(decode=True),
"pöstal\n".encode('utf-8'))


headertest_headers = (
('From: foo@bar.com', ('From', 'foo@bar.com')),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix TypeError in :func:`email.Message.get_payload` when the charset is :rfc:`2231`
encoded.
Loading