summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBerker Peksag <berker.peksag@gmail.com>2014-09-27 00:57:29 +0300
committerBerker Peksag <berker.peksag@gmail.com>2014-09-27 00:57:29 +0300
commitfe21e4d4d76d3fd1efd16d124808d72a69588d7b (patch)
tree74e47a453633ae97d05f98cf18aeb361edb951e8
parent081bbf6b28868774c1abca0a8469a517abb9f6cc (diff)
downloadcpython-git-fe21e4d4d76d3fd1efd16d124808d72a69588d7b.tar.gz
Issue #16324: _charset parameter of MIMEText now also accepts email.charset.Charset instances.
Initial patch by Claude Paroz.
-rw-r--r--Doc/library/email.mime.rst6
-rw-r--r--Lib/email/mime/text.py3
-rw-r--r--Lib/test/test_email/test_email.py4
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS3
5 files changed, 16 insertions, 1 deletions
diff --git a/Doc/library/email.mime.rst b/Doc/library/email.mime.rst
index 4cdb322f46..950b1c630d 100644
--- a/Doc/library/email.mime.rst
+++ b/Doc/library/email.mime.rst
@@ -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.
diff --git a/Lib/email/mime/text.py b/Lib/email/mime/text.py
index ec18b8594d..479928ec94 100644
--- a/Lib/email/mime/text.py
+++ b/Lib/email/mime/text.py
@@ -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})
diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py
index c3ecd0ab22..d16d4612ec 100644
--- a/Lib/test/test_email/test_email.py
+++ b/Lib/test/test_email/test_email.py
@@ -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
diff --git a/Misc/ACKS b/Misc/ACKS
index 48e1de9574..e00f88311f 100644
--- 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
diff --git a/Misc/NEWS b/Misc/NEWS
index 47fbafa70c..e96b6599e4 100644
--- 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.