Index: Lib/email/test/test_email.py =================================================================== --- Lib/email/test/test_email.py (revision 77711) +++ Lib/email/test/test_email.py (working copy) @@ -103,6 +103,15 @@ eq(msg['content-type'], 'text/x-weird; charset="iso-8859-1"') eq(msg['content-transfer-encoding'], 'quinted-puntable') + def test_set_charset_non_text_maintype(self): + msg=MIMEMultipart() + msg.attach(MIMEText('foo','plain')) + try: + msg.set_charset('utf-8') + self.fail('Should not be able to successfully change the charset on a message not of maintype text/* .') + except TypeError: + pass + def test_set_charset_from_string(self): eq = self.assertEqual msg = Message() Index: Lib/email/message.py =================================================================== --- Lib/email/message.py (revision 77711) +++ Lib/email/message.py (working copy) @@ -238,6 +238,8 @@ Content-Type, Content-Transfer-Encoding) will be added as needed. """ + if self.get_content_maintype()!='text': + raise TypeError('set_charset() called on a message not of type text/*') if charset is None: self.del_param('charset') self._charset = None