summaryrefslogtreecommitdiff
path: root/Lib/email
diff options
context:
space:
mode:
authorAbhilash Raj <maxking@users.noreply.github.com>2019-06-25 10:03:19 -0700
committerBarry Warsaw <barry@python.org>2019-06-25 10:03:19 -0700
commit02257012f6d3821d816cb6a7e8461a88a05b9a08 (patch)
tree62ddd6ec0b021a615997180039f90d829ff2edc6 /Lib/email
parentd7c87d982d4ec4ba201bcee14324ae5e0e90581f (diff)
downloadcpython-git-02257012f6d3821d816cb6a7e8461a88a05b9a08.tar.gz
bpo-33972: Fix EmailMessage.iter_attachments raising AttributeError. (GH-14119)
* bpo-33972: Fix EmailMessage.iter_attachments raising AttributeError. When certain malformed messages have content-type set to 'mulitpart/*' but still have a single part body, iter_attachments can raise AttributeError. This patch fixes it by returning a None value instead when the body is single part.
Diffstat (limited to 'Lib/email')
-rw-r--r--Lib/email/message.py11
1 files changed, 10 insertions, 1 deletions
diff --git a/Lib/email/message.py b/Lib/email/message.py
index b6512f2198..1262602617 100644
--- a/Lib/email/message.py
+++ b/Lib/email/message.py
@@ -1041,7 +1041,16 @@ class MIMEPart(Message):
maintype, subtype = self.get_content_type().split('/')
if maintype != 'multipart' or subtype == 'alternative':
return
- parts = self.get_payload().copy()
+ payload = self.get_payload()
+ # Certain malformed messages can have content type set to `multipart/*`
+ # but still have single part body, in which case payload.copy() can
+ # fail with AttributeError.
+ try:
+ parts = payload.copy()
+ except AttributeError:
+ # payload is not a list, it is most probably a string.
+ return
+
if maintype == 'multipart' and subtype == 'related':
# For related, we treat everything but the root as an attachment.
# The root may be indicated by 'start'; if there's no start or we