[issue37532] email.header.make_header() doesn't work if any `ascii` code is out of range(128)

Aldwin Pollefeyt report at bugs.python.org
Wed Jul 10 04:00:16 EDT 2019


Aldwin Pollefeyt <aldwinaldwin at gmail.com> added the comment:

Changing everything to utf-8 breaks a lot of tests, so here a less invasive solution?

diff --git a/Lib/email/header.py b/Lib/email/header.py
index 4ab0032bc6..1e71eeae7f 100644
--- a/Lib/email/header.py
+++ b/Lib/email/header.py
@@ -136,7 +136,14 @@ def decode_header(header):
     last_word = last_charset = None
     for word, charset in decoded_words:
         if isinstance(word, str):
-            word = bytes(word, 'raw-unicode-escape')
+            word_tmp = bytes(word, 'raw-unicode-escape')
+            input_charset = charset or 'us-ascii'
+            try:
+                _ = word_tmp.decode(input_charset, errors='strict')
+                word = word_tmp
+            except UnicodeDecodeError:
+                word = str(word).encode('utf-8')
+                charset = 'utf-8'
         if last_word is None:
             last_word = word
             last_charset = charset

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37532>
_______________________________________


More information about the Python-bugs-list mailing list