This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: MIMEText __init__ does not support Charset instance
Type: enhancement Stage: resolved
Components: email Versions: Python 3.5
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: berker.peksag Nosy List: barry, berker.peksag, claudep, python-dev, r.david.murray
Priority: normal Keywords: patch

Created on 2012-10-25 15:49 by claudep, last changed 2022-04-11 14:57 by admin. This issue is now closed.

File name Uploaded Description Edit
issue16324-1.diff claudep, 2012-10-26 12:18 Allow Charset to be passed to MIMEText review
issue16324_v2.diff berker.peksag, 2014-09-26 13:09 review
Messages (7)
msg173764 - (view) Author: Claude Paroz (claudep) Date: 2012-10-25 15:49
When initializing a MIMEText instance, it might be desirable to set the _charset parameter to a real Charset instance, not only a charset identifier (for example to pass a Charset with customized body_encoding). Unfortunately, this is failing:

  File ".../django/core/mail/", line 128, in __init__
    MIMEText.__init__(self, text, subtype, charset)
  File "/usr/lib/python3.2/email/mime/", line 29, in __init__
    **{'charset': _charset})
  File "/usr/lib/python3.2/email/mime/", line 25, in __init__
    self.add_header('Content-Type', ctype, **_params)
  File "/usr/lib/python3.2/email/", line 475, in add_header
    parts.append(_formatparam(k.replace('_', '-'), v))
  File "/usr/lib/python3.2/email/", line 67, in _formatparam
    if value is not None and len(value) > 0:
TypeError: object of type 'Charset' has no len()

It is possible to later call set_charset, but the payload is already encoded (and 'Content-Transfer-Encoding' is set).
Did I miss anything?
msg173793 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-10-25 19:08
I don't think you missed anything.  It doesn't look like this has ever worked, but you'd certainly think it would.  The documentation doesn't claim anything about it one way or another.  That probably means we should treat it as an enhancement rather than a bug fix, but I'm open to argument on that.
msg173794 - (view) Author: Claude Paroz (claudep) Date: 2012-10-25 19:16
It's fine for me to consider it as an enhancement. The fix might be as simple as replacing **{'charset': _charset} by **{'charset': str(_charset)} in MIMEText __init__.
msg227615 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2014-09-26 13:09
Here's an updated patch.
msg227617 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-09-26 14:05
The updated patch looks good to me.  Go ahead and commit it.
msg227660 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-09-26 21:57
New changeset d43d4d4ebf2c by Berker Peksag in branch 'default':
Issue #16324: _charset parameter of MIMEText now also accepts email.charset.Charset instances.
msg227661 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2014-09-26 21:58
Thanks for the patch, Claude and thanks for the review, David!
Date User Action Args
2022-04-11 14:57:37adminsetgithub: 60528
2014-09-26 21:58:57berker.peksagsetstatus: open -> closed
messages: + msg227661

assignee: berker.peksag
resolution: fixed
stage: patch review -> resolved
2014-09-26 21:57:24python-devsetnosy: + python-dev
messages: + msg227660
2014-09-26 14:05:02r.david.murraysetmessages: + msg227617
2014-09-26 13:09:19berker.peksagsetfiles: + issue16324_v2.diff

type: behavior -> enhancement
versions: + Python 3.5, - Python 3.2
nosy: + berker.peksag

messages: + msg227615
stage: patch review
2012-10-26 12:18:40claudepsetfiles: + issue16324-1.diff
keywords: + patch
2012-10-25 19:16:55claudepsetmessages: + msg173794
2012-10-25 19:08:31r.david.murraysetmessages: + msg173793
2012-10-25 15:49:32claudepcreate