Skip to content

Commit 25335d2

Browse files
skv0zsnegpicnixz
andauthored
pythongh-134759: fix UnboundLocalError in email.message.Message.get_payload (python#136071)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent 5a20e79 commit 25335d2

File tree

4 files changed

+14
-0
lines changed

4 files changed

+14
-0
lines changed

Lib/email/message.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ def get_payload(self, i=None, decode=False):
313313
# If it does happen, turn the string into bytes in a way
314314
# guaranteed not to fail.
315315
bpayload = payload.encode('raw-unicode-escape')
316+
else:
317+
bpayload = payload
316318
if cte == 'quoted-printable':
317319
return quopri.decodestring(bpayload)
318320
elif cte == 'base64':

Lib/test/test_email/test_message.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,15 @@ def test_get_body_malformed(self):
10551055
# AttributeError: 'str' object has no attribute 'is_attachment'
10561056
m.get_body()
10571057

1058+
def test_get_bytes_payload_with_quoted_printable_encoding(self):
1059+
# We use a memoryview to avoid directly changing the private payload
1060+
# and to prevent using the dedicated paths for string or bytes objects.
1061+
payload = memoryview(b'Some payload')
1062+
m = self._make_message()
1063+
m.add_header('Content-Transfer-Encoding', 'quoted-printable')
1064+
m.set_payload(payload)
1065+
self.assertEqual(m.get_payload(decode=True), payload)
1066+
10581067

10591068
class TestMIMEPart(TestEmailMessageBase, TestEmailBase):
10601069
# Doing the full test run here may seem a bit redundant, since the two

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,7 @@ Alexander Lakeev
10551055
David Lam
10561056
Thomas Lamb
10571057
Valerie Lambert
1058+
Kliment Lamonov
10581059
Peter Lamut
10591060
Jean-Baptiste "Jiba" Lamy
10601061
Ronan Lamy
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :exc:`UnboundLocalError` in :func:`email.message.Message.get_payload` when
2+
the payload to decode is a :class:`bytes` object. Patch by Kliment Lamonov.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy