]> git.ozlabs.org Git - patchwork/commitdiff
[parser] Handle patches with no content charset defined
authorJeremy Kerr <jk@ozlabs.org>
Thu, 23 Oct 2008 03:22:33 +0000 (14:22 +1100)
committerJeremy Kerr <jk@ozlabs.org>
Thu, 23 Oct 2008 03:22:33 +0000 (14:22 +1100)
If we don't have an incoming charset defined, assume utf-8.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
apps/patchwork/bin/parsemail.py
apps/patchwork/tests/patchparser.py

index 9aaaa9d9421a87405d566a8b691d11e324d2886a..772728e41e82ffbba9e2d42a54ca17004652e3bf 100755 (executable)
@@ -138,10 +138,16 @@ def find_content(project, mail):
             continue
 
         payload = part.get_payload(decode=True)
-        if not isinstance(payload, unicode):
-            payload = unicode(payload, part.get_content_charset())
+        charset = part.get_content_charset()
         subtype = part.get_content_subtype()
 
+        # if we don't have a charset, assume utf-8
+        if charset is None:
+            charset = 'utf-8'
+
+        if not isinstance(payload, unicode):
+            payload = unicode(payload, charset)
+
         if subtype in ['x-patch', 'x-diff']:
             patchbuf = payload
 
index e508dc08a9beffcf376d5f3bb28baa84a76d44ad..351843259b69ab8c84012c817f349b282e55f381 100644 (file)
@@ -83,6 +83,17 @@ class UTF8InlinePatchTest(InlinePatchTest):
                              content_encoding = self.patch_encoding)
         (self.patch, self.comment) = find_content(self.project, email)
 
+class NoCharsetInlinePatchTest(InlinePatchTest):
+    """ Test mails with no content-type or content-encoding header """
+    patch_filename = '0001-add-line.patch'
+
+    def setUp(self):
+        self.orig_patch = read_patch(self.patch_filename)
+        email = create_email(self.test_comment + '\n' + self.orig_patch)
+        del email['Content-Type']
+        del email['Content-Transfer-Encoding']
+        (self.patch, self.comment) = find_content(self.project, email)
+
 class SignatureCommentTest(InlinePatchTest):
     patch_filename = '0001-add-line.patch'
     test_comment = 'Test comment\nmore comment'