Issue #6477: Added support for pickling the types of built-in singletons.
diff --git a/Lib/pickle.py b/Lib/pickle.py
index d62f014..386ffba 100644
--- a/Lib/pickle.py
+++ b/Lib/pickle.py
@@ -728,9 +728,18 @@
self.memoize(obj)
+ 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(...):
+ return self.save_reduce(type, (...,), obj=obj)
+ return self.save_global(obj)
+
dispatch[FunctionType] = save_global
dispatch[BuiltinFunctionType] = save_global
- dispatch[type] = save_global
+ dispatch[type] = save_type
# Pickling helpers
diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py
index 86869e1..86d668e 100644
--- a/Lib/test/pickletester.py
+++ b/Lib/test/pickletester.py
@@ -768,6 +768,15 @@
u = self.loads(s)
self.assertEqual(NotImplemented, u)
+ def test_singleton_types(self):
+ # Issue #6477: Test that types of built-in singletons can be pickled.
+ singletons = [None, ..., 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):