]> granicus.if.org Git - python/commitdiff
Issue #8383: pickle and pickletools use surrogatepass error handler when
authorVictor Stinner <victor.stinner@haypocalc.com>
Tue, 13 Apr 2010 11:07:24 +0000 (11:07 +0000)
committerVictor Stinner <victor.stinner@haypocalc.com>
Tue, 13 Apr 2010 11:07:24 +0000 (11:07 +0000)
encoding unicode as utf8 to support lone surrogates and stay compatible with
Python 2.x and 3.0

Lib/pickle.py
Lib/pickletools.py
Lib/test/pickletester.py
Misc/NEWS
Modules/_pickle.c

index 8a2abcc1382af8eaa637339625e7124b6d7bdb62..c4fc2c4d1f6b07a866b39b2883b53d86b487561c 100644 (file)
@@ -499,7 +499,7 @@ class _Pickler:
 
     def save_str(self, obj, pack=struct.pack):
         if self.bin:
-            encoded = obj.encode('utf-8')
+            encoded = obj.encode('utf-8', 'surrogatepass')
             n = len(encoded)
             self.write(BINUNICODE + pack("<i", n) + encoded)
         else:
@@ -966,7 +966,7 @@ class _Unpickler:
 
     def load_binunicode(self):
         len = mloads(b'i' + self.read(4))
-        self.append(str(self.read(len), 'utf-8'))
+        self.append(str(self.read(len), 'utf-8', 'surrogatepass'))
     dispatch[BINUNICODE[0]] = load_binunicode
 
     def load_short_binstring(self):
index ca11aa3871852b5f5512ae61f884e0677bbdf73c..6ab75c7ab77bcf28dc85fee242b9f7cc4a091e98 100644 (file)
@@ -469,7 +469,7 @@ def read_unicodestring4(f):
         raise ValueError("unicodestring4 byte count < 0: %d" % n)
     data = f.read(n)
     if len(data) == n:
-        return str(data, 'utf-8')
+        return str(data, 'utf-8', 'surrogatepass')
     raise ValueError("expected %d bytes in a unicodestring4, but only %d "
                      "remain" % (n, len(data)))
 
index 79407a698dfade582f06aa223fafcf701de9eda1..dd0ed15f2db2df21250807473aa1cd07903d15ee 100644 (file)
@@ -515,7 +515,9 @@ class AbstractPickleTests(unittest.TestCase):
 
     def test_unicode(self):
         endcases = ['', '<\\u>', '<\\\u1234>', '<\n>',
-                    '<\\>', '<\\\U00012345>']
+                    '<\\>', '<\\\U00012345>',
+                    # surrogates
+                    '<\udc80>']
         for proto in protocols:
             for u in endcases:
                 p = self.dumps(u, proto)
index 8f80a967f9bff3546b20c1bf193b54ae41ca6b7f..35a405f85f6ab3b7d40e33d2595c2ac84e794416 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -312,6 +312,10 @@ C-API
 Library
 -------
 
+- Issue #8383: pickle and pickletools use surrogatepass error handler when
+  encoding unicode as utf8 to support lone surrogates and stay compatible with
+  Python 2.x and 3.0
+
 - Issue #7585: difflib context and unified diffs now place a tab between
   filename and date, conforming to the 'standards' they were originally
   designed to follow.  This improves compatibility with patch tools.
index 29aed7adb3b8d8ea4fbefeb1900d094038d6d180..0e1c2cdc8828af1c791749ae95c305171d336787 100644 (file)
@@ -1227,7 +1227,9 @@ save_unicode(PicklerObject *self, PyObject *obj)
     if (self->bin) {
         char pdata[5];
 
-        encoded = PyUnicode_AsUTF8String(obj);
+        encoded = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(obj),
+                                    PyUnicode_GET_SIZE(obj),
+                                    "surrogatepass");
         if (encoded == NULL)
             goto error;
 
@@ -3352,7 +3354,7 @@ load_binunicode(UnpicklerObject *self)
     if (unpickler_read(self, &s, size) < 0)
         return -1;
 
-    str = PyUnicode_DecodeUTF8(s, size, NULL);
+    str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
     if (str == NULL)
         return -1;