Skip to content

Commit 0397f04

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 b2076b0 commit 0397f04

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
@@ -1980,6 +1982,33 @@ def test_singleton_types(self):
19801982
u = self.loads(s)
19811983
self.assertIs(type(singleton), u)
19821984

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

19852014
def test_proto(self):

0 commit comments

Comments
 (0)