Permalink
Browse files

Merge pull request #307 from mlorant/fix-243-encoding-exception

Ignore invalid encoding in Address string representation
  • Loading branch information...
NicolasLM committed Nov 3, 2017
2 parents f05d2e5 + bad16f7 commit 278edd0e05623cc11bbb01af3275f120e7049212
Showing with 20 additions and 1 deletion.
  1. +11 −1 imapclient/util.py
  2. +9 −0 tests/test_response_parser.py
View
@@ -4,12 +4,22 @@
from __future__ import unicode_literals
import logging
from six import binary_type, text_type
logger = logging.getLogger(__name__)
def to_unicode(s):
if isinstance(s, binary_type):
return s.decode('ascii')
try:
return s.decode('ascii')
except UnicodeDecodeError:
logger.warning(
"An error occurred while decoding %s in ASCII 'strict' mode. Fallback to "
"'ignore' errors handling, some characters might have been stripped", s
)
return s.decode('ascii', 'ignore')
return s
@@ -9,6 +9,7 @@
from __future__ import unicode_literals
from datetime import datetime
from mock import patch
from imapclient.datetime_util import datetime_to_native
from imapclient.fixed_offset import FixedOffset
@@ -497,6 +498,14 @@ def test_Address_str(self):
self.assertEqual(str(Address(None, None, None, "undisclosed-recipients")),
"undisclosed-recipients")
@patch('imapclient.util.logger')
def test_Address_str_ignores_encoding_error(self, mock_logger):
self.assertEqual(
str(Address(b'Russian \xc2\xeb\xe0\xe4\xe8\xec\xe8\xf0', None, b"g\xe9rard", "domain.org")),
"Russian <grard@domain.org>"
)
# Ensure warning has been triggered twice, for name and mailbox bytes
self.assertEqual(mock_logger.warning.call_count, 2)
def add_crlf(text):

0 comments on commit 278edd0

Please sign in to comment.