changeset: 89490:302c8fdb17e3 user: R David Murray date: Thu Mar 06 11:44:17 2014 -0500 files: Lib/email/message.py Lib/test/test_email/test_email.py Misc/ACKS Misc/NEWS description: #11558: Better message if attach called on non-multipart. Original patch by Varun Sharma. diff -r b07400659dba -r 302c8fdb17e3 Lib/email/message.py --- a/Lib/email/message.py Thu Mar 06 17:06:41 2014 +0100 +++ b/Lib/email/message.py Thu Mar 06 11:44:17 2014 -0500 @@ -203,7 +203,11 @@ 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 -r b07400659dba -r 302c8fdb17e3 Lib/test/test_email/test_email.py --- a/Lib/test/test_email/test_email.py Thu Mar 06 17:06:41 2014 +0100 +++ b/Lib/test/test_email/test_email.py Thu Mar 06 11:44:17 2014 -0500 @@ -124,6 +124,14 @@ 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 -r b07400659dba -r 302c8fdb17e3 Misc/ACKS --- a/Misc/ACKS Thu Mar 06 17:06:41 2014 +0100 +++ b/Misc/ACKS Thu Mar 06 11:44:17 2014 -0500 @@ -1188,6 +1188,7 @@ Ha Shao Mark Shannon Richard Shapiro +Varun Sharma Vlad Shcherbina Justin Sheehy Charlie Shepherd diff -r b07400659dba -r 302c8fdb17e3 Misc/NEWS --- a/Misc/NEWS Thu Mar 06 17:06:41 2014 +0100 +++ b/Misc/NEWS Thu Mar 06 11:44:17 2014 -0500 @@ -20,6 +20,10 @@ 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.