diff Lib/test/test_codeccallbacks.py @ 64590:fc76e8c617dc

Issue #9804: ascii() now always represents unicode surrogate pairs as a single `\UXXXXXXXX`, regardless of whether the character is printable or not. Also, the "backslashreplace" error handler now joins surrogate pairs into a single character on UCS-2 builds.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 09 Sep 2010 20:30:23 +0000
parents e0d5a4a49112
children 9fe76c26ea9f
line wrap: on
line diff
--- a/Lib/test/test_codeccallbacks.py	Thu Sep 09 18:33:21 2010 +0000
+++ b/Lib/test/test_codeccallbacks.py	Thu Sep 09 20:30:23 2010 +0000
@@ -577,17 +577,31 @@
                 UnicodeEncodeError("ascii", "\uffff", 0, 1, "ouch")),
             ("\\uffff", 1)
         )
-        if sys.maxunicode>0xffff:
-            self.assertEquals(
-                codecs.backslashreplace_errors(
-                    UnicodeEncodeError("ascii", "\U00010000", 0, 1, "ouch")),
-                ("\\U00010000", 1)
-            )
-            self.assertEquals(
-                codecs.backslashreplace_errors(
-                    UnicodeEncodeError("ascii", "\U0010ffff", 0, 1, "ouch")),
-                ("\\U0010ffff", 1)
-            )
+        # 1 on UCS-4 builds, 2 on UCS-2
+        len_wide = len("\U00010000")
+        self.assertEquals(
+            codecs.backslashreplace_errors(
+                UnicodeEncodeError("ascii", "\U00010000",
+                                   0, len_wide, "ouch")),
+            ("\\U00010000", len_wide)
+        )
+        self.assertEquals(
+            codecs.backslashreplace_errors(
+                UnicodeEncodeError("ascii", "\U0010ffff",
+                                   0, len_wide, "ouch")),
+            ("\\U0010ffff", len_wide)
+        )
+        # Lone surrogates (regardless of unicode width)
+        self.assertEquals(
+            codecs.backslashreplace_errors(
+                UnicodeEncodeError("ascii", "\ud800", 0, 1, "ouch")),
+            ("\\ud800", 1)
+        )
+        self.assertEquals(
+            codecs.backslashreplace_errors(
+                UnicodeEncodeError("ascii", "\udfff", 0, 1, "ouch")),
+            ("\\udfff", 1)
+        )
 
     def test_badhandlerresults(self):
         results = ( 42, "foo", (1,2,3), ("foo", 1, 3), ("foo", None), ("foo",), ("foo", 1, 3), ("foo", None), ("foo",) )