[Python-checkins] cpython (merge 3.3 -> default): Merge: #16948: Fix quopri encoding of non-latin1 character sets.

r.david.murray python-checkins at python.org
Tue Feb 5 17:35:06 CET 2013


http://hg.python.org/cpython/rev/009dc81e8bc9
changeset:   82023:009dc81e8bc9
parent:      82020:68be406e76e1
parent:      82022:e644058e8e7b
user:        R David Murray <rdmurray at bitdance.com>
date:        Tue Feb 05 11:34:39 2013 -0500
summary:
  Merge: #16948: Fix quopri encoding of non-latin1 character sets.

files:
  Lib/email/charset.py              |  13 +++++++++++
  Lib/test/test_email/test_email.py |  21 +++++++++++++++++++
  Misc/NEWS                         |   3 ++
  3 files changed, 37 insertions(+), 0 deletions(-)


diff --git a/Lib/email/charset.py b/Lib/email/charset.py
--- a/Lib/email/charset.py
+++ b/Lib/email/charset.py
@@ -392,6 +392,19 @@
                 string = string.encode(self.output_charset)
             return email.base64mime.body_encode(string)
         elif self.body_encoding is QP:
+            # quopromime.body_encode takes a string, but operates on it as if
+            # it were a list of byte codes.  For a (minimal) history on why
+            # this is so, see changeset 0cf700464177.  To correctly encode a
+            # character set, then, we must turn it into pseudo bytes via the
+            # latin1 charset, which will encode any byte as a single code point
+            # between 0 and 255, which is what body_encode is expecting.
+            #
+            # Note that this clause doesn't handle the case of a _payload that
+            # is already bytes.  It never did, and the semantics of _payload
+            # being bytes has never been nailed down, so fixing that is a
+            # longer term TODO.
+            if isinstance(string, str):
+                string = string.encode(self.output_charset).decode('latin1')
             return email.quoprimime.body_encode(string)
         else:
             if isinstance(string, str):
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
@@ -677,6 +677,27 @@
         msg = MIMEText('文', _charset='euc-jp')
         eq(msg['content-transfer-encoding'], '7bit')
 
+    def test_qp_encode_latin1(self):
+        msg = MIMEText('\xe1\xf6\n', 'text', 'ISO-8859-1')
+        self.assertEqual(str(msg), textwrap.dedent("""\
+            MIME-Version: 1.0
+            Content-Type: text/text; charset="iso-8859-1"
+            Content-Transfer-Encoding: quoted-printable
+
+            =E1=F6
+            """))
+
+    def test_qp_encode_non_latin1(self):
+        # Issue 16948
+        msg = MIMEText('\u017c\n', 'text', 'ISO-8859-2')
+        self.assertEqual(str(msg), textwrap.dedent("""\
+            MIME-Version: 1.0
+            Content-Type: text/text; charset="iso-8859-2"
+            Content-Transfer-Encoding: quoted-printable
+
+            =BF
+            """))
+
 
 # Test long header wrapping
 class TestLongHeaders(TestEmailBase):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -235,6 +235,9 @@
 Library
 -------
 
+- Issue #16948: Fix quoted printable body encoding for non-latin1 character
+  sets in the email package.
+
 - Issue #16811: Fix folding of headers with no value in the provisional email
   policies.
 

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


More information about the Python-checkins mailing list