[PATCH 4 of 5] mailutil: utf patches with appropriate charset

Christian Ebert blacktrash at gmx.net
Tue Feb 19 13:16:55 CST 2008


# HG changeset patch
# User Christian Ebert <blacktrash at gmx.net>
# Date 1203448082 -3600
# Node ID f33748dec3fa32e327982711be64a439d7b695e8
# Parent  6f75570a97dbc60c98da98214978ec5553f16399
mailutil: utf patches with appropriate charset

diff --git a/hgext/mailutil.py b/hgext/mailutil.py
--- a/hgext/mailutil.py
+++ b/hgext/mailutil.py
@@ -4,9 +4,9 @@
 
 def ischarset(s, cs):
     try:
-        s.decode(cs)
+        s.decode(cs).encode(cs)
         return True
-    except UnicodeDecodeError:
+    except (UnicodeDecodeError, UnicodeEncodeError):
         return False
 
 class converter(email.Charset.Charset):
@@ -80,3 +80,13 @@
         charset and transfer-encoding accordingly.'''
         s = self.encode(s)
         return email.MIMEText.MIMEText(s, 'plain', self.output_codec)
+
+    def mimetextpatch(self, s, subtype='plain'):
+        '''If patch in ascii or utf-8 we can set correct charset.
+        Otherwise no way to determine the charset.'''
+        for cs in ('us-ascii', 'utf-8'):
+            if ischarset(s, cs):
+                return email.MIMEText.MIMEText(s, subtype, cs)
+        # for now fall back on old behaviour
+        # send as binary attachment instead?
+        return email.MIMEText.MIMEText(s, subtype)


More information about the Mercurial-devel mailing list