Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5dda124

Browse files
committed
#11558: Better message if attach called on non-multipart.
Original patch by Varun Sharma.
1 parent 733e50a commit 5dda124

4 files changed

Lines changed: 18 additions & 1 deletion

File tree

Lib/email/message.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,11 @@ def attach(self, payload):
203203
if self._payload is None:
204204
self._payload = [payload]
205205
else:
206-
self._payload.append(payload)
206+
try:
207+
self._payload.append(payload)
208+
except AttributeError:
209+
raise TypeError("Attach is not valid on a message with a"
210+
" non-multipart payload")
207211

208212
def get_payload(self, i=None, decode=False):
209213
"""Return a reference to the payload.

Lib/test/test_email/test_email.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ def test_set_payload_to_list(self):
124124
msg.set_payload([])
125125
self.assertEqual(msg.get_payload(), [])
126126

127+
def test_attach_when_payload_is_string(self):
128+
msg = Message()
129+
msg['Content-Type'] = 'multipart/mixed'
130+
msg.set_payload('string payload')
131+
sub_msg = MIMEMessage(Message())
132+
self.assertRaisesRegex(TypeError, "[Aa]ttach.*non-multipart",
133+
msg.attach, sub_msg)
134+
127135
def test_get_charsets(self):
128136
eq = self.assertEqual
129137

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,7 @@ Daniel Shahaf
11881188
Ha Shao
11891189
Mark Shannon
11901190
Richard Shapiro
1191+
Varun Sharma
11911192
Vlad Shcherbina
11921193
Justin Sheehy
11931194
Charlie Shepherd

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ Core and Builtins
2020
Library
2121
-------
2222

23+
- Issue #11558: ``email.message.Message.attach`` now returns a more
24+
useful error message if ``attach`` is called on a message for which
25+
``is_multipart`` is False.
26+
2327
- Issue #20283: RE pattern methods now accept the string keyword parameters
2428
as documented. The pattern and source keyword parameters are left as
2529
deprecated aliases.

0 commit comments

Comments
 (0)