From a2cf80ef99fa3e4b63b9d7b5ce02aee0564254e0 Mon Sep 17 00:00:00 2001 From: Adam Coddington Date: Tue, 22 Apr 2014 17:06:17 -0700 Subject: [PATCH] Fixing header decoding such that it works properly under both Python 3.x and 2.x. --- django_mailbox/models.py | 2 +- django_mailbox/tests/test_process_email.py | 7 ++++++- django_mailbox/utils.py | 13 ++++++++++--- setup.py | 2 +- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/django_mailbox/models.py b/django_mailbox/models.py index 0d9c709..f6e938b 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -257,7 +257,7 @@ class Mailbox(models.Model): msg = Message() msg.mailbox = self if 'subject' in message: - msg.subject = convert_header_to_unicode(message['subject'][0:255]) + msg.subject = convert_header_to_unicode(message['subject'])[0:255] if 'message-id' in message: msg.message_id = message['message-id'][0:255] if 'from' in message: diff --git a/django_mailbox/tests/test_process_email.py b/django_mailbox/tests/test_process_email.py index d3f15bc..1f2213c 100644 --- a/django_mailbox/tests/test_process_email.py +++ b/django_mailbox/tests/test_process_email.py @@ -191,7 +191,12 @@ class TestProcessEmail(EmailMessageTestCase): actual_subject = msg.subject self.assertEqual(actual_subject, expected_subject) - expected_from = six.u('test test') + if six.PY3: + # There were various bugfixes in Py3k's email module, + # this is apparently one of them. + expected_from = six.u('test test ') + else: + expected_from = six.u('test test') actual_from = msg.from_header self.assertEqual(expected_from, actual_from) diff --git a/django_mailbox/utils.py b/django_mailbox/utils.py index fb25c55..7d884eb 100644 --- a/django_mailbox/utils.py +++ b/django_mailbox/utils.py @@ -1,6 +1,8 @@ import email.header import logging +import six + from django.conf import settings @@ -15,13 +17,18 @@ DEFAULT_CHARSET = getattr( def convert_header_to_unicode(header): + def _decode(value, encoding): + if isinstance(value, six.text_type): + return value + if not encoding or encoding == 'unknown-8bit': + encoding = DEFAULT_CHARSET + return value.decode(encoding, 'REPLACE') + try: return ''.join( [ ( - bytestr.decode(encoding, 'REPLACE') - if encoding - else bytestr.decode(DEFAULT_CHARSET, 'REPLACE') + _decode(bytestr, encoding) ) for bytestr, encoding in email.header.decode_header(header) ] ) diff --git a/setup.py b/setup.py index decf8cb..81b7b61 100755 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ tests_require = [ setup( name='django-mailbox', - version='3.1', + version='3.1.1', url='http://github.com/latestrevision/django-mailbox/', description=( 'Import mail from POP3, IMAP, local mailboxes or directly from '