https://github.com/python/cpython/commit/fda8cd1fd38a12e17b1db1775d54ff0fa4d886b8 commit: fda8cd1fd38a12e17b1db1775d54ff0fa4d886b8 branch: 3.12 author: Miss Islington (bot) <[email protected]> committer: serhiy-storchaka <[email protected]> date: 2024-04-17T16:59:35Z summary:
[3.12] gh-80361: Fix TypeError in email.Message.get_payload() (GH-117994) (GH-117998) It was raised when the charset is rfc2231 encoded, e.g.: Content-Type: text/plain; charset*=ansi-x3.4-1968''utf-8 (cherry picked from commit deaecb88fa5da68cbffca413c63af95fd99578dd) Co-authored-by: Serhiy Storchaka <[email protected]> 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 a373c53c7c798c..fc8d87974e6a4c 100644 --- a/Lib/test/test_email/test_email.py +++ b/Lib/test/test_email/test_email.py @@ -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: [email protected] + 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: [email protected]', ('From', '[email protected]')), 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 -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/python-checkins.python.org/ Member address: [email protected]
