diff roundup/mailgw.py @ 5764:611737bc7261 issue2551008

Handle the issue in _decode_header by decoding only when decode_header returns bytes.
author Ezio Melotti <ezio.melotti@gmail.com>
date Wed, 05 Jun 2019 00:13:45 +0200
parents 945051a69d85
children 7264b2e79a31
line wrap: on
line diff
--- a/roundup/mailgw.py	Tue Jun 04 23:41:45 2019 +0200
+++ b/roundup/mailgw.py	Wed Jun 05 00:13:45 2019 +0200
@@ -202,15 +202,19 @@
     def _decode_header(self, hdr):
         parts = []
         for part, encoding in decode_header(hdr):
-            if encoding:
-                part = part.decode(encoding)
-            else:
-                # if the encoding is unknown, try decoding with utf-8
-                # and fallback on iso-8859-1 if that fails
-                try:
-                    part = part.decode('utf-8')
-                except UnicodeDecodeError:
-                    part = part.decode('iso-8859-1')
+            # decode_header might return either bytes or unicode,
+            # see https://bugs.python.org/issue21492
+            # If part is bytes, try to decode it with the specified
+            # encoding if it's provided, otherwise try utf-8 and
+            # fallback on iso-8859-1 if that fails.
+            if isinstance(part, bytes):
+                if encoding:
+                    part = part.decode(encoding)
+                else:
+                    try:
+                        part = part.decode('utf-8')
+                    except UnicodeDecodeError:
+                        part = part.decode('iso-8859-1')
             # RFC 2047 specifies that between encoded parts spaces are
             # swallowed while at the borders from encoded to non-encoded
             # or vice-versa we must preserve a space. Multiple adjacent

Roundup Issue Tracker: http://roundup-tracker.org/