classification
Title: email.header.decode_header return type is not consistent
Type: behavior Stage: resolved
Components: email Versions: Python 3.6, Python 3.4, Python 3.5
process
Status: closed Resolution: duplicate
Dependencies: Superseder: email.header.decode_header sometimes returns bytes, sometimes str
View: 21492
Assigned To: Nosy List: Sebastian Kreft, barry, r.david.murray
Priority: normal Keywords:

Created on 2015-08-05 15:18 by Sebastian Kreft, last changed 2015-08-17 16:10 by r.david.murray. This issue is now closed.

Messages (4)
msg248047 - (view) Author: Sebastian Kreft (Sebastian Kreft) Date: 2015-08-05 15:18
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)]
msg248049 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-08-05 15:49
Yeah, don't use that, use the new APIs.
msg248734 - (view) Author: Sebastian Kreft (Sebastian Kreft) Date: 2015-08-17 16:05
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.
msg248735 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-08-17 16:10
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.
History
Date User Action Args
2019-06-03 09:30:24SilentGhostlinkissue37139 superseder
2015-08-17 16:10:37r.david.murraysetmessages: + msg248735
2015-08-17 16:05:45Sebastian Kreftsetmessages: + msg248734
2015-08-05 15:49:44r.david.murraysetstatus: open -> closed
superseder: email.header.decode_header sometimes returns bytes, sometimes str
messages: + msg248049

resolution: duplicate
stage: resolved
2015-08-05 15:31:06serhiy.storchakasetnosy: + barry, r.david.murray
type: behavior
components: + email
2015-08-05 15:18:45Sebastian Kreftcreate