From a824c52eb323fdb12f8637b15603232b84fc24c4 Mon Sep 17 00:00:00 2001 From: Chris White Date: Sat, 6 Oct 2018 15:31:32 -0700 Subject: [PATCH 1/4] Add fix for NoneType --- cloudpickle/cloudpickle.py | 3 +++ tests/cloudpickle_test.py | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/cloudpickle/cloudpickle.py b/cloudpickle/cloudpickle.py index b4ab21baa..32d0826f7 100644 --- a/cloudpickle/cloudpickle.py +++ b/cloudpickle/cloudpickle.py @@ -677,6 +677,9 @@ def save_global(self, obj, name=None, pack=struct.pack): The name of this method is somewhat misleading: all types get dispatched here. """ + if obj is type(None): + return self.save_reduce(type, (None,), obj=obj) + if obj.__module__ == "__main__": return self.save_dynamic_class(obj) diff --git a/tests/cloudpickle_test.py b/tests/cloudpickle_test.py index dbe9ceb7b..84b3e54dc 100644 --- a/tests/cloudpickle_test.py +++ b/tests/cloudpickle_test.py @@ -607,6 +607,10 @@ def test_NotImplemented(self): ExcClone = pickle_depickle(NotImplemented, protocol=self.protocol) self.assertEqual(NotImplemented, ExcClone) + def test_NoneType(self): + res = pickle_depickle(type(None), protocol=self.protocol) + self.assertEqual(type(None), res) + def test_builtin_function_without_module(self): on = object.__new__ on_depickled = pickle_depickle(on, protocol=self.protocol) From c198de8a4ec052249fcf5253d2722b9eefe68e9f Mon Sep 17 00:00:00 2001 From: Chris White Date: Sat, 6 Oct 2018 15:34:26 -0700 Subject: [PATCH 2/4] Update changelog --- CHANGES.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index fde06f8fd..ca1e4823b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,9 +4,12 @@ master - Ensure that unpickling a function defined in a dynamic module several times sequentially does not reset the values of global variables. ([issue #187](https://github.com/cloudpipe/cloudpickle/issues/205)) + - Restrict the ability to pickle annotations to python3.7+ ([issue #193]( https://github.com/cloudpipe/cloudpickle/issues/193) and [issue #196](https://github.com/cloudpipe/cloudpickle/issues/196)) +- Fixed pickling issue with `NoneType` ([issue #209](https://github.com/cloudpipe/cloudpickle/issues/209)) + 0.5.6 ===== From 35d94e3ca5fd4430682dcb81809454a038f765f6 Mon Sep 17 00:00:00 2001 From: Chris White Date: Sat, 6 Oct 2018 15:44:37 -0700 Subject: [PATCH 3/4] Fix for other type singletons --- cloudpickle/cloudpickle.py | 4 ++++ tests/cloudpickle_test.py | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/cloudpickle/cloudpickle.py b/cloudpickle/cloudpickle.py index 32d0826f7..4e8825d46 100644 --- a/cloudpickle/cloudpickle.py +++ b/cloudpickle/cloudpickle.py @@ -679,6 +679,10 @@ def save_global(self, obj, name=None, pack=struct.pack): """ if obj is type(None): return self.save_reduce(type, (None,), obj=obj) + elif obj is type(Ellipsis): + return self.save_reduce(type, (Ellipsis,), obj=obj) + elif obj is type(NotImplemented): + return self.save_reduce(type, (NotImplemented,), obj=obj) if obj.__module__ == "__main__": return self.save_dynamic_class(obj) diff --git a/tests/cloudpickle_test.py b/tests/cloudpickle_test.py index 84b3e54dc..4d4442987 100644 --- a/tests/cloudpickle_test.py +++ b/tests/cloudpickle_test.py @@ -611,6 +611,14 @@ def test_NoneType(self): res = pickle_depickle(type(None), protocol=self.protocol) self.assertEqual(type(None), res) + def test_EllipsisType(self): + res = pickle_depickle(type(Ellipsis), protocol=self.protocol) + self.assertEqual(type(Ellipsis), res) + + def test_NotImplementedType(self): + res = pickle_depickle(type(NotImplemented), protocol=self.protocol) + self.assertEqual(type(NotImplemented), res) + def test_builtin_function_without_module(self): on = object.__new__ on_depickled = pickle_depickle(on, protocol=self.protocol) From 0b453e740fc5731fa52247dab0c1b7df39c6cf08 Mon Sep 17 00:00:00 2001 From: Chris White Date: Sat, 6 Oct 2018 15:46:34 -0700 Subject: [PATCH 4/4] Update changelog again --- CHANGES.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index ca1e4823b..dd3a3b75c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,7 +8,8 @@ master - Restrict the ability to pickle annotations to python3.7+ ([issue #193]( https://github.com/cloudpipe/cloudpickle/issues/193) and [issue #196](https://github.com/cloudpipe/cloudpickle/issues/196)) -- Fixed pickling issue with `NoneType` ([issue #209](https://github.com/cloudpipe/cloudpickle/issues/209)) +- Fixed pickling issue with singleton types `NoneType`, `type(...)` and + `type(NotImplemented)` ([issue #209](https://github.com/cloudpipe/cloudpickle/issues/209)) 0.5.6