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.get_body() #87058
Comments
Following this issue: https://bugs.python.org/issue33972 Same bug apply to email.message.get_body() with attached email example and the following code: from email.policy import default
import email
with open('email_bad_formatted.eml', 'rb') as fp:
msg = email.message_from_binary_file(fp, policy=default)
body = msg.get_body()
/usr/local/lib/python3.9/email/message.py:978: AttributeError |
Attached PR fix the issue. |
Can you see how it happened that part is a str? |
Yes, that's the real question. That's what needs to be fixed, otherwise we'll just keep finding new bugs. For example, try calling iter_parts() on that message. It isn't pretty :) |
Actually, I'm wrong. The body of a part can be a string, and that's what's going to happen with a malformed body of something claiming to be a multipart. The problem is that there is code that doesn't guard against this possibility. The following patch against master fixes the bug listed here, as well as iter_parts(). But it causes one test suite failure so it isn't a correct patch as it stands: diff --git a/Lib/email/message.py b/Lib/email/message.py
index 3701b30553..d5d4a2385a 100644
--- a/Lib/email/message.py
+++ b/Lib/email/message.py
@@ -982,7 +982,7 @@ def _find_body(self, part, preferencelist):
if subtype in preferencelist:
yield (preferencelist.index(subtype), part)
return
- if maintype != 'multipart':
+ if maintype != 'multipart' or not self.is_multipart():
return
if subtype != 'related':
for subpart in part.iter_parts():
@@ -1087,7 +1087,7 @@ def iter_parts(self):
Return an empty iterator for a non-multipart.
"""
- if self.get_content_maintype() == 'multipart':
+ if self.is_multipart():
yield from self.get_payload()
def get_content(self, *args, content_manager=None, **kw): Maybe someone can take this and finish it (with tests)...I may or may not have time to get back to this. |
I'll try to fix the unit test today. |
Put up the PR here: #26903 |
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: