classification
Title: Exception raised when attempting to call set_charset on an email.mime.multipart once sub-parts have been attached
Type: behavior Stage: resolved
Components: Versions: Python 2.7, Python 2.6
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Possible to set invalid Content-Transfer-Encoding on email.mime.multipart.MIMEMultipart
View: 1823
Assigned To: r.david.murray Nosy List: Sharebear, barry, christian.heimes, cjw296, loewis, pocketcookies, r.david.murray
Priority: normal Keywords: easy, patch

Created on 2009-03-05 13:01 by cjw296, last changed 2010-12-28 05:01 by r.david.murray. This issue is now closed.

Files
File name Uploaded Description Edit
issue5423.patch pocketcookies, 2010-01-23 20:27 Prints a more relevant error message when calling set_charset on a message not of type text/*. Also includes a test case for this. review
Messages (3)
msg83193 - (view) Author: Chris Withers (cjw296) * (Python committer) Date: 2009-03-05 13:01
Hi All,

I'm splitting this out from [Issue1823] as it's a separate issue.

Here's a minimal case to reproduce:

>>> from email.MIMEMultipart import MIMEMultipart
>>> from email.MIMEText import MIMEText
>>> msg = MIMEMultipart()
>>> msg.attach(MIMEText('foo','plain'))
>>> msg.set_charset('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "C:\python24\lib\email\Message.py", line 260, in set_charset
    self._payload = charset.body_encode(self._payload)
  File "C:\python24\lib\email\Charset.py", line 366, in body_encode
    return email.base64MIME.body_encode(s)
  File "C:\python24\lib\email\base64MIME.py", line 136, in encode
    enc = b2a_base64(s[i:i + max_unencoded])
TypeError: b2a_base64() argument 1 must be string or read-only buffer, 
not list

NB: The exception raised can vary depending on the character set used:

>>> msg.set_charset('iso-8859-15')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "C:\python24\lib\email\Message.py", line 260, in set_charset
    self._payload = charset.body_encode(self._payload)
  File "C:\python24\lib\email\Charset.py", line 368, in body_encode
    return email.quopriMIME.body_encode(s)
  File "C:\python24\lib\email\quopriMIME.py", line 180, in encode
    body = fix_eols(body)
  File "C:\python24\lib\email\Utils.py", line 62, in fix_eols
    s = re.sub(r'(?<!\r)\n', CRLF, s)
  File "C:\python24\lib\sre.py", line 142, in sub
    return _compile(pattern, 0).sub(repl, string, count)
TypeError: expected string or buffer

Chris
msg98194 - (view) Author: John Edmonds (pocketcookies) Date: 2010-01-23 19:03
It looks like set_charset's documentation says that it assumes you are calling it on a message with a mimetype of text/*.  This case calls it on a message of type multipart/mixed.  Nonetheless, I am attaching a patch that catches this earlier and prints a more relevant and consistent error message.
msg124762 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-12-28 05:01
Since set_charset should not be valid on a multipart, I don't see a reason to have a separate issue for the post-attach case.

Also, although I haven't searched the RFCs, I don't think we can assume that set_chaset is valid only on text parts, so I don't think the patch will work as a solution.
History
Date User Action Args
2010-12-28 05:01:54r.david.murraysetstatus: open -> closed
nosy: loewis, barry, christian.heimes, cjw296, Sharebear, r.david.murray, pocketcookies
messages: + msg124762

superseder: Possible to set invalid Content-Transfer-Encoding on email.mime.multipart.MIMEMultipart
resolution: duplicate
stage: patch review -> resolved
2010-06-03 00:42:35r.david.murraysetassignee: r.david.murray

nosy: + r.david.murray
2010-01-23 22:23:27ezio.melottisetpriority: normal
stage: test needed -> patch review
versions: + Python 2.7
2010-01-23 20:28:07pocketcookiessetfiles: - issue5423.patch
2010-01-23 20:27:54pocketcookiessetfiles: + issue5423.patch
2010-01-23 19:03:37pocketcookiessetfiles: + issue5423.patch

nosy: + pocketcookies
messages: + msg98194

keywords: + patch
2009-04-22 14:38:43ajaksu2setkeywords: + easy
stage: test needed
type: behavior
versions: + Python 2.6
2009-03-05 13:01:11cjw296create