changeset: 92594:d43d4d4ebf2c user: Berker Peksag date: Sat Sep 27 00:57:29 2014 +0300 files: Doc/library/email.mime.rst Lib/email/mime/text.py Lib/test/test_email/test_email.py Misc/ACKS Misc/NEWS description: Issue #16324: _charset parameter of MIMEText now also accepts email.charset.Charset instances. Initial patch by Claude Paroz. diff -r ad9cc6124a19 -r d43d4d4ebf2c Doc/library/email.mime.rst --- a/Doc/library/email.mime.rst Fri Sep 26 17:34:54 2014 -0400 +++ b/Doc/library/email.mime.rst Sat Sep 27 00:57:29 2014 +0300 @@ -195,7 +195,8 @@ set of the text and is passed as an argument to the :class:`~email.mime.nonmultipart.MIMENonMultipart` constructor; it defaults to ``us-ascii`` if the string contains only ``ascii`` codepoints, and - ``utf-8`` otherwise. + ``utf-8`` otherwise. The *_charset* parameter accepts either a string or a + :class:`~email.charset.Charset` instance. Unless the *_charset* argument is explicitly set to ``None``, the MIMEText object created will have both a :mailheader:`Content-Type` header @@ -206,3 +207,6 @@ ``Content-Transfer-Encoding`` header, after which a ``set_payload`` call will automatically encode the new payload (and add a new :mailheader:`Content-Transfer-Encoding` header). + + .. versionchanged:: 3.5 + *_charset* also accepts :class:`~email.charset.Charset` instances. diff -r ad9cc6124a19 -r d43d4d4ebf2c Lib/email/mime/text.py --- a/Lib/email/mime/text.py Fri Sep 26 17:34:54 2014 -0400 +++ b/Lib/email/mime/text.py Sat Sep 27 00:57:29 2014 +0300 @@ -6,6 +6,7 @@ __all__ = ['MIMEText'] +from email.charset import Charset from email.mime.nonmultipart import MIMENonMultipart @@ -34,6 +35,8 @@ _charset = 'us-ascii' except UnicodeEncodeError: _charset = 'utf-8' + if isinstance(_charset, Charset): + _charset = str(_charset) MIMENonMultipart.__init__(self, 'text', _subtype, **{'charset': _charset}) diff -r ad9cc6124a19 -r d43d4d4ebf2c Lib/test/test_email/test_email.py --- a/Lib/test/test_email/test_email.py Fri Sep 26 17:34:54 2014 -0400 +++ b/Lib/test/test_email/test_email.py Sat Sep 27 00:57:29 2014 +0300 @@ -1636,6 +1636,10 @@ msg = MIMEText('hello there', _charset='us-ascii') eq(msg.get_charset().input_charset, 'us-ascii') eq(msg['content-type'], 'text/plain; charset="us-ascii"') + # Also accept a Charset instance + msg = MIMEText('hello there', _charset=Charset('utf-8')) + eq(msg.get_charset().input_charset, 'utf-8') + eq(msg['content-type'], 'text/plain; charset="utf-8"') def test_7bit_input(self): eq = self.assertEqual diff -r ad9cc6124a19 -r d43d4d4ebf2c Misc/ACKS --- a/Misc/ACKS Fri Sep 26 17:34:54 2014 -0400 +++ b/Misc/ACKS Sat Sep 27 00:57:29 2014 +0300 @@ -1024,6 +1024,7 @@ Alexandre Parenteau Dan Parisien William Park +Claude Paroz Heikki Partanen Harri Pasanen Gaƫl Pasgrimaud diff -r ad9cc6124a19 -r d43d4d4ebf2c Misc/NEWS --- a/Misc/NEWS Fri Sep 26 17:34:54 2014 -0400 +++ b/Misc/NEWS Sat Sep 27 00:57:29 2014 +0300 @@ -10,6 +10,9 @@ Core and Builtins ----------------- +- Issue #16324: _charset parameter of MIMEText now also accepts + email.charset.Charset instances. Initial patch by Claude Paroz. + - Issue #1764286: Fix inspect.getsource() to support decorated functions. Patch by Claudiu Popa.