[3.7] bpo-33972: Fix EmailMessage.iter_attachments raising AttributeE… · python/cpython@01b2394 (original) (raw)

Original file line number Diff line number Diff line change
@@ -1041,7 +1041,16 @@ def iter_attachments(self):
1041 1041 maintype, subtype = self.get_content_type().split('/')
1042 1042 if maintype != 'multipart' or subtype == 'alternative':
1043 1043 return
1044 -parts = self.get_payload().copy()
1044 +payload = self.get_payload()
1045 +# Certain malformed messages can have content type set to `multipart/*`
1046 +# but still have single part body, in which case payload.copy() can
1047 +# fail with AttributeError.
1048 +try:
1049 +parts = payload.copy()
1050 +except AttributeError:
1051 +# payload is not a list, it is most probably a string.
1052 +return
1053 +
1045 1054 if maintype == 'multipart' and subtype == 'related':
1046 1055 # For related, we treat everything but the root as an attachment.
1047 1056 # The root may be indicated by 'start'; if there's no start or we