[Python-checkins] cpython: #19957: Simplify encode_7or8bit now that _payload is always str.

r.david.murray python-checkins at python.org
Fri Dec 13 03:41:19 CET 2013


http://hg.python.org/cpython/rev/29e5dd1f608a
changeset:   87925:29e5dd1f608a
user:        R David Murray <rdmurray at bitdance.com>
date:        Thu Dec 12 21:40:20 2013 -0500
summary:
  #19957: Simplify encode_7or8bit now that _payload is always str.

Patch by Vajrasky Kok, test enhancement by me.

files:
  Lib/email/encoders.py             |  17 ++++-------------
  Lib/test/test_email/test_email.py |   9 ++++++++-
  2 files changed, 12 insertions(+), 14 deletions(-)


diff --git a/Lib/email/encoders.py b/Lib/email/encoders.py
--- a/Lib/email/encoders.py
+++ b/Lib/email/encoders.py
@@ -54,21 +54,12 @@
         # There's no payload.  For backwards compatibility we use 7bit
         msg['Content-Transfer-Encoding'] = '7bit'
         return
-    # We play a trick to make this go fast.  If encoding/decode to ASCII
-    # succeeds, we know the data must be 7bit, otherwise treat it as 8bit.
+    # We play a trick to make this go fast.  If decoding from ASCII succeeds,
+    # we know the data must be 7bit, otherwise treat it as 8bit.
     try:
-        if isinstance(orig, str):
-            orig.encode('ascii')
-        else:
-            orig.decode('ascii')
+        orig.decode('ascii')
     except UnicodeError:
-        charset = msg.get_charset()
-        output_cset = charset and charset.output_charset
-        # iso-2022-* is non-ASCII but encodes to a 7-bit representation
-        if output_cset and output_cset.lower().startswith('iso-2022-'):
-            msg['Content-Transfer-Encoding'] = '7bit'
-        else:
-            msg['Content-Transfer-Encoding'] = '8bit'
+        msg['Content-Transfer-Encoding'] = '8bit'
     else:
         msg['Content-Transfer-Encoding'] = '7bit'
 
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
@@ -765,8 +765,15 @@
         # whose output character set is 7bit gets a transfer-encoding
         # of 7bit.
         eq = self.assertEqual
-        msg = MIMEText('文', _charset='euc-jp')
+        msg = MIMEText('文\n', _charset='euc-jp')
         eq(msg['content-transfer-encoding'], '7bit')
+        eq(msg.as_string(), textwrap.dedent("""\
+            MIME-Version: 1.0
+            Content-Type: text/plain; charset="iso-2022-jp"
+            Content-Transfer-Encoding: 7bit
+
+            \x1b$BJ8\x1b(B
+            """))
 
     def test_qp_encode_latin1(self):
         msg = MIMEText('\xe1\xf6\n', 'text', 'ISO-8859-1')

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


More information about the Python-checkins mailing list