summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-04-17 16:59:35 (GMT)
committerGitHub <noreply@github.com>2024-04-17 16:59:35 (GMT)
commitfda8cd1fd38a12e17b1db1775d54ff0fa4d886b8 (patch)
treeba0127c7f89e042d76be05558b193aae3db7959f
parente95a535ea2915060759db66ae9e228a19f134125 (diff)
downloadcpython-fda8cd1fd38a12e17b1db1775d54ff0fa4d886b8.zip
cpython-fda8cd1fd38a12e17b1db1775d54ff0fa4d886b8.tar.gz
cpython-fda8cd1fd38a12e17b1db1775d54ff0fa4d886b8.tar.bz2
[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 <storchaka@gmail.com>
-rw-r--r--Lib/email/message.py2
-rw-r--r--Lib/test/test_email/test_email.py15
-rw-r--r--Misc/NEWS.d/next/Library/2024-04-17-18-00-30.gh-issue-80361.RstWg-.rst2
3 files changed, 18 insertions, 1 deletions
diff --git a/Lib/email/message.py b/Lib/email/message.py
index a14cca5..46bb8c2 100644
--- a/Lib/email/message.py
+++ b/Lib/email/message.py
@@ -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:
diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py
index a373c53..fc8d879 100644
--- a/Lib/test/test_email/test_email.py
+++ b/Lib/test/test_email/test_email.py
@@ -4010,6 +4010,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
index 0000000..3bbae23
--- /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.