[Python-checkins] cpython: #14380: Have MIMEText defaults to utf-8 when passed non-ASCII unicode

r.david.murray python-checkins at python.org
Fri Mar 23 03:18:02 CET 2012


http://hg.python.org/cpython/rev/bfd1ba2bbaf8
changeset:   75892:bfd1ba2bbaf8
user:        R David Murray <rdmurray at bitdance.com>
date:        Thu Mar 22 22:17:51 2012 -0400
summary:
  #14380: Have MIMEText defaults to utf-8 when passed non-ASCII unicode

Previously it would just accept the unicode, which would wind up as unicode in
the transfer-encoded message object, which is just wrong.

Patch by Jeff Knupp.

files:
  Doc/library/email.mime.rst        |   6 +++---
  Lib/email/mime/text.py            |  10 ++++++++++
  Lib/test/test_email/test_email.py |  15 ++++++++++++++-
  Misc/ACKS                         |   1 +
  Misc/NEWS                         |   3 +++
  5 files changed, 31 insertions(+), 4 deletions(-)


diff --git a/Doc/library/email.mime.rst b/Doc/library/email.mime.rst
--- a/Doc/library/email.mime.rst
+++ b/Doc/library/email.mime.rst
@@ -175,7 +175,7 @@
 
 .. currentmodule:: email.mime.text
 
-.. class:: MIMEText(_text, _subtype='plain', _charset='us-ascii')
+.. class:: MIMEText(_text, _subtype='plain', _charset=None)
 
    Module: :mod:`email.mime.text`
 
@@ -185,5 +185,5 @@
    minor type and defaults to :mimetype:`plain`.  *_charset* is the character
    set of the text and is passed as a parameter to the
    :class:`~email.mime.nonmultipart.MIMENonMultipart` constructor; it defaults
-   to ``us-ascii``.  No guessing or encoding is performed on the text data.
-
+   to ``us-ascii`` if the string contains only ``ascii`` codepoints, and
+   ``utf-8`` otherwise.
diff --git a/Lib/email/mime/text.py b/Lib/email/mime/text.py
--- a/Lib/email/mime/text.py
+++ b/Lib/email/mime/text.py
@@ -27,4 +27,14 @@
         """
         MIMENonMultipart.__init__(self, 'text', _subtype,
                                   **{'charset': _charset})
+
+        # If _charset was defualted, check to see see if there are non-ascii
+        # characters present. Default to utf-8 if there are.
+        # XXX: This can be removed once #7304 is fixed.
+        if _charset =='us-ascii':
+            try:
+                _text.encode(_charset)
+            except UnicodeEncodeError:
+                _charset = 'utf-8'
+
         self.set_payload(_text, _charset)
diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py
--- a/Lib/test/test_email/test_email.py
+++ b/Lib/test/test_email/test_email.py
@@ -617,6 +617,19 @@
             abc
             """))
 
+    def test_unicode_body_defaults_to_utf8_encoding(self):
+        # Issue 14291
+        m = MIMEText('É testabc\n')
+        self.assertEqual(str(m),textwrap.dedent("""\
+            MIME-Version: 1.0
+            Content-Type: text/plain; charset="utf-8"
+            Content-Transfer-Encoding: base64
+
+            w4kgdGVzdGFiYwo=
+            """))
+
+
+
 # Test the email.encoders module
 class TestEncoders(unittest.TestCase):
 
@@ -642,7 +655,7 @@
         eq(msg['content-transfer-encoding'], '7bit')
         # Similar, but with 8bit data
         msg = MIMEText('hello \xf8 world')
-        eq(msg['content-transfer-encoding'], '8bit')
+        eq(msg['content-transfer-encoding'], 'base64')
         # And now with a different charset
         msg = MIMEText('hello \xf8 world', _charset='iso-8859-1')
         eq(msg['content-transfer-encoding'], 'quoted-printable')
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -548,6 +548,7 @@
 Kim Knapp
 Lenny Kneler
 Pat Knight
+Jeff Knupp
 Greg Kochanski
 Damon Kohler
 Marko Kohtala
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -34,6 +34,9 @@
 Library
 -------
 
+- Issue #14380: MIMEText now defaults to utf-8 when passed non-ASCII unicode
+  with no charset specified.
+
 - Issue #10340: asyncore - properly handle EINVAL in dispatcher constructor on
   OSX; avoid to call handle_connect in case of a disconnected socket which
   was not meant to connect.

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list