Skip to content

Commit

Permalink
python#14380: Make actual default match docs, fix __init__ order.
Browse files Browse the repository at this point in the history
É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.
  • Loading branch information
bitdancer committed Mar 23, 2012
1 parent 8680bcc commit 42243c4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
16 changes: 9 additions & 7 deletions Lib/email/mime/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -25,16 +25,18 @@ def __init__(self, _text, _subtype='plain', _charset='us-ascii'):
header. This defaults to "us-ascii". Note that as a side-effect, the
Content-Transfer-Encoding header will also be set.
"""
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.
# 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 =='us-ascii':
if _charset is None:
try:
_text.encode(_charset)
_text.encode('us-ascii')
_charset = 'us-ascii'
except UnicodeEncodeError:
_charset = 'utf-8'

MIMENonMultipart.__init__(self, 'text', _subtype,
**{'charset': _charset})

self.set_payload(_text, _charset)
3 changes: 1 addition & 2 deletions Lib/test/test_email/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,15 +621,14 @@ 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"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
w4kgdGVzdGFiYwo=
"""))



# Test the email.encoders module
class TestEncoders(unittest.TestCase):

Expand Down

0 comments on commit 42243c4

Please sign in to comment.