This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: get_payload(n, True) returns None
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Bob.Glickstein, r.david.murray
Priority: normal Keywords:

Created on 2012-05-07 13:36 by Bob.Glickstein, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg160144 - (view) Author: Bob Glickstein (Bob.Glickstein) * Date: 2012-05-07 13:36
Passing both a value for i and decode=True to email.message.Message.get_payload(), when self is a multipart, returns None when it should return a string.  The reason is that an is_multipart() test is done on self when it should instead be done on the selected payload part.  Here's a patch that fixes this:


--- /usr/lib64/python2.7/email/message.py	2011-10-26 18:40:36.000000000 -0700
+++ /home/bobg/tmp/message.py	2012-05-07 06:34:28.579401481 -0700
@@ -192,9 +192,9 @@
         else:
             payload = self._payload[i]
         if decode:
-            if self.is_multipart():
+            if payload.is_multipart():
                 return None
-            cte = self.get('content-transfer-encoding', '').lower()
+            cte = payload.get('content-transfer-encoding', '').lower()
             if cte == 'quoted-printable':
                 return utils._qdecode(payload)
             elif cte == 'base64':
msg160145 - (view) Author: Bob Glickstein (Bob.Glickstein) * Date: 2012-05-07 13:37
Incidentally, a workaround is:

msg.get_payload(n).get_payload(decode=True)
msg160146 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-05-07 13:57
Thanks for the report and patch suggestion, but...

This is actually the way it is designed to work.  get_payload(i) returns the ith element of a multipart payload.  Your workaround is in fact the correct way to do this operation.  decode=True is documented to return None if is_multipart is True.

You will note that if decode=False, you get back the Message sub-object, not a string.  Since decoding a Message object (as opposed to its payload) is not a meaningful operation, None is returned for decode=True.  Arguably we should raise a TypeError or ValueError instead, but we don't for historical reasons.
History
Date User Action Args
2022-04-11 14:57:29adminsetgithub: 58945
2012-05-07 13:57:47r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg160146

resolution: not a bug
stage: resolved
2012-05-07 13:37:44Bob.Glicksteinsetmessages: + msg160145
2012-05-07 13:36:32Bob.Glicksteincreate