From: Dmitry Shachnev Subject: email: Add special case for latin texts In MIMEText class, use iso-8859-1 encoding by default if all the characters are in range(256). This makes the messages use quoted-printable transfer-encoding for most western-european languages. Update the tests accordingly. Bug: http://bugs.python.org/issue15016 diff -ruN a/Lib/email/mime/text.py b/Lib/email/mime/text.py --- a/Lib/email/mime/text.py 2012-06-06 12:30:13.866861588 +0400 +++ b/Lib/email/mime/text.py 2012-06-06 13:28:43.427552123 +0400 @@ -26,15 +26,24 @@ 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. + # If no _charset was specified, try to guess which encoding to use: + # - All the characters are in range(128) -> using us-ascii + # - All the characters are in range(256) -> using iso-8859-1 + # - Else using 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' + try: + _text.encode('iso-8859-1') + except UnicodeEncodeError: + _charset = 'utf-8' + else: + _charset = 'iso-8859-1' + else: + _charset = 'us-ascii' MIMENonMultipart.__init__(self, 'text', _subtype, **{'charset': _charset}) diff -ruN a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py --- a/Lib/test/test_email/test_email.py 2012-06-06 12:29:30.537759836 +0400 +++ b/Lib/test/test_email/test_email.py 2012-06-06 13:26:47.728596390 +0400 @@ -622,15 +622,26 @@ abc """)) - def test_unicode_body_defaults_to_utf8_encoding(self): - # Issue 14291 + def test_latin_body_defaults_to_latin_encoding(self): + # Issue 15016 m = MIMEText('É testabc\n') self.assertEqual(str(m),textwrap.dedent("""\ + Content-Type: text/plain; charset="iso-8859-1" + MIME-Version: 1.0 + Content-Transfer-Encoding: quoted-printable + + =C9 testabc + """)) + + def test_non_latin_body_defaults_to_utf8_encoding(self): + # Issue 14291 + m = MIMEText(chr(256)+' testabc\n') + self.assertEqual(str(m),textwrap.dedent("""\ Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 - w4kgdGVzdGFiYwo= + xIAgdGVzdGFiYwo= """))