Avoid:
TypeError: sequence item 0: expected str instance, bytes found
When an RFC-2047-encoded string contains both an unencoded and an
encoded section. For example:
>>> import email.header
>>> email.header.decode_header('Keld =?ISO-8859-1?Q?J=F8rn_Simonsen?=')
[(b'Keld ', None), (b'J\xf8rn Simonsen', 'iso-8859-1')]
returns the decoded string in bytes but no charset information for the
first chunk. I'm not sure what the default charset for header values
is, but RFC 2047 sets itself up to deal with non-ASCII header values
[1], so I'm guessing it's ASCII ;).
[1]: http://tools.ietf.org/html/rfc2047#section-1
'hello'
>>> _decode_header(string='=?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?=')
'Keld Jørn Simonsen'
+ >>> _decode_header(string='Keld =?ISO-8859-1?Q?J=F8rn_Simonsen?=')
+ 'Keld Jørn Simonsen'
"""
chunks = []
for decoded, charset in _email_header.decode_header(string):
+ if isinstance(decoded, bytes) and not charset:
+ charset = 'ASCII'
if charset:
decoded = str(decoded, charset)
chunks.append(decoded)