]> granicus.if.org Git - python/commitdiff
#11584: make Header and make_header handle binary unknown-8bit input
authorR David Murray <rdmurray@bitdance.com>
Sat, 18 Jun 2011 16:57:28 +0000 (12:57 -0400)
committerR David Murray <rdmurray@bitdance.com>
Sat, 18 Jun 2011 16:57:28 +0000 (12:57 -0400)
Analogous to the decode_header fix, this fix makes Header.append and
make_header correctly handle the unknown-8bit charset introduced by email5.1,
when the input to them is binary strings.  Previous to this fix the
make_header(decode_header(x)) == x invariant was broken in the face of the
unknown-8bit charset.

Lib/email/header.py
Lib/email/test/test_email.py
Misc/NEWS

index 06708853c2d90de91981c5300b658ceabeb21db6..2e687b7a6f10742e25e9ea33d2ddb06a5e616c7d 100644 (file)
@@ -275,7 +275,10 @@ class Header:
             charset = Charset(charset)
         if not isinstance(s, str):
             input_charset = charset.input_codec or 'us-ascii'
-            s = s.decode(input_charset, errors)
+            if input_charset == _charset.UNKNOWN8BIT:
+                s = s.decode('us-ascii', 'surrogateescape')
+            else:
+                s = s.decode(input_charset, errors)
         # Ensure that the bytes we're storing can be decoded to the output
         # character set, otherwise an early error is thrown.
         output_charset = charset.output_codec or 'us-ascii'
index 97a1e86a25f65306aa01dea736b2ca7a15d5d72d..102e15b9ff053a3242eb6807ec727f6270bd0a57 100644 (file)
@@ -4182,6 +4182,21 @@ A very long line that must get split to something other than at the
                         'Ynwp4dUEbay Auction Semiar- No Charge \uFFFD Earn Big')
         self.assertEqual(email.header.decode_header(h), [(x, 'unknown-8bit')])
 
+    def test_header_handles_binary_unknown8bit(self):
+        x = b'Ynwp4dUEbay Auction Semiar- No Charge \x96 Earn Big'
+        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_make_header_handles_binary_unknown8bit(self):
+        x = b'Ynwp4dUEbay Auction Semiar- No Charge \x96 Earn Big'
+        h = Header(x, charset=email.charset.UNKNOWN8BIT)
+        h2 = email.header.make_header(email.header.decode_header(h))
+        self.assertEqual(str(h2),
+                        'Ynwp4dUEbay Auction Semiar- No Charge \uFFFD Earn Big')
+        self.assertEqual(email.header.decode_header(h2), [(x, 'unknown-8bit')])
+
     def test_modify_returned_list_does_not_change_header(self):
         h = Header('test')
         chunks = email.header.decode_header(h)
index 9c3f69347e5c2c849da7590be4c486577f032a83..0ec55b6a9aa4bbbeef5231a2c8f0eb6ec2097c39 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -26,7 +26,8 @@ Library
 -------
 
 - Issue #11584: email.header.decode_header no longer fails if the header
-  passed to it is a Header object.
+  passed to it is a Header object, and Header/make_header no longer fail
+  if given binary unknown-8bit input.
 
 - Issue #11700: mailbox proxy object close methods can now be called multiple
   times without error.