]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-80361: Fix TypeError in email.Message.get_payload() (GH-117994)
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 17 Apr 2024 16:31:26 +0000 (19:31 +0300)
committerGitHub <noreply@github.com>
Wed, 17 Apr 2024 16:31:26 +0000 (19:31 +0300)
It was raised when the charset is rfc2231 encoded, e.g.:

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

Lib/email/message.py
Lib/test/test_email/test_email.py
Misc/NEWS.d/next/Library/2024-04-17-18-00-30.gh-issue-80361.RstWg-.rst [new file with mode: 0644]

index a14cca56b3745aa9119af291ceeda4f497f18451..46bb8c21942af85ed4dada333ab6b34e6c86f3e2 100644 (file)
@@ -294,7 +294,7 @@ class Message:
                 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:
index d9af05c306eb3035b859641f6cd7736691994ec6..65ddbabcaa19978d23e74238bd298f9093455be0 100644 (file)
@@ -4181,6 +4181,21 @@ class Test8BitBytesHandling(TestEmailBase):
         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 (file)
index 0000000..3bbae23
--- /dev/null
@@ -0,0 +1,2 @@
+Fix TypeError in :func:`email.Message.get_payload` when the charset is :rfc:`2231`
+encoded.