Skip to content

bpo-32032: Test both implementations of module-level pickle API. #4401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions Lib/test/pickletester.py
Original file line number Diff line number Diff line change
Expand Up @@ -2534,7 +2534,7 @@ def test_dump_closed_file(self):
f = open(TESTFN, "wb")
try:
f.close()
self.assertRaises(ValueError, pickle.dump, 123, f)
self.assertRaises(ValueError, self.dump, 123, f)
finally:
os.remove(TESTFN)

Expand All @@ -2543,16 +2543,16 @@ def test_load_closed_file(self):
f = open(TESTFN, "wb")
try:
f.close()
self.assertRaises(ValueError, pickle.dump, 123, f)
self.assertRaises(ValueError, self.dump, 123, f)
finally:
os.remove(TESTFN)

def test_load_from_and_dump_to_file(self):
stream = io.BytesIO()
data = [123, {}, 124]
pickle.dump(data, stream)
self.dump(data, stream)
stream.seek(0)
unpickled = pickle.load(stream)
unpickled = self.load(stream)
self.assertEqual(unpickled, data)

def test_highest_protocol(self):
Expand All @@ -2562,20 +2562,20 @@ def test_highest_protocol(self):
def test_callapi(self):
f = io.BytesIO()
# With and without keyword arguments
pickle.dump(123, f, -1)
pickle.dump(123, file=f, protocol=-1)
pickle.dumps(123, -1)
pickle.dumps(123, protocol=-1)
pickle.Pickler(f, -1)
pickle.Pickler(f, protocol=-1)
self.dump(123, f, -1)
self.dump(123, file=f, protocol=-1)
self.dumps(123, -1)
self.dumps(123, protocol=-1)
self.Pickler(f, -1)
self.Pickler(f, protocol=-1)

def test_bad_init(self):
# Test issue3664 (pickle can segfault from a badly initialized Pickler).
# Override initialization without calling __init__() of the superclass.
class BadPickler(pickle.Pickler):
class BadPickler(self.Pickler):
def __init__(self): pass

class BadUnpickler(pickle.Unpickler):
class BadUnpickler(self.Unpickler):
def __init__(self): pass

self.assertRaises(pickle.PicklingError, BadPickler().dump, 0)
Expand Down
16 changes: 12 additions & 4 deletions Lib/test/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@
has_c_implementation = False


class PickleTests(AbstractPickleModuleTests):
pass
class PyPickleTests(AbstractPickleModuleTests):
dump = staticmethod(pickle._dump)
dumps = staticmethod(pickle._dumps)
load = staticmethod(pickle._load)
loads = staticmethod(pickle._loads)
Pickler = pickle._Pickler
Unpickler = pickle._Unpickler


class PyUnpicklerTests(AbstractUnpickleTests):
Expand Down Expand Up @@ -136,6 +141,9 @@ def get_dispatch_table(self):


if has_c_implementation:
class CPickleTests(AbstractPickleModuleTests):
from _pickle import dump, dumps, load, loads, Pickler, Unpickler

class CUnpicklerTests(PyUnpicklerTests):
unpickler = _pickle.Unpickler
bad_stack_errors = (pickle.UnpicklingError,)
Expand Down Expand Up @@ -426,12 +434,12 @@ def test_multiprocessing_exceptions(self):


def test_main():
tests = [PickleTests, PyUnpicklerTests, PyPicklerTests,
tests = [PyPickleTests, PyUnpicklerTests, PyPicklerTests,
PyPersPicklerTests, PyIdPersPicklerTests,
PyDispatchTableTests, PyChainDispatchTableTests,
CompatPickleTests]
if has_c_implementation:
tests.extend([CUnpicklerTests, CPicklerTests,
tests.extend([CPickleTests, CUnpicklerTests, CPicklerTests,
CPersPicklerTests, CIdPersPicklerTests,
CDumpPickle_LoadPickle, DumpPickle_CLoadPickle,
PyPicklerUnpicklerObjectTests,
Expand Down
3 changes: 1 addition & 2 deletions Lib/test/test_pickletools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
import pickletools
from test import support
from test.pickletester import AbstractPickleTests
from test.pickletester import AbstractPickleModuleTests
import unittest

class OptimizedPickleTests(AbstractPickleTests, AbstractPickleModuleTests):
class OptimizedPickleTests(AbstractPickleTests):

def dumps(self, arg, proto=None):
return pickletools.optimize(pickle.dumps(arg, proto))
Expand Down