Skip to content

Commit 2dda668

Browse files
gh-100129: Add tests for pickling all builtin types and functions (GH-100142)
(cherry picked from commit b98d2d3) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 7f24056 commit 2dda668

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Lib/test/pickletester.py

+29
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import builtins
12
import collections
23
import copyreg
34
import dbm
@@ -11,6 +12,7 @@
1112
import struct
1213
import sys
1314
import threading
15+
import types
1416
import unittest
1517
import weakref
1618
from textwrap import dedent
@@ -1979,6 +1981,33 @@ def test_singleton_types(self):
19791981
u = self.loads(s)
19801982
self.assertIs(type(singleton), u)
19811983

1984+
def test_builtin_types(self):
1985+
for t in builtins.__dict__.values():
1986+
if isinstance(t, type) and not issubclass(t, BaseException):
1987+
for proto in protocols:
1988+
s = self.dumps(t, proto)
1989+
self.assertIs(self.loads(s), t)
1990+
1991+
def test_builtin_exceptions(self):
1992+
for t in builtins.__dict__.values():
1993+
if isinstance(t, type) and issubclass(t, BaseException):
1994+
for proto in protocols:
1995+
s = self.dumps(t, proto)
1996+
u = self.loads(s)
1997+
if proto <= 2 and issubclass(t, OSError) and t is not BlockingIOError:
1998+
self.assertIs(u, OSError)
1999+
elif proto <= 2 and issubclass(t, ImportError):
2000+
self.assertIs(u, ImportError)
2001+
else:
2002+
self.assertIs(u, t)
2003+
2004+
def test_builtin_functions(self):
2005+
for t in builtins.__dict__.values():
2006+
if isinstance(t, types.BuiltinFunctionType):
2007+
for proto in protocols:
2008+
s = self.dumps(t, proto)
2009+
self.assertIs(self.loads(s), t)
2010+
19822011
# Tests for protocol 2
19832012

19842013
def test_proto(self):

0 commit comments

Comments
 (0)