]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Merged revisions 81660 via svnmerge from
authorR. David Murray <rdmurray@bitdance.com>
Thu, 3 Jun 2010 02:05:47 +0000 (02:05 +0000)
committerR. David Murray <rdmurray@bitdance.com>
Thu, 3 Jun 2010 02:05:47 +0000 (02:05 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

................
  r81660 | r.david.murray | 2010-06-02 21:58:28 -0400 (Wed, 02 Jun 2010) | 25 lines

  Fix Charset.body_encode to encode to output_charset before calling base64mime.

  This means that what gets encoded in base64 is the encoded version of the
  unicode payload.  This bug was revealed by a forward port of the tests from
  Issue 1368247, but the fix was completely different.

  Note that the merge is only of the tests, the doc changes were inappropriate
  since email5 expects unicode, not bytes.  I'm also not convinced that
  quopri works correctly in email5, but that's a different issue.

  Merged revisions 81658 via svnmerge from
  svn+ssh://pythondev@svn.python.org/python/trunk

  ........
    r81658 | r.david.murray | 2010-06-02 18:03:15 -0400 (Wed, 02 Jun 2010) | 9 lines

    #1368247: make set_charset/MIMEText automatically encode unicode _payload.

    Fixes (mysterious, to the end user) UnicodeErrors when using utf-8 as
    the charset and unicode as the _text argument.  Also makes the way in
    which unicode gets encoded to quoted printable for other charsets more
    sane (it only worked by accident previously).  The _payload now is encoded
    to the charset.output_charset if it is unicode.
  ........
................

Lib/email/charset.py
Lib/email/test/test_email.py
Misc/NEWS

index 9e5ee67bfe657658635b776770c45ec92908f130..a44b711aa4b303792cd667b21fbae85496715cc0 100644 (file)
@@ -377,6 +377,8 @@ class Charset:
         """
         # 7bit/8bit encodings return the string unchanged (module conversions)
         if self.body_encoding is BASE64:
+            if isinstance(string, str):
+                string = string.encode(self.output_charset)
             return email.base64mime.body_encode(string)
         elif self.body_encoding is QP:
             return email.quoprimime.body_encode(string)
index 2ebd39f2db94fe7167966b38fbbcaa30d0c687ed..5508456389e59f66e277d010b7b37bd218e89a14 100644 (file)
@@ -531,7 +531,7 @@ class TestEncoders(unittest.TestCase):
         # whose output character set is 7bit gets a transfer-encoding
         # of 7bit.
         eq = self.assertEqual
-        msg = MIMEText('\xca\xb8', _charset='euc-jp')
+        msg = MIMEText('', _charset='euc-jp')
         eq(msg['content-transfer-encoding'], '7bit')
 
 \f
@@ -1076,6 +1076,33 @@ class TestMIMEText(unittest.TestCase):
         eq(msg.get_charset().input_charset, 'us-ascii')
         eq(msg['content-type'], 'text/plain; charset="us-ascii"')
 
+    def test_7bit_input(self):
+        eq = self.assertEqual
+        msg = MIMEText('hello there', _charset='us-ascii')
+        eq(msg.get_charset().input_charset, 'us-ascii')
+        eq(msg['content-type'], 'text/plain; charset="us-ascii"')
+
+    def test_7bit_input_no_charset(self):
+        eq = self.assertEqual
+        msg = MIMEText('hello there')
+        eq(msg.get_charset(), 'us-ascii')
+        eq(msg['content-type'], 'text/plain; charset="us-ascii"')
+        self.assertTrue('hello there' in msg.as_string())
+
+    def test_utf8_input(self):
+        teststr = '\u043a\u0438\u0440\u0438\u043b\u0438\u0446\u0430'
+        eq = self.assertEqual
+        msg = MIMEText(teststr, _charset='utf-8')
+        eq(msg.get_charset().output_charset, 'utf-8')
+        eq(msg['content-type'], 'text/plain; charset="utf-8"')
+        eq(msg.get_payload(decode=True), teststr.encode('utf-8'))
+
+    @unittest.skip("can't fix because of backward compat in email5, "
+        "will fix in email6")
+    def test_utf8_input_no_charset(self):
+        teststr = '\u043a\u0438\u0440\u0438\u043b\u0438\u0446\u0430'
+        self.assertRaises(UnicodeEncodeError, MIMEText, teststr)
+
 
 \f
 # Test complicated multipart/* messages
index 3c4aa768662f5c9b78d23c4d3a6233b19417c246..ea7e5034a03b518845b584728a8fc5bfb5356925 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -54,6 +54,10 @@ C-API
 Library
 -------
 
+- Charset.body_encode now correctly handles base64 encoding by encoding
+  with the output_charset before calling base64mime.encode.  Passes the
+  tests from 2.x issue 1368247.
+
 - Issue #7150: Raise OverflowError if the result of adding or subtracting
   timedelta from date or datetime falls outside of the MINYEAR:MAXYEAR range.