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: AttributeError in email.message.iter_attachments()
Type: behavior Stage: resolved
Components: email Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: barry, iritkatriel, miss-islington, r.david.murray, skrohlas, stoksc, xavier2
Priority: normal Keywords: patch

Created on 2018-06-26 14:43 by skrohlas, last changed 2022-04-11 14:59 by admin. This issue is now closed.

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 (9)
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
msg378378 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-10-10 10:35
This seems complete, can it be closed?
msg379028 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2020-10-19 21:50
Thank you all for the fix.  Closing.
msg384835 - (view) Author: Xavier Hausherr (xavier2) * Date: 2021-01-11 14:46
The problem still occurs with the _find_body 
 method. The "part.is_attachment()" method can trigger an AttributeError too "AttributeError: 'str' object has no attribute 'is_attachment'"

https://github.com/python/cpython/blob/3.9/Lib/email/message.py#L978
msg384837 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-01-11 14:51
Xavier, I think it would be best if you could open a new issue for that, and also include code to reproduce the problem.
History
Date User Action Args
2022-04-11 14:59:02adminsetgithub: 78153
2021-01-11 14:51:45iritkatrielsetmessages: + msg384837
2021-01-11 14:46:31xavier2setnosy: + xavier2

messages: + msg384835
versions: + Python 3.9, - Python 3.8
2020-10-19 21:50:00barrysetstatus: open -> closed
resolution: fixed
messages: + msg379028

stage: patch review -> resolved
2020-10-10 10:35:32iritkatrielsetnosy: + iritkatriel
messages: + msg378378
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