Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-22833: Fix bytes/str inconsistency in email.header.decode_header() #30548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/email/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
def decode_header(header):
"""Decode a message header value without converting charset.

Returns a list of (string, charset) pairs containing each of the decoded
Returns a list of (bytes, charset) pairs containing each of the decoded
parts of the header. Charset is None for non-encoded parts of the header,
otherwise a lower-case string containing the name of the character set
specified in the encoded string.
Expand All @@ -78,7 +78,7 @@ def decode_header(header):
for string, charset in header._chunks]
# If no encoding, just return the header with no charset.
if not ecre.search(header):
return [(header, None)]
return [bytes(header, 'raw-unicode-escape'), None)]
# First step is to parse all the encoded parts into triplets of the form
# (encoded_string, encoding, charset). For unencoded strings, the last
# two parts will be None.
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_email/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -2432,6 +2432,18 @@ def test_multiline_header(self):
self.assertEqual(str(make_header(decode_header(s))),
'"Müller T" <[email protected]>')

def test_unencoded_ascii(self):
# issue 22833
s = 'header without encoded words'
self.assertEqual(decode_header(s),
[(b'header without encoded words', None)])

def test_unencoded_utf8(self):
# issue 22833
s = 'header with unexpected non ASCII caract\xe8res'
self.assertEqual(decode_header(s),
[(b'header with unexpected non ASCII caract\xe8res', None)])


# Test the MIMEMessage class
class TestMIMEMessage(TestEmailBase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The :func:`email.header.decode_header` function now always provides :class:`bytes`,
never :class:`str`, as the first member of the tuples it returns. Previously, it would
return (str, None) when decoding a header consisting only of a single, unencoded part.