Skip to content

Commit 8a8c015

Browse files
committed
Cline fix for get_payload UnboundLocalError
1 parent 33f561d commit 8a8c015

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

Lib/email/message.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2001 Python Software Foundation
1+
# Copyright (C) 2001-2007 Python Software Foundation
22
# Author: Barry Warsaw
33
# Contact: email-sig@python.org
44

@@ -21,7 +21,7 @@
2121

2222
SEMISPACE = '; '
2323

24-
# Regular expression that matches 'special' characters in parameters, the
24+
# Regular expression that matches `special' characters in parameters, the
2525
# existence of which force quoting of the parameter value.
2626
tspecials = re.compile(r'[ \(\)<>@,;:\\"/\[\]\?=]')
2727

@@ -141,7 +141,7 @@ class Message:
141141
multipart or a message/rfc822), then the payload is a list of Message
142142
objects, otherwise it is a string.
143143
144-
Message objects implement part of the 'mapping' interface, which assumes
144+
Message objects implement part of the `mapping' interface, which assumes
145145
there is exactly one occurrence of the header per message. Some headers
146146
do in fact appear multiple times (e.g. Received) and for those headers,
147147
you must use the explicit API to set or get all the headers. Not all of
@@ -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':
@@ -601,7 +603,7 @@ def get_content_type(self):
601603
"""Return the message's content type.
602604
603605
The returned string is coerced to lower case of the form
604-
'maintype/subtype'. If there was no Content-Type header in the
606+
`maintype/subtype'. If there was no Content-Type header in the
605607
message, the default type as given by get_default_type() will be
606608
returned. Since according to RFC 2045, messages always have a default
607609
type this will always return a value.
@@ -624,7 +626,7 @@ def get_content_type(self):
624626
def get_content_maintype(self):
625627
"""Return the message's main content type.
626628
627-
This is the 'maintype' part of the string returned by
629+
This is the `maintype' part of the string returned by
628630
get_content_type().
629631
"""
630632
ctype = self.get_content_type()
@@ -633,14 +635,14 @@ def get_content_maintype(self):
633635
def get_content_subtype(self):
634636
"""Returns the message's sub-content type.
635637
636-
This is the 'subtype' part of the string returned by
638+
This is the `subtype' part of the string returned by
637639
get_content_type().
638640
"""
639641
ctype = self.get_content_type()
640642
return ctype.split('/')[1]
641643

642644
def get_default_type(self):
643-
"""Return the 'default' content type.
645+
"""Return the `default' content type.
644646
645647
Most messages have a default content type of text/plain, except for
646648
messages that are subparts of multipart/digest containers. Such
@@ -649,7 +651,7 @@ def get_default_type(self):
649651
return self._default_type
650652

651653
def set_default_type(self, ctype):
652-
"""Set the 'default' content type.
654+
"""Set the `default' content type.
653655
654656
ctype should be either "text/plain" or "message/rfc822", although this
655657
is not enforced. The default content type is not stored in the
@@ -682,8 +684,8 @@ def get_params(self, failobj=None, header='content-type', unquote=True):
682684
"""Return the message's Content-Type parameters, as a list.
683685
684686
The elements of the returned list are 2-tuples of key/value pairs, as
685-
split on the '=' sign. The left hand side of the '=' is the key,
686-
while the right hand side is the value. If there is no '=' sign in
687+
split on the `=' sign. The left hand side of the `=' is the key,
688+
while the right hand side is the value. If there is no `=' sign in
687689
the parameter the value is the empty string. The value is as
688690
described in the get_param() method.
689691
@@ -843,9 +845,9 @@ def get_filename(self, failobj=None):
843845
"""Return the filename associated with the payload if present.
844846
845847
The filename is extracted from the Content-Disposition header's
846-
'filename' parameter, and it is unquoted. If that header is missing
847-
the 'filename' parameter, this method falls back to looking for the
848-
'name' parameter.
848+
`filename' parameter, and it is unquoted. If that header is missing
849+
the `filename' parameter, this method falls back to looking for the
850+
`name' parameter.
849851
"""
850852
missing = object()
851853
filename = self.get_param('filename', missing, 'content-disposition')
@@ -858,7 +860,7 @@ def get_filename(self, failobj=None):
858860
def get_boundary(self, failobj=None):
859861
"""Return the boundary associated with the payload if present.
860862
861-
The boundary is extracted from the Content-Type header's 'boundary'
863+
The boundary is extracted from the Content-Type header's `boundary'
862864
parameter, and it is unquoted.
863865
"""
864866
missing = object()

Lib/test/test_email/test_message.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,14 @@ def test_get_body_malformed(self):
10541054
# In bpo-42892, this would raise
10551055
# AttributeError: 'str' object has no attribute 'is_attachment'
10561056
m.get_body()
1057-
1057+
def test_get_bytes_payload_with_quoted_printable_encoding(self):
1058+
# We use a memoryview to avoid directly changing the private payload
1059+
# and to prevent using the dedicated paths for string or bytes objects.
1060+
payload = memoryview(b'Some payload')
1061+
m = self._make_message()
1062+
m.add_header('Content-Transfer-Encoding', 'quoted-printable')
1063+
m.set_payload(payload)
1064+
self.assertEqual(m.get_payload(decode=True), payload)
10581065

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

0 commit comments

Comments
 (0)