(original) (raw)

Index: Doc/library/email.header.rst =================================================================== --- Doc/library/email.header.rst (revision 85191) +++ Doc/library/email.header.rst (working copy) @@ -148,16 +148,18 @@ Decode a message header value without converting the character set. The header value is in *header*. - This function returns a list of ``(decoded_string, charset)`` pairs containing + This function returns a list of ``(decoded_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. - Here's an example:: + Here are some example: >>> from email.header import decode_header + >>> decode_header('No encoded words') + [(b'no encoded words', None)] >>> decode_header('=?iso-8859-1?q?p=F6stal?=') - [('p\xf6stal', 'iso-8859-1')] + [(b'p\xf6stal', 'iso-8859-1')] .. function:: make_header(decoded_seq, maxlinelen=None, header_name=None, continuation_ws=' ') Index: Lib/email/header.py =================================================================== --- Lib/email/header.py (revision 85191) +++ Lib/email/header.py (working copy) @@ -56,7 +56,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. @@ -66,7 +66,7 @@ """ # If no encoding, just return the header with no charset. if not ecre.search(header): - return [(header, None)] + return [(header.encode('ascii'), 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. Index: Lib/email/test/test_email.py =================================================================== --- Lib/email/test/test_email.py (revision 85191) +++ Lib/email/test/test_email.py (working copy) @@ -1602,6 +1602,13 @@ # Test RFC 2047 header encoding and decoding class TestRFC2047(TestEmailBase): + + def test_no_decoding_needed(self): + eq = self.assertEqual + s = "Re: Re: The Department of Redundant Redundancy" + dh = decode_header(s) + eq(dh, [(b'Re: Re: The Department of Redundant Redundancy', None)]) + def test_rfc2047_multiline(self): eq = self.assertEqual s = """Re: =?mac-iceland?q?r=8Aksm=9Arg=8Cs?= baz @@ -1640,7 +1647,7 @@ def test_rfc2047_missing_whitespace(self): s = 'Sm=?ISO-8859-1?B?9g==?=rg=?ISO-8859-1?B?5Q==?=sbord' dh = decode_header(s) - self.assertEqual(dh, [(s, None)]) + self.assertEqual(dh, [(s.encode('ascii'), None)]) def test_rfc2047_with_whitespace(self): s = 'Sm =?ISO-8859-1?B?9g==?= rg =?ISO-8859-1?B?5Q==?= sbord' @@ -2935,7 +2942,7 @@ def test_header_needs_no_decoding(self): h = 'no decoding needed' - self.assertEqual(decode_header(h), [(h, None)]) + self.assertEqual(decode_header(h), [(h.encode('ascii'), None)]) def test_long(self): h = Header("I am the very model of a modern Major-General; I've information vegetable, animal, and mineral; I know the kings of England, and I quote the fights historical from Marathon to Waterloo, in order categorical; I'm very well acquainted, too, with matters mathematical; I understand equations, both the simple and quadratical; about binomial theorem I'm teeming with a lot o' news, with many cheerful facts about the square of the hypotenuse.", @@ -3163,7 +3170,7 @@ eq = self.assertEqual s = 'hello' x = decode_header(s) - eq(x, [('hello', None)]) + eq(x, [(b'hello', None)]) h = make_header(x) eq(s, h.encode())