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

Skip to content

Commit 041015c

Browse files
committed
#11584: Since __getitem__ returns headers, make decode_header handle them.
Why I consider this a bug rather than an API change: the API change was to Message, which didn't used to return Headers unless you added them yourself. Now it does (for 8bit binary header input), so decode_header needs to be able to handle them.
1 parent 213eb96 commit 041015c

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

Lib/email/header.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,15 @@ def decode_header(header):
6666
otherwise a lower-case string containing the name of the character set
6767
specified in the encoded string.
6868
69+
header may be a string that may or may not contain RFC2047 encoded words,
70+
or it may be a Header object.
71+
6972
An email.errors.HeaderParseError may be raised when certain decoding error
7073
occurs (e.g. a base64 decoding exception).
7174
"""
75+
# If it is a Header object, we can just return the chunks.
76+
if hasattr(header, '_chunks'):
77+
return list(header._chunks)
7278
# If no encoding, just return the header with no charset.
7379
if not ecre.search(header):
7480
return [(header, None)]

Lib/email/test/test_email.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3918,6 +3918,20 @@ def test_bad_8bit_header(self):
39183918
h.append(x, errors='replace')
39193919
eq(str(h), e)
39203920

3921+
def test_escaped_8bit_header(self):
3922+
x = b'Ynwp4dUEbay Auction Semiar- No Charge \x96 Earn Big'
3923+
x = x.decode('ascii', 'surrogateescape')
3924+
h = Header(x, charset=email.charset.UNKNOWN8BIT)
3925+
self.assertEqual(str(h),
3926+
'Ynwp4dUEbay Auction Semiar- No Charge \uFFFD Earn Big')
3927+
self.assertEqual(email.header.decode_header(h), [(x, 'unknown-8bit')])
3928+
3929+
def test_modify_returned_list_does_not_change_header(self):
3930+
h = Header('test')
3931+
chunks = email.header.decode_header(h)
3932+
chunks.append(('ascii', 'test2'))
3933+
self.assertEqual(str(h), 'test')
3934+
39213935
def test_encoded_adjacent_nonencoded(self):
39223936
eq = self.assertEqual
39233937
h = Header()

0 commit comments

Comments
 (0)