Issue 14740: get_payload(n, True) returns None (original) (raw)

Issue14740

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:29 admin set github: 58945
2012-05-07 13:57:47 r.david.murray set status: open -> closednosy: + r.david.murraymessages: + resolution: not a bugstage: resolved
2012-05-07 13:37:44 Bob.Glickstein set messages: +
2012-05-07 13:36:32 Bob.Glickstein create