[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 00:36:50 EDT 2019


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

Maybe a solution, if no charset defined, then encode it as utf-8 in decode_header, because it's Python3's default encoding?


diff --git a/Lib/email/header.py b/Lib/email/header.py
index 4ab0032bc6..8dbfe58a57 100644
--- a/Lib/email/header.py
+++ b/Lib/email/header.py
@@ -135,7 +135,10 @@ def decode_header(header):
     collapsed = []
     last_word = last_charset = None
     for word, charset in decoded_words:
-        if isinstance(word, str):
+        if not charset and isinstance(word, str):
+            word = word.encode('utf-8')
+            charset = 'utf-8'
+        elif isinstance(word, str):
             word = bytes(word, 'raw-unicode-escape')
         if last_word is None:
             last_word = word



Python 3.9.0a0 (heads/master:110a47c4f4, Jul 10 2019, 11:32:53) 
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import email.header
>>> header = "Your booking at Voyager Int'l Hostel,=?UTF-8?B?IFBhbmFtw6EgQ2l0eQ==?=,   Panamá- Casco Antiguo"
>>> print(email.header.make_header(email.header.decode_header(header)))
Your booking at Voyager Int'l Hostel, Panamá City,   Panamá- Casco Antiguo
>>>

----------
nosy: +aldwinaldwin

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


More information about the Python-bugs-list mailing list