Mailing List Archive

gh-80361: Fix TypeError in email.Message.get_payload() (GH-117994)
https://github.com/python/cpython/commit/deaecb88fa5da68cbffca413c63af95fd99578dd
commit: deaecb88fa5da68cbffca413c63af95fd99578dd
branch: main
author: Serhiy Storchaka <storchaka@gmail.com>
committer: serhiy-storchaka <storchaka@gmail.com>
date: 2024-04-17T19:31:26+03:00
summary:

gh-80361: Fix TypeError in email.Message.get_payload() (GH-117994)

It was raised when the charset is rfc2231 encoded, e.g.:

Content-Type: text/plain; charset*=ansi-x3.4-1968''utf-8

files:
A Misc/NEWS.d/next/Library/2024-04-17-18-00-30.gh-issue-80361.RstWg-.rst
M Lib/email/message.py
M Lib/test/test_email/test_email.py

diff --git a/Lib/email/message.py b/Lib/email/message.py
index a14cca56b3745a..46bb8c21942af8 100644
--- a/Lib/email/message.py
+++ b/Lib/email/message.py
@@ -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:
diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py
index d9af05c306eb30..65ddbabcaa1997 100644
--- a/Lib/test/test_email/test_email.py
+++ b/Lib/test/test_email/test_email.py
@@ -4181,6 +4181,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')),
diff --git a/Misc/NEWS.d/next/Library/2024-04-17-18-00-30.gh-issue-80361.RstWg-.rst b/Misc/NEWS.d/next/Library/2024-04-17-18-00-30.gh-issue-80361.RstWg-.rst
new file mode 100644
index 00000000000000..3bbae23cc18bf6
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-04-17-18-00-30.gh-issue-80361.RstWg-.rst
@@ -0,0 +1,2 @@
+Fix TypeError in :func:`email.Message.get_payload` when the charset is :rfc:`2231`
+encoded.

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-leave@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: list-python-checkins@lists.gossamer-threads.com