classification
Title: AttributeError in email.message.iter_attachments()
Type: behavior Stage: patch review
Components: email Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: barry, miss-islington, r.david.murray, skrohlas, stoksc
Priority: normal Keywords: patch

Created on 2018-06-26 14:43 by skrohlas, last changed 2019-06-25 18:40 by miss-islington.

Pull Requests
URL Status Linked Edit
PR 8041 closed python-dev, 2018-07-02 05:35
PR 14119 merged maxking, 2019-06-15 18:41
PR 14380 merged maxking, 2019-06-25 18:21
PR 14381 merged maxking, 2019-06-25 18:21
Messages (5)
msg320494 - (view) Author: Sven Krohlas (skrohlas) Date: 2018-06-26 14:43
Hello everyone,

Today I stumbled over unexpected behaviour when parsing emails with Python 3.5.

mail is of type: <class 'email.message.EmailMessage'>


Traceback (most recent call last):
File "XXX", line YYY, in ZZZ
for attachment in mail.iter_attachments():
File "/usr/lib/python3.5/email/message.py", line 1025, in iter_attachments
parts = self.get_payload().copy()
AttributeError: 'str' object has no attribute 'copy'


/usr/lib/python3.5/email/message.py calls self.get_payload().copy() without distinguishing between different possible return types of get_payload().

get_payload() belongs to class MIMEPart(Message)

From the (base) class Message:


"def get_payload(self, i=None, decode=False):
"""Return a reference to the payload.

The payload will either be a list object or a string."

So, it might return a string, which seems to be the case I'm hitting here.

The code in cpython trunk still looks identical apart from a few line offsets, so newer versions might be affected, too.

Am I doing something wrong or do we have a bug to squash here?

Thanks,
Sven
msg320852 - (view) Author: stoksc (stoksc) * Date: 2018-07-02 05:39
Hey Sven,

New here, decided to try a bit at an issue. 

According to the email.message docs, "the payload is either a string or bytes object, in the case of simple message objects, or a list of EmailMessage objects, for MIME container documents such as multipart/* and message/rfc822 message objects."

Like you said, this error happens if the email.message.EmailMessage object is bad, i.e. the payload is a string or bytes but the content-type is marked as multipart/*. After reading online, it seems like an error is necessary.

I've proposed a small change in a pull request to make this situation raise a more useful error. It doesn't blow up any earlier but it at least says something more useful. 

The function could return instead of throwing an error, just providing an empty iterator, but that seems even less useful.

I fixed it on master (3.8 dev or whatever).

Best,
Brad
msg346540 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2019-06-25 17:03
New changeset 02257012f6d3821d816cb6a7e8461a88a05b9a08 by Barry Warsaw (Abhilash Raj) in branch 'master':
bpo-33972: Fix EmailMessage.iter_attachments raising AttributeError. (GH-14119)
https://github.com/python/cpython/commit/02257012f6d3821d816cb6a7e8461a88a05b9a08
msg346546 - (view) Author: miss-islington (miss-islington) Date: 2019-06-25 18:38
New changeset c6e32824cf038386174fe2b9e7273e4779c9958c by Miss Islington (bot) (Abhilash Raj) in branch '3.8':
[3.8] bpo-33972: Fix EmailMessage.iter_attachments raising AttributeError (GH-14119) (GH-14380)
https://github.com/python/cpython/commit/c6e32824cf038386174fe2b9e7273e4779c9958c
msg346547 - (view) Author: miss-islington (miss-islington) Date: 2019-06-25 18:40
New changeset 01b23948038d35b119cededd149110231ecce00d by Miss Islington (bot) (Abhilash Raj) in branch '3.7':
[3.7] bpo-33972: Fix EmailMessage.iter_attachments raising AttributeError. (GH-14119) (GH-14381)
https://github.com/python/cpython/commit/01b23948038d35b119cededd149110231ecce00d
History
Date User Action Args
2019-06-25 18:40:44miss-islingtonsetmessages: + msg346547
2019-06-25 18:38:56miss-islingtonsetnosy: + miss-islington
messages: + msg346546
2019-06-25 18:21:37maxkingsetpull_requests: + pull_request14195
2019-06-25 18:21:11maxkingsetpull_requests: + pull_request14194
2019-06-25 17:03:33barrysetmessages: + msg346540
2019-06-15 18:41:46maxkingsetpull_requests: + pull_request13968
2018-07-11 07:56:34serhiy.storchakasettype: crash -> behavior
2018-07-02 05:39:42stokscsetnosy: + stoksc

messages: + msg320852
versions: + Python 3.8, - Python 3.5
2018-07-02 05:35:21python-devsetkeywords: + patch
stage: patch review
pull_requests: + pull_request7650
2018-06-26 14:43:31skrohlascreate