]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-134759: fix `UnboundLocalError` in `email.message.Message.get_payload`...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 12 Jul 2025 13:52:54 +0000 (15:52 +0200)
committerGitHub <noreply@github.com>
Sat, 12 Jul 2025 13:52:54 +0000 (13:52 +0000)
gh-134759: fix `UnboundLocalError` in `email.message.Message.get_payload` (GH-136071)
(cherry picked from commit 25335d297b5248922a4c82183bcdf0c0ada8352b)

Co-authored-by: Kliment Lamonov <klimentlamonov@yandex.ru>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Lib/email/message.py
Lib/test/test_email/test_message.py
Misc/ACKS
Misc/NEWS.d/next/Library/2025-06-28-11-32-57.gh-issue-134759.AjjKcG.rst [new file with mode: 0644]

index f48fcd2ea925daec1fe2b1fc7923c64a82870fed..37771f97b0c8a6474832b564525d6510e092f98b 100644 (file)
@@ -313,6 +313,8 @@ class Message:
                 # If it does happen, turn the string into bytes in a way
                 # guaranteed not to fail.
                 bpayload = payload.encode('raw-unicode-escape')
+        else:
+            bpayload = payload
         if cte == 'quoted-printable':
             return quopri.decodestring(bpayload)
         elif cte == 'base64':
index 96979db27f3a21b681df87aa7b727912b0f8fcc8..411524fef66252469fcc84a1ecfd1e617230d5fc 100644 (file)
@@ -1031,6 +1031,15 @@ class TestEmailMessage(TestEmailMessageBase, TestEmailBase):
         # AttributeError: 'str' object has no attribute 'is_attachment'
         m.get_body()
 
+    def test_get_bytes_payload_with_quoted_printable_encoding(self):
+        # We use a memoryview to avoid directly changing the private payload
+        # and to prevent using the dedicated paths for string or bytes objects.
+        payload = memoryview(b'Some payload')
+        m = self._make_message()
+        m.add_header('Content-Transfer-Encoding', 'quoted-printable')
+        m.set_payload(payload)
+        self.assertEqual(m.get_payload(decode=True), payload)
+
 
 class TestMIMEPart(TestEmailMessageBase, TestEmailBase):
     # Doing the full test run here may seem a bit redundant, since the two
index d0aaf75ebcc1f7bdd26a435e2d58bccaa5b1c52c..6cc30812cced119143105d83bafede7fe42df6db 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1040,6 +1040,7 @@ Alexander Lakeev
 David Lam
 Thomas Lamb
 Valerie Lambert
+Kliment Lamonov
 Peter Lamut
 Jean-Baptiste "Jiba" Lamy
 Ronan Lamy
diff --git a/Misc/NEWS.d/next/Library/2025-06-28-11-32-57.gh-issue-134759.AjjKcG.rst b/Misc/NEWS.d/next/Library/2025-06-28-11-32-57.gh-issue-134759.AjjKcG.rst
new file mode 100644 (file)
index 0000000..79b8532
--- /dev/null
@@ -0,0 +1,2 @@
+Fix :exc:`UnboundLocalError` in :func:`email.message.Message.get_payload` when
+the payload to decode is a :class:`bytes` object. Patch by Kliment Lamonov.