Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 42243c4

Browse files
committed
#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.
1 parent 8680bcc commit 42243c4

2 files changed

Lines changed: 10 additions & 9 deletions

File tree

Lib/email/mime/text.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
class MIMEText(MIMENonMultipart):
1515
"""Class for generating text/* type MIME documents."""
1616

17-
def __init__(self, _text, _subtype='plain', _charset='us-ascii'):
17+
def __init__(self, _text, _subtype='plain', _charset=None):
1818
"""Create a text/* type MIME document.
1919
2020
_text is the string for this message object.
@@ -25,16 +25,18 @@ def __init__(self, _text, _subtype='plain', _charset='us-ascii'):
2525
header. This defaults to "us-ascii". Note that as a side-effect, the
2626
Content-Transfer-Encoding header will also be set.
2727
"""
28-
MIMENonMultipart.__init__(self, 'text', _subtype,
29-
**{'charset': _charset})
3028

31-
# If _charset was defualted, check to see see if there are non-ascii
32-
# characters present. Default to utf-8 if there are.
29+
# If no _charset was specified, check to see see if there are non-ascii
30+
# characters present. If not, use 'us-ascii', otherwise use utf-8.
3331
# XXX: This can be removed once #7304 is fixed.
34-
if _charset =='us-ascii':
32+
if _charset is None:
3533
try:
36-
_text.encode(_charset)
34+
_text.encode('us-ascii')
35+
_charset = 'us-ascii'
3736
except UnicodeEncodeError:
3837
_charset = 'utf-8'
3938

39+
MIMENonMultipart.__init__(self, 'text', _subtype,
40+
**{'charset': _charset})
41+
4042
self.set_payload(_text, _charset)

Lib/test/test_email/test_email.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -621,15 +621,14 @@ def test_unicode_body_defaults_to_utf8_encoding(self):
621621
# Issue 14291
622622
m = MIMEText('É testabc\n')
623623
self.assertEqual(str(m),textwrap.dedent("""\
624-
MIME-Version: 1.0
625624
Content-Type: text/plain; charset="utf-8"
625+
MIME-Version: 1.0
626626
Content-Transfer-Encoding: base64
627627
628628
w4kgdGVzdGFiYwo=
629629
"""))
630630

631631

632-
633632
# Test the email.encoders module
634633
class TestEncoders(unittest.TestCase):
635634

0 commit comments

Comments
 (0)