You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
email.header.decode_header return type is not consistent #68985
bpo-21492: email.header.decode_header sometimes returns bytes, sometimes str
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2015-08-05.15:49:44.577>created_at=<Date2015-08-05.15:18:45.131>labels= ['type-bug', 'expert-email']
title='email.header.decode_header return type is not consistent'updated_at=<Date2015-08-17.16:10:37.946>user='https://bugs.python.org/SebastianKreft'
The return type of email.header.decode_header is not consistent. When there are encoded parts the return type is a list of (bytes, charset or None) (Note that the documentation says it is a list of (str, charset)). However, when there are no encoded parts the return type is [(str, None)]. Note that, at the end of the function, there is a routine that converts everything to bytes.
Compare:
In [01]: email.header.decode_header('=?UTF-8?Q?foo?=bar')
Out[01]: [(b'foo', 'utf-8'), (b'bar', None)]
In [02]: email.header.decode_header('foobar')
Out[02]: [('foobar', None)]
Right, it's provisional in 3.4. I'm working on the doc rewrite now (although I suppose there's a chance it won't get accepted for 3.5, I expect it to).
Take a look at https://docs.python.org/3/library/email.policy.html. Basically, if you use EmailMessage instead of Message (and therefore policy.default instead of policy.compat32) header parsing and decoding is handled for you.
Activity
SebastianKreft commentedon Aug 5, 2015
The return type of email.header.decode_header is not consistent. When there are encoded parts the return type is a list of (bytes, charset or None) (Note that the documentation says it is a list of (str, charset)). However, when there are no encoded parts the return type is [(str, None)]. Note that, at the end of the function, there is a routine that converts everything to bytes.
Compare:
In [01]: email.header.decode_header('=?UTF-8?Q?foo?=bar')
Out[01]: [(b'foo', 'utf-8'), (b'bar', None)]
In [02]: email.header.decode_header('foobar')
Out[02]: [('foobar', None)]
bitdancer commentedon Aug 5, 2015
Yeah, don't use that, use the new APIs.
SebastianKreft commentedon Aug 17, 2015
And what would the new API be?
There is nothing pointing to it either in the documentation https://docs.python.org/3.4/library/email.header.html or source code.
bitdancer commentedon Aug 17, 2015
Right, it's provisional in 3.4. I'm working on the doc rewrite now (although I suppose there's a chance it won't get accepted for 3.5, I expect it to).
Take a look at https://docs.python.org/3/library/email.policy.html. Basically, if you use EmailMessage instead of Message (and therefore policy.default instead of policy.compat32) header parsing and decoding is handled for you.