Skip to content

Commit 6176101

Browse files
miss-islingtonskv0zsnegpicnixz
authored
[3.13] gh-134759: fix UnboundLocalError in email.message.Message.get_payload (GH-136071) (#136580)
gh-134759: fix `UnboundLocalError` in `email.message.Message.get_payload` (GH-136071) (cherry picked from commit 25335d2) Co-authored-by: Kliment Lamonov <[email protected]> Co-authored-by: Bénédikt Tran <[email protected]>
1 parent 01710af commit 6176101

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
@@ -1031,6 +1031,15 @@ def test_get_body_malformed(self):
10311031
# AttributeError: 'str' object has no attribute 'is_attachment'
10321032
m.get_body()
10331033

1034+
def test_get_bytes_payload_with_quoted_printable_encoding(self):
1035+
# We use a memoryview to avoid directly changing the private payload
1036+
# and to prevent using the dedicated paths for string or bytes objects.
1037+
payload = memoryview(b'Some payload')
1038+
m = self._make_message()
1039+
m.add_header('Content-Transfer-Encoding', 'quoted-printable')
1040+
m.set_payload(payload)
1041+
self.assertEqual(m.get_payload(decode=True), payload)
1042+
10341043

10351044
class TestMIMEPart(TestEmailMessageBase, TestEmailBase):
10361045
# 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
@@ -1040,6 +1040,7 @@ Alexander Lakeev
10401040
David Lam
10411041
Thomas Lamb
10421042
Valerie Lambert
1043+
Kliment Lamonov
10431044
Peter Lamut
10441045
Jean-Baptiste "Jiba" Lamy
10451046
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)