From: Ɓukasz Langa Date: Mon, 12 Mar 2012 18:46:12 +0000 (+0100) Subject: Fixes #13842: cannot pickle Ellipsis or NotImplemented. X-Git-Tag: v3.3.0a2~234^2~10^2^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f3078fbee2b6555f0f5b1819a231f4b7d8bdf6b5;p=python Fixes #13842: cannot pickle Ellipsis or NotImplemented. Thanks for James Sanders for the bug report and the patch. --- diff --git a/Lib/pickle.py b/Lib/pickle.py index 20b3646a5b..9e65368327 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -438,6 +438,14 @@ class _Pickler: self.write(NONE) dispatch[type(None)] = save_none + def save_ellipsis(self, obj): + self.save_global(Ellipsis, 'Ellipsis') + dispatch[type(Ellipsis)] = save_ellipsis + + def save_notimplemented(self, obj): + self.save_global(NotImplemented, 'NotImplemented') + dispatch[type(NotImplemented)] = save_notimplemented + def save_bool(self, obj): if self.proto >= 2: self.write(obj and NEWTRUE or NEWFALSE) diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py index 1a551c8bde..3686a62c66 100644 --- a/Lib/test/pickletester.py +++ b/Lib/test/pickletester.py @@ -743,6 +743,18 @@ class AbstractPickleTests(unittest.TestCase): u = self.loads(s) self.assertEqual(t, u) + def test_ellipsis(self): + for proto in protocols: + s = self.dumps(..., proto) + u = self.loads(s) + self.assertEqual(..., u) + + def test_notimplemented(self): + for proto in protocols: + s = self.dumps(NotImplemented, proto) + u = self.loads(s) + self.assertEqual(NotImplemented, u) + # Tests for protocol 2 def test_proto(self): diff --git a/Misc/ACKS b/Misc/ACKS index 49efa297f7..a0685c1a0f 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -883,6 +883,7 @@ George Sakkis Rich Salz Kevin Samborn Adrian Sampson +James Sanders Ilya Sandler Mark Sapiro Ty Sarna diff --git a/Modules/_pickle.c b/Modules/_pickle.c index 2dc3a4116d..4212e7a02f 100644 --- a/Modules/_pickle.c +++ b/Modules/_pickle.c @@ -2811,6 +2811,19 @@ save_global(PicklerObject *self, PyObject *obj, PyObject *name) return status; } +static int +save_ellipsis(PicklerObject *self, PyObject *obj) +{ + return save_global(self, Py_Ellipsis, PyUnicode_FromString("Ellipsis")); +} + +static int +save_notimplemented(PicklerObject *self, PyObject *obj) +{ + return save_global(self, Py_NotImplemented, + PyUnicode_FromString("NotImplemented")); +} + static int save_pers(PicklerObject *self, PyObject *obj, PyObject *func) { @@ -3114,6 +3127,14 @@ save(PicklerObject *self, PyObject *obj, int pers_save) status = save_none(self, obj); goto done; } + else if (obj == Py_Ellipsis) { + status = save_ellipsis(self, obj); + goto done; + } + else if (obj == Py_NotImplemented) { + status = save_notimplemented(self, obj); + goto done; + } else if (obj == Py_False || obj == Py_True) { status = save_bool(self, obj); goto done;