[Python-checkins] cpython (3.5): 27988: Make sure iter_attachments does not mutate the payload list.

r.david.murray python-checkins at python.org
Wed Sep 7 13:41:40 EDT 2016


https://hg.python.org/cpython/rev/3bf2f6818719
changeset:   103231:3bf2f6818719
branch:      3.5
parent:      103226:096dfac57e44
user:        R David Murray <rdmurray at bitdance.com>
date:        Wed Sep 07 13:39:36 2016 -0400
summary:
  27988: Make sure iter_attachments does not mutate the payload list.

files:
  Lib/email/message.py                |   2 +-
  Lib/test/test_email/test_message.py |  10 ++++++++++
  Misc/NEWS                           |   2 ++
  3 files changed, 13 insertions(+), 1 deletions(-)


diff --git a/Lib/email/message.py b/Lib/email/message.py
--- a/Lib/email/message.py
+++ b/Lib/email/message.py
@@ -1022,7 +1022,7 @@
         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
diff --git a/Lib/test/test_email/test_message.py b/Lib/test/test_email/test_message.py
--- a/Lib/test/test_email/test_message.py
+++ b/Lib/test/test_email/test_message.py
@@ -732,6 +732,16 @@
         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
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -62,6 +62,8 @@
 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.
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list