From 485fb56eb86a1fcd35fd3d0d37efb5ec514dba2b Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 13 Apr 2010 11:07:24 +0000 Subject: [PATCH] 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 --- Lib/pickle.py | 4 ++-- Lib/pickletools.py | 2 +- Lib/test/pickletester.py | 4 +++- Misc/NEWS | 4 ++++ Modules/_pickle.c | 6 ++++-- 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Lib/pickle.py b/Lib/pickle.py index 8a2abcc1382..c4fc2c4d1f6 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -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("', '<\\\u1234>', '<\n>', - '<\\>', '<\\\U00012345>'] + '<\\>', '<\\\U00012345>', + # surrogates + '<\udc80>'] for proto in protocols: for u in endcases: p = self.dumps(u, proto) diff --git a/Misc/NEWS b/Misc/NEWS index 8f80a967f9b..35a405f85f6 100644 --- 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. diff --git a/Modules/_pickle.c b/Modules/_pickle.c index 29aed7adb3b..0e1c2cdc882 100644 --- a/Modules/_pickle.c +++ b/Modules/_pickle.c @@ -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;