[Python-checkins] cpython (merge 3.2 -> default): Merge #11584: Since __getitem__ returns headers, make decode_header handle them.

r.david.murray python-checkins at python.org
Fri Mar 25 20:32:57 CET 2011


http://hg.python.org/cpython/rev/12e39cd7a0e4
changeset:   68929:12e39cd7a0e4
parent:      68926:9da300ad8255
parent:      68928:b21fdfa0019c
user:        R David Murray <rdmurray at bitdance.com>
date:        Fri Mar 25 15:31:52 2011 -0400
summary:
  Merge #11584: Since __getitem__ returns headers, make decode_header handle them.

files:
  Lib/email/header.py               |   6 ++++++
  Lib/test/test_email/test_email.py |  14 ++++++++++++++
  2 files changed, 20 insertions(+), 0 deletions(-)


diff --git a/Lib/email/header.py b/Lib/email/header.py
--- a/Lib/email/header.py
+++ b/Lib/email/header.py
@@ -66,9 +66,15 @@
     otherwise a lower-case string containing the name of the character set
     specified in the encoded string.
 
+    header may be a string that may or may not contain RFC2047 encoded words,
+    or it may be a Header object.
+
     An email.errors.HeaderParseError may be raised when certain decoding error
     occurs (e.g. a base64 decoding exception).
     """
+    # If it is a Header object, we can just return the chunks.
+    if hasattr(header, '_chunks'):
+        return list(header._chunks)
     # If no encoding, just return the header with no charset.
     if not ecre.search(header):
         return [(header, None)]
diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py
--- a/Lib/test/test_email/test_email.py
+++ b/Lib/test/test_email/test_email.py
@@ -3925,6 +3925,20 @@
         h.append(x, errors='replace')
         eq(str(h), e)
 
+    def test_escaped_8bit_header(self):
+        x = b'Ynwp4dUEbay Auction Semiar- No Charge \x96 Earn Big'
+        x = x.decode('ascii', 'surrogateescape')
+        h = Header(x, charset=email.charset.UNKNOWN8BIT)
+        self.assertEqual(str(h),
+                        'Ynwp4dUEbay Auction Semiar- No Charge \uFFFD Earn Big')
+        self.assertEqual(email.header.decode_header(h), [(x, 'unknown-8bit')])
+
+    def test_modify_returned_list_does_not_change_header(self):
+        h = Header('test')
+        chunks = email.header.decode_header(h)
+        chunks.append(('ascii', 'test2'))
+        self.assertEqual(str(h), 'test')
+
     def test_encoded_adjacent_nonencoded(self):
         eq = self.assertEqual
         h = Header()

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list