Skip to content

[3.14] gh-134759: fix UnboundLocalError in email.message.Message.get_payload (GH-136071) #136579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
gh-134759: fix UnboundLocalError in `email.message.Message.get_payl…
…oad` (GH-136071)

(cherry picked from commit 25335d2)

Co-authored-by: Kliment Lamonov <klimentlamonov@yandex.ru>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
  • Loading branch information
2 people authored and miss-islington committed Jul 12, 2025
commit 9853a5968b4f8140ab828279fa6ef96a8b2fabb2
2 changes: 2 additions & 0 deletions Lib/email/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ def get_payload(self, i=None, decode=False):
# 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':
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_email/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,15 @@ def test_get_body_malformed(self):
# 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
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,7 @@ Alexander Lakeev
David Lam
Thomas Lamb
Valerie Lambert
Kliment Lamonov
Peter Lamut
Jean-Baptiste "Jiba" Lamy
Ronan Lamy
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Loading