[Python-checkins] cpython: #14380: Make actual default match docs, fix __init__ order.

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


http://hg.python.org/cpython/rev/9ceac471bd8c
changeset:   75893:9ceac471bd8c
user:        R David Murray <rdmurray at bitdance.com>
date:        Thu Mar 22 22:40:44 2012 -0400
summary:
  #14380: Make actual default match docs, fix __init__ order.

Éric pointed out that given that the default was documented as None, someone
would reasonably pass that to get the default behavior.  In fixing the code to
use None, I noticed that the change to _charset was being done after it had
already been passed to MIMENonMultipart.  The change to the test verifies that
the order is now correct.

files:
  Lib/email/mime/text.py            |  22 ++++++++++--------
  Lib/test/test_email/test_email.py |   3 +-
  2 files changed, 13 insertions(+), 12 deletions(-)


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
@@ -14,7 +14,7 @@
 class MIMEText(MIMENonMultipart):
     """Class for generating text/* type MIME documents."""
 
-    def __init__(self, _text, _subtype='plain', _charset='us-ascii'):
+    def __init__(self, _text, _subtype='plain', _charset=None):
         """Create a text/* type MIME document.
 
         _text is the string for this message object.
@@ -25,16 +25,18 @@
         header.  This defaults to "us-ascii".  Note that as a side-effect, the
         Content-Transfer-Encoding header will also be set.
         """
+
+        # If no _charset was specified, check to see see if there are non-ascii
+        # characters present. If not, use 'us-ascii', otherwise use utf-8.
+        # XXX: This can be removed once #7304 is fixed.
+        if _charset is None:
+            try:
+                _text.encode('us-ascii')
+                _charset = 'us-ascii'
+            except UnicodeEncodeError:
+                _charset = 'utf-8'
+
         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
@@ -621,15 +621,14 @@
         # Issue 14291
         m = MIMEText('É testabc\n')
         self.assertEqual(str(m),textwrap.dedent("""\
+            Content-Type: text/plain; charset="utf-8"
             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):
 

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


More information about the Python-checkins mailing list