]> granicus.if.org Git - python/commitdiff
#11558: Better message if attach called on non-multipart.
authorR David Murray <rdmurray@bitdance.com>
Thu, 6 Mar 2014 16:44:17 +0000 (11:44 -0500)
committerR David Murray <rdmurray@bitdance.com>
Thu, 6 Mar 2014 16:44:17 +0000 (11:44 -0500)
Original patch by Varun Sharma.

Lib/email/message.py
Lib/test/test_email/test_email.py
Misc/ACKS
Misc/NEWS

index 88b5fa3552404744590fd693949710a44a32958b..b4bc8cbc9e54ee56fd8b6c377d00f861bc5742c9 100644 (file)
@@ -203,7 +203,11 @@ class Message:
         if self._payload is None:
             self._payload = [payload]
         else:
-            self._payload.append(payload)
+            try:
+                self._payload.append(payload)
+            except AttributeError:
+                raise TypeError("Attach is not valid on a message with a"
+                                " non-multipart payload")
 
     def get_payload(self, i=None, decode=False):
         """Return a reference to the payload.
index 26ed96ccc4ff635c4cbad1d5fa68509f30e167e3..2f89a10e0a736153553aaf8efaf37fa74ff1f8dd 100644 (file)
@@ -124,6 +124,14 @@ class TestMessageAPI(TestEmailBase):
         msg.set_payload([])
         self.assertEqual(msg.get_payload(), [])
 
+    def test_attach_when_payload_is_string(self):
+        msg = Message()
+        msg['Content-Type'] = 'multipart/mixed'
+        msg.set_payload('string payload')
+        sub_msg = MIMEMessage(Message())
+        self.assertRaisesRegex(TypeError, "[Aa]ttach.*non-multipart",
+                               msg.attach, sub_msg)
+
     def test_get_charsets(self):
         eq = self.assertEqual
 
index dbf83e67a774c3f166e737fe28d88080979055fc..9c559ec238c33883c1e544af3c458e316c6de91e 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1188,6 +1188,7 @@ Daniel Shahaf
 Ha Shao
 Mark Shannon
 Richard Shapiro
+Varun Sharma
 Vlad Shcherbina
 Justin Sheehy
 Charlie Shepherd
index 37628d633f6cbf7a02b261f39670e89e597c4471..d6db677670d0915a5dca964180459a8ba1a6d335 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -20,6 +20,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #11558: ``email.message.Message.attach`` now returns a more
+  useful error message if ``attach`` is called on a message for which
+  ``is_multipart`` is False.
+
 - Issue #20283: RE pattern methods now accept the string keyword parameters
   as documented.  The pattern and source keyword parameters are left as
   deprecated aliases.