Issue #6477: Added pickling support for singletons and their types.
diff --git a/Lib/pickle.py b/Lib/pickle.py
index 299de16..5d0aba7 100644
--- a/Lib/pickle.py
+++ b/Lib/pickle.py
@@ -427,6 +427,14 @@
self.write(NONE)
dispatch[NoneType] = 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)
@@ -770,7 +778,18 @@
dispatch[ClassType] = save_global
dispatch[FunctionType] = save_global
dispatch[BuiltinFunctionType] = save_global
- dispatch[TypeType] = save_global
+
+ def save_type(self, obj):
+ if obj is type(None):
+ return self.save_reduce(type, (None,), obj=obj)
+ elif obj is type(NotImplemented):
+ return self.save_reduce(type, (NotImplemented,), obj=obj)
+ elif obj is type(Ellipsis):
+ return self.save_reduce(type, (Ellipsis,), obj=obj)
+ return self.save_global(obj)
+
+ dispatch[TypeType] = save_type
+
# Pickling helpers
diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py
index 1599893..d7b68db 100644
--- a/Lib/test/pickletester.py
+++ b/Lib/test/pickletester.py
@@ -661,6 +661,15 @@
u = self.loads(s)
self.assertEqual(t, u)
+ def test_singleton_types(self):
+ # Issue #6477: Test that types of built-in singletons can be pickled.
+ singletons = [None, Ellipsis, NotImplemented]
+ for singleton in singletons:
+ for proto in protocols:
+ s = self.dumps(type(singleton), proto)
+ u = self.loads(s)
+ self.assertIs(type(singleton), u)
+
# Tests for protocol 2
def test_proto(self):