diff options
-rw-r--r-- | Lib/email/message.py | 11 | ||||
-rw-r--r-- | Lib/test/test_email/test_message.py | 9 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2019-06-15-14-39-50.bpo-33972.XxnNPw.rst | 2 |
3 files changed, 21 insertions, 1 deletions
diff --git a/Lib/email/message.py b/Lib/email/message.py index b6512f2..1262602 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 diff --git a/Lib/test/test_email/test_message.py b/Lib/test/test_email/test_message.py index 5dc46e1..fab97d9 100644 --- a/Lib/test/test_email/test_message.py +++ b/Lib/test/test_email/test_message.py @@ -929,6 +929,15 @@ class TestMIMEPart(TestEmailMessageBase, TestEmailBase): m.set_content(content_manager=cm) self.assertNotIn('MIME-Version', m) + def test_string_payload_with_multipart_content_type(self): + msg = message_from_string(textwrap.dedent("""\ + Content-Type: multipart/mixed; charset="utf-8" + + sample text + """), policy=policy.default) + attachments = msg.iter_attachments() + self.assertEqual(list(attachments), []) + if __name__ == '__main__': unittest.main() diff --git a/Misc/NEWS.d/next/Library/2019-06-15-14-39-50.bpo-33972.XxnNPw.rst b/Misc/NEWS.d/next/Library/2019-06-15-14-39-50.bpo-33972.XxnNPw.rst new file mode 100644 index 0000000..ded1570 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-06-15-14-39-50.bpo-33972.XxnNPw.rst @@ -0,0 +1,2 @@ +Email with single part but content-type set to ``multipart/*`` doesn't raise +AttributeError anymore. |