gh-100129: Add tests for pickling all builtin types and functions (GH-100142)

This commit is contained in:
Serhiy Storchaka 2022-12-21 16:31:22 +02:00 committed by GitHub
parent 12be23cf3c
commit b98d2d31bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 0 deletions

View File

@ -1,3 +1,4 @@
import builtins
import collections
import copyreg
import dbm
@ -11,6 +12,7 @@ import shutil
import struct
import sys
import threading
import types
import unittest
import weakref
from textwrap import dedent
@ -1980,6 +1982,33 @@ class AbstractPickleTests:
u = self.loads(s)
self.assertIs(type(singleton), u)
def test_builtin_types(self):
for t in builtins.__dict__.values():
if isinstance(t, type) and not issubclass(t, BaseException):
for proto in protocols:
s = self.dumps(t, proto)
self.assertIs(self.loads(s), t)
def test_builtin_exceptions(self):
for t in builtins.__dict__.values():
if isinstance(t, type) and issubclass(t, BaseException):
for proto in protocols:
s = self.dumps(t, proto)
u = self.loads(s)
if proto <= 2 and issubclass(t, OSError) and t is not BlockingIOError:
self.assertIs(u, OSError)
elif proto <= 2 and issubclass(t, ImportError):
self.assertIs(u, ImportError)
else:
self.assertIs(u, t)
def test_builtin_functions(self):
for t in builtins.__dict__.values():
if isinstance(t, types.BuiltinFunctionType):
for proto in protocols:
s = self.dumps(t, proto)
self.assertIs(self.loads(s), t)
# Tests for protocol 2
def test_proto(self):