utils: relax conversion to unicode
authorOlivier Matz <zer0@droids-corp.org>
Sun, 29 Nov 2015 11:01:30 +0000 (12:01 +0100)
committerOlivier Matz <zer0@droids-corp.org>
Sun, 29 Nov 2015 11:01:30 +0000 (12:01 +0100)
Some mails have incorrectly encoded headers, resulting in an exception
when decoding them. This can be reproduced manually:

>>> s='Vos deux avantages : 20% de r\xc3\xa9duction  + Q1|\x051\xa5\xd9\xc9\x85\xa5\xcd\xbd\xb8\x80\xd0\xe1\xa0\x81\xbd\x99\x99\x95\xc9\xd1\x94\x80\x84'
>>> s.decode("utf-8")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)

Be more relax and try to fix the encoding errors instead of throwing
an exception.

Signed-off-by: Olivier Matz <zer0@droids-corp.org>
imapami/utils.py

index 291b71f..4af970c 100644 (file)
@@ -76,7 +76,7 @@ def headers_to_unicode(headers):
         if encoding is None:
             value = unicode(value)
         else:
-            value = value.decode(encoding)
+            value = value.decode(encoding, errors="replace")
         unicode_headers[key] = value
     return unicode_headers