]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
27988: Make sure iter_attachments does not mutate the payload list.
authorR David Murray <rdmurray@bitdance.com>
Wed, 7 Sep 2016 17:39:36 +0000 (13:39 -0400)
committerR David Murray <rdmurray@bitdance.com>
Wed, 7 Sep 2016 17:39:36 +0000 (13:39 -0400)
Lib/email/message.py
Lib/test/test_email/test_message.py
Misc/NEWS

index aefaf57d00e0d2ec251c706e91a4803c721bf98b..6cd6cb77c08065847472483cb53b9457f69c255f 100644 (file)
@@ -1022,7 +1022,7 @@ class MIMEPart(Message):
         maintype, subtype = self.get_content_type().split('/')
         if maintype != 'multipart' or subtype == 'alternative':
             return
-        parts = self.get_payload()
+        parts = self.get_payload().copy()
         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
index d78049e315d04b1f7ddfaa03e49dffea7b6da951..434516226cae08a628eb78a80b6c3ccc8da1658a 100644 (file)
@@ -732,6 +732,16 @@ class TestEmailMessageBase:
         m.set_param('filename', 'abc.png', 'Content-Disposition')
         self.assertTrue(m.is_attachment())
 
+    def test_iter_attachments_mutation(self):
+        # We had a bug where iter_attachments was mutating the list.
+        m = self._make_message()
+        m.set_content('arbitrary text as main part')
+        m.add_related('more text as a related part')
+        m.add_related('yet more text as a second "attachment"')
+        orig = m.get_payload().copy()
+        self.assertEqual(len(list(m.iter_attachments())), 2)
+        self.assertEqual(m.get_payload(), orig)
+
 
 class TestEmailMessage(TestEmailMessageBase, TestEmailBase):
     message = EmailMessage
index 7b4f657879c2c40d2cddbf3546de58cb7dcf2f9f..bfc693aaf05f08803997f4eb8291601d5b550d7a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -62,6 +62,8 @@ Core and Builtins
 Library
 -------
 
+- Issue 27988: Fix email iter_attachments incorrect mutation of payload list.
+
 - Issue #27691: Fix ssl module's parsing of GEN_RID subject alternative name
   fields in X.509 certs.