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

Skip to content

Commit fe21e4d

Browse files
committed
Issue #16324: _charset parameter of MIMEText now also accepts email.charset.Charset instances.
Initial patch by Claude Paroz.
1 parent 081bbf6 commit fe21e4d

5 files changed

Lines changed: 16 additions & 1 deletion

File tree

Doc/library/email.mime.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ Here are the classes:
195195
set of the text and is passed as an argument to the
196196
:class:`~email.mime.nonmultipart.MIMENonMultipart` constructor; it defaults
197197
to ``us-ascii`` if the string contains only ``ascii`` codepoints, and
198-
``utf-8`` otherwise.
198+
``utf-8`` otherwise. The *_charset* parameter accepts either a string or a
199+
:class:`~email.charset.Charset` instance.
199200

200201
Unless the *_charset* argument is explicitly set to ``None``, the
201202
MIMEText object created will have both a :mailheader:`Content-Type` header
@@ -206,3 +207,6 @@ Here are the classes:
206207
``Content-Transfer-Encoding`` header, after which a ``set_payload`` call
207208
will automatically encode the new payload (and add a new
208209
:mailheader:`Content-Transfer-Encoding` header).
210+
211+
.. versionchanged:: 3.5
212+
*_charset* also accepts :class:`~email.charset.Charset` instances.

Lib/email/mime/text.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
__all__ = ['MIMEText']
88

9+
from email.charset import Charset
910
from email.mime.nonmultipart import MIMENonMultipart
1011

1112

@@ -34,6 +35,8 @@ def __init__(self, _text, _subtype='plain', _charset=None):
3435
_charset = 'us-ascii'
3536
except UnicodeEncodeError:
3637
_charset = 'utf-8'
38+
if isinstance(_charset, Charset):
39+
_charset = str(_charset)
3740

3841
MIMENonMultipart.__init__(self, 'text', _subtype,
3942
**{'charset': _charset})

Lib/test/test_email/test_email.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,6 +1636,10 @@ def test_charset(self):
16361636
msg = MIMEText('hello there', _charset='us-ascii')
16371637
eq(msg.get_charset().input_charset, 'us-ascii')
16381638
eq(msg['content-type'], 'text/plain; charset="us-ascii"')
1639+
# Also accept a Charset instance
1640+
msg = MIMEText('hello there', _charset=Charset('utf-8'))
1641+
eq(msg.get_charset().input_charset, 'utf-8')
1642+
eq(msg['content-type'], 'text/plain; charset="utf-8"')
16391643

16401644
def test_7bit_input(self):
16411645
eq = self.assertEqual

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,7 @@ Peter Parente
10241024
Alexandre Parenteau
10251025
Dan Parisien
10261026
William Park
1027+
Claude Paroz
10271028
Heikki Partanen
10281029
Harri Pasanen
10291030
Gaël Pasgrimaud

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: TBA
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #16324: _charset parameter of MIMEText now also accepts
14+
email.charset.Charset instances. Initial patch by Claude Paroz.
15+
1316
- Issue #1764286: Fix inspect.getsource() to support decorated functions.
1417
Patch by Claudiu Popa.
1518

0 commit comments

Comments
 (0)