New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AttributeError in email.message.iter_attachments() #78153
Comments
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): 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, |
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, |
This seems complete, can it be closed? |
Thank you all for the fix. Closing. |
The problem still occurs with the _find_body https://github.com/python/cpython/blob/3.9/Lib/email/message.py#L978 |
Xavier, I think it would be best if you could open a new issue for that, and also include code to reproduce the problem. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: