summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2014-03-06 16:44:17 (GMT)
committerR David Murray <rdmurray@bitdance.com>2014-03-06 16:44:17 (GMT)
commit5dda12491e3e2233b63737a1c5f6066f54f6deb5 (patch)
treeadb09127fa2582cf98e47f3e643b7ba7f8a36001
parent733e50ad9ee2885323c39080b42716fa5d1fd8c1 (diff)
downloadcpython-5dda12491e3e2233b63737a1c5f6066f54f6deb5.zip
cpython-5dda12491e3e2233b63737a1c5f6066f54f6deb5.tar.gz
cpython-5dda12491e3e2233b63737a1c5f6066f54f6deb5.tar.bz2
#11558: Better message if attach called on non-multipart.
Original patch by Varun Sharma.
-rw-r--r--Lib/email/message.py6
-rw-r--r--Lib/test/test_email/test_email.py8
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS4
4 files changed, 18 insertions, 1 deletions
diff --git a/Lib/email/message.py b/Lib/email/message.py
index 88b5fa3..b4bc8cb 100644
--- a/Lib/email/message.py
+++ b/Lib/email/message.py
@@ -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.
diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py
index 26ed96c..2f89a10 100644
--- a/Lib/test/test_email/test_email.py
+++ b/Lib/test/test_email/test_email.py
@@ -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
diff --git a/Misc/ACKS b/Misc/ACKS
index dbf83e6..9c559ec 100644
--- 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
diff --git a/Misc/NEWS b/Misc/NEWS
index 37628d6..d6db677 100644
--- 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.