]> granicus.if.org Git - python/commitdiff
Issue #16324: _charset parameter of MIMEText now also accepts email.charset.Charset...
authorBerker Peksag <berker.peksag@gmail.com>
Fri, 26 Sep 2014 21:57:29 +0000 (00:57 +0300)
committerBerker Peksag <berker.peksag@gmail.com>
Fri, 26 Sep 2014 21:57:29 +0000 (00:57 +0300)
Initial patch by Claude Paroz.

Doc/library/email.mime.rst
Lib/email/mime/text.py
Lib/test/test_email/test_email.py
Misc/ACKS
Misc/NEWS

index 4cdb322f4604f6f6c347dd646f1470e8a9cee7cb..950b1c630de6546176c4a82bae208fb0cfd7dd9d 100644 (file)
@@ -195,7 +195,8 @@ Here are the classes:
    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 @@ Here are the classes:
    ``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.
index ec18b8594dd976a31cbdcec68c031605ae3c025a..479928ec945d77f092879deb4d70392fa4091f93 100644 (file)
@@ -6,6 +6,7 @@
 
 __all__ = ['MIMEText']
 
+from email.charset import Charset
 from email.mime.nonmultipart import MIMENonMultipart
 
 
@@ -34,6 +35,8 @@ class MIMEText(MIMENonMultipart):
                 _charset = 'us-ascii'
             except UnicodeEncodeError:
                 _charset = 'utf-8'
+        if isinstance(_charset, Charset):
+            _charset = str(_charset)
 
         MIMENonMultipart.__init__(self, 'text', _subtype,
                                   **{'charset': _charset})
index c3ecd0ab2217d9df50b929d91e8a2c05e815aa84..d16d4612ecc12b77777f8e745223bc0d96949a24 100644 (file)
@@ -1636,6 +1636,10 @@ class TestMIMEText(unittest.TestCase):
         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
index 48e1de95747bf6f62358b4f1a477489d3aa18b50..e00f88311f4a92ae563a515c1e26836f3ef06016 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1024,6 +1024,7 @@ Peter Parente
 Alexandre Parenteau
 Dan Parisien
 William Park
+Claude Paroz
 Heikki Partanen
 Harri Pasanen
 GaĆ«l Pasgrimaud
index 47fbafa70c0939c830668a370507db52c17b74b6..e96b6599e494a5b33c0a4b0aea85f33864362d29 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ Release date: TBA
 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.