Revision: 8179
          http://svn.sourceforge.net/mailman/?rev=8179&view=rev
Author:   tkikuchi
Date:     2007-03-23 22:17:00 -0700 (Fri, 23 Mar 2007)

Log Message:
-----------
Brush up the fall-back-to-utf8 mechanizm in ToDigest.py.  Neither cStringIO
nor StringIO have 'encoding'.  What we need was unicode string StringIO and
python StringIO has it already.   Also, use utf-8 for the table of contents
in MIME digest if the subject has a different charset.

Adding a test code for the multi-language digest.

Modified Paths:
--------------
    trunk/mailman/Mailman/Handlers/ToDigest.py
    trunk/mailman/Mailman/testing/test_handlers.py

Modified: trunk/mailman/Mailman/Handlers/ToDigest.py
===================================================================
--- trunk/mailman/Mailman/Handlers/ToDigest.py  2007-03-23 11:28:12 UTC (rev 
8178)
+++ trunk/mailman/Mailman/Handlers/ToDigest.py  2007-03-24 05:17:00 UTC (rev 
8179)
@@ -31,7 +31,7 @@
 import time
 import logging
 
-from StringIO import StringIO
+from StringIO import StringIO          # cStringIO can't handle unicode.
 from email.Charset import Charset
 from email.Generator import Generator
 from email.Header import decode_header, make_header, Header
@@ -163,8 +163,6 @@
     mimemsg['Message-ID'] = Utils.unique_message_id(mlist)
     # Set things up for the rfc1153 digest
     plainmsg = StringIO()
-    # cStringIO doesn't have encoding. Sigh.
-    plainmsg.encoding = 'utf-8'
     rfc1153msg = Message.Message()
     rfc1153msg['From'] = mlist.GetRequestEmail()
     rfc1153msg['Subject'] = digestsubj
@@ -211,7 +209,6 @@
     #
     # Meanwhile prepare things for the table of contents
     toc = StringIO()
-    toc.encoding = 'utf-8'
     print >> toc, _("Today's Topics:\n")
     # Now cruise through all the messages in the mailbox of digest messages,
     # building the MIME payload and core of the RFC 1153 digest.  We'll also
@@ -292,7 +289,10 @@
         return
     toctext = toc.getvalue()
     # MIME
-    tocpart = MIMEText(toctext.encode(lcset), _charset=lcset)
+    try:
+        tocpart = MIMEText(toctext.encode(lcset), _charset=lcset)
+    except UnicodeError:
+        tocpart = MIMEText(toctext.encode('utf-8'), _charset='utf-8')
     tocpart['Content-Description']= _("Today's Topics ($msgcount messages)")
     mimemsg.attach(tocpart)
     # RFC 1153
@@ -333,16 +333,12 @@
         # -- just stringfy it.
         payload = msg.get_payload(decode=True) \
                   or msg.as_string().split('\n\n',1)[1]
-        mcset = msg.get_content_charset('')
-        if mcset and mcset <> lcset and mcset <> lcset_out:
-            try:
-                payload = unicode(payload, mcset, 'replace'
-                          ).encode(lcset, 'replace')
-            except (UnicodeError, LookupError):
-                # TK: Message has something unknown charset.
-                #     _out means charset in 'outer world'.
-                payload = unicode(payload, lcset_out, 'replace'
-                          ).encode(lcset, 'replace')
+        mcset = msg.get_content_charset('us-ascii')
+        try:
+            payload = unicode(payload, mcset, 'replace')
+        except (LookupError, TypeError):
+            # unknown or empty charset
+            payload = unicode(payload, 'us-ascii', 'replace')
         print >> plainmsg, payload
         if not payload.endswith('\n'):
             print >> plainmsg
@@ -403,7 +399,12 @@
                     listname=mlist.fqdn_listname,
                     isdigest=True)
     # RFC 1153
-    rfc1153msg.set_payload(plainmsg.getvalue().encode(lcset), lcset)
+    # If the entire digest message can't be encoded by list charset, fall
+    # back to 'utf-8'.
+    try:
+        rfc1153msg.set_payload(plainmsg.getvalue().encode(lcset), lcset)
+    except UnicodeError:
+        rfc1153msg.set_payload(plainmsg.getvalue().encode('utf-8'), 'utf-8')
     virginq.enqueue(rfc1153msg,
                     recips=plainrecips,
                     listname=mlist.fqdn_listname,

Modified: trunk/mailman/Mailman/testing/test_handlers.py
===================================================================
--- trunk/mailman/Mailman/testing/test_handlers.py      2007-03-23 11:28:12 UTC 
(rev 8178)
+++ trunk/mailman/Mailman/testing/test_handlers.py      2007-03-24 05:17:00 UTC 
(rev 8179)
@@ -1606,6 +1606,41 @@
         # BAW: this test is incomplete...
 
 
+    def test_send_i18n_digest(self):
+        eq = self.assertEqual
+        mlist = self._mlist
+       mlist.preferred_language = 'fr'
+        msg = email.message_from_string("""\
+From: [EMAIL PROTECTED]
+To: [EMAIL PROTECTED]
+Subject: =?iso-2022-jp?b?GyRCMGxIVhsoQg==?=
+MIME-Version: 1.0
+Content-Type: text/plain; charset=iso-2022-jp
+Content-Transfer-Encoding: 7bit
+
+\x1b$B0lHV\x1b(B
+""")
+        mlist.digest_size_threshhold = 0
+        ToDigest.process(mlist, msg, {})
+       files = self._sb.files()
+        eq(len(files), 2)
+        for filebase in files:
+           qmsg, qdata = self._sb.dequeue(filebase)
+            if qmsg.get_content_maintype() == 'multipart':
+                mimemsg = qmsg
+                mimedata = qdata
+            else:
+                rfc1153msg = qmsg
+                rfc1153data = qdata
+       eq(rfc1153msg.get_content_type(), 'text/plain')
+       eq(rfc1153msg.get_content_charset(), 'utf-8')
+       eq(rfc1153msg['content-transfer-encoding'], 'base64')
+       toc = mimemsg.get_payload()[1]
+       eq(toc.get_content_type(), 'text/plain')
+       eq(toc.get_content_charset(), 'utf-8')
+       eq(toc['content-transfer-encoding'], 'base64')
+
+
 
 class TestToOutgoing(TestBase):
     def setUp(self):


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.
_______________________________________________
Mailman-checkins mailing list
[email protected]
Unsubscribe: 
http://mail.python.org/mailman/options/mailman-checkins/archive%40jab.org

Reply via email to