From d45218b2e3894211e11313820bea9f59677c4bf2 Mon Sep 17 00:00:00 2001 From: Jeremy Kerr Date: Thu, 23 Oct 2008 14:22:33 +1100 Subject: [parser] Handle patches with no content charset defined If we don't have an incoming charset defined, assume utf-8. Signed-off-by: Jeremy Kerr --- apps/patchwork/bin/parsemail.py | 10 ++++++++-- apps/patchwork/tests/patchparser.py | 11 +++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) (limited to 'apps/patchwork') diff --git a/apps/patchwork/bin/parsemail.py b/apps/patchwork/bin/parsemail.py index 9aaaa9d..772728e 100755 --- a/apps/patchwork/bin/parsemail.py +++ b/apps/patchwork/bin/parsemail.py @@ -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 diff --git a/apps/patchwork/tests/patchparser.py b/apps/patchwork/tests/patchparser.py index e508dc0..3518432 100644 --- a/apps/patchwork/tests/patchparser.py +++ b/apps/patchwork/tests/patchparser.py @@ -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' -- cgit v1.2.3