Commit 655720e2 authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #22777: Test pickling with all protocols.

parent c3741a06
......@@ -14,9 +14,9 @@ class X(Structure):
class Y(X):
_fields_ = [("str", c_char_p)]
class PickleTest(unittest.TestCase):
class PickleTest:
def dumps(self, item):
return pickle.dumps(item)
return pickle.dumps(item, self.proto)
def loads(self, item):
return pickle.loads(item)
......@@ -67,17 +67,15 @@ class PickleTest(unittest.TestCase):
self.assertRaises(ValueError, lambda: self.dumps(item))
def test_wchar(self):
pickle.dumps(c_char("x"))
self.dumps(c_char(b"x"))
# Issue 5049
pickle.dumps(c_wchar(u"x"))
self.dumps(c_wchar(u"x"))
class PickleTest_1(PickleTest):
def dumps(self, item):
return pickle.dumps(item, 1)
class PickleTest_2(PickleTest):
def dumps(self, item):
return pickle.dumps(item, 2)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
name = 'PickleTest_%s' % proto
globals()[name] = type(name,
(PickleTest, unittest.TestCase),
{'proto': proto})
if __name__ == "__main__":
unittest.main()
......@@ -61,7 +61,8 @@ class AudioTests:
self.assertEqual(params,
(nchannels, sampwidth, framerate, nframes, comptype, compname))
dump = pickle.dumps(params)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
dump = pickle.dumps(params, proto)
self.assertEqual(pickle.loads(dump), params)
......
......@@ -305,42 +305,40 @@ class BoolTest(unittest.TestCase):
def test_pickle(self):
import pickle
self.assertIs(pickle.loads(pickle.dumps(True)), True)
self.assertIs(pickle.loads(pickle.dumps(False)), False)
self.assertIs(pickle.loads(pickle.dumps(True, True)), True)
self.assertIs(pickle.loads(pickle.dumps(False, True)), False)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
self.assertIs(pickle.loads(pickle.dumps(True, proto)), True)
self.assertIs(pickle.loads(pickle.dumps(False, proto)), False)
def test_cpickle(self):
import cPickle
self.assertIs(cPickle.loads(cPickle.dumps(True)), True)
self.assertIs(cPickle.loads(cPickle.dumps(False)), False)
self.assertIs(cPickle.loads(cPickle.dumps(True, True)), True)
self.assertIs(cPickle.loads(cPickle.dumps(False, True)), False)
for proto in range(cPickle.HIGHEST_PROTOCOL + 1):
self.assertIs(cPickle.loads(cPickle.dumps(True, proto)), True)
self.assertIs(cPickle.loads(cPickle.dumps(False, proto)), False)
def test_mixedpickle(self):
import pickle, cPickle
self.assertIs(pickle.loads(cPickle.dumps(True)), True)
self.assertIs(pickle.loads(cPickle.dumps(False)), False)
self.assertIs(pickle.loads(cPickle.dumps(True, True)), True)
self.assertIs(pickle.loads(cPickle.dumps(False, True)), False)
self.assertIs(cPickle.loads(pickle.dumps(True)), True)
self.assertIs(cPickle.loads(pickle.dumps(False)), False)
self.assertIs(cPickle.loads(pickle.dumps(True, True)), True)
self.assertIs(cPickle.loads(pickle.dumps(False, True)), False)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
self.assertIs(pickle.loads(cPickle.dumps(True, proto)), True)
self.assertIs(pickle.loads(cPickle.dumps(False, proto)), False)
self.assertIs(cPickle.loads(pickle.dumps(True, proto)), True)
self.assertIs(cPickle.loads(pickle.dumps(False, proto)), False)
def test_picklevalues(self):
import pickle, cPickle
# Test for specific backwards-compatible pickle values
self.assertEqual(pickle.dumps(True), "I01\n.")
self.assertEqual(pickle.dumps(False), "I00\n.")
self.assertEqual(cPickle.dumps(True), "I01\n.")
self.assertEqual(cPickle.dumps(False), "I00\n.")
self.assertEqual(pickle.dumps(True, True), "I01\n.")
self.assertEqual(pickle.dumps(False, True), "I00\n.")
self.assertEqual(cPickle.dumps(True, True), "I01\n.")
self.assertEqual(cPickle.dumps(False, True), "I00\n.")
self.assertEqual(pickle.dumps(True, protocol=0), "I01\n.")
self.assertEqual(pickle.dumps(False, protocol=0), "I00\n.")
self.assertEqual(cPickle.dumps(True, protocol=0), "I01\n.")
self.assertEqual(cPickle.dumps(False, protocol=0), "I00\n.")
self.assertEqual(pickle.dumps(True, protocol=1), "I01\n.")
self.assertEqual(pickle.dumps(False, protocol=1), "I00\n.")
self.assertEqual(cPickle.dumps(True, protocol=1), "I01\n.")
self.assertEqual(cPickle.dumps(False, protocol=1), "I00\n.")
self.assertEqual(pickle.dumps(True, protocol=2), b'\x80\x02\x88.')
self.assertEqual(pickle.dumps(False, protocol=2), b'\x80\x02\x89.')
self.assertEqual(cPickle.dumps(True, protocol=2), b'\x80\x02\x88.')
self.assertEqual(cPickle.dumps(False, protocol=2), b'\x80\x02\x89.')
def test_convert_to_bool(self):
# Verify that TypeError occurs when bad things are returned
......
......@@ -614,7 +614,8 @@ class ExceptionPicklingTestCase(unittest.TestCase):
def test_error(self):
import pickle
e1 = ConfigParser.Error('value')
pickled = pickle.dumps(e1)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
pickled = pickle.dumps(e1, proto)
e2 = pickle.loads(pickled)
self.assertEqual(e1.message, e2.message)
self.assertEqual(repr(e1), repr(e2))
......@@ -622,7 +623,8 @@ class ExceptionPicklingTestCase(unittest.TestCase):
def test_nosectionerror(self):
import pickle
e1 = ConfigParser.NoSectionError('section')
pickled = pickle.dumps(e1)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
pickled = pickle.dumps(e1, proto)
e2 = pickle.loads(pickled)
self.assertEqual(e1.message, e2.message)
self.assertEqual(e1.args, e2.args)
......@@ -632,7 +634,8 @@ class ExceptionPicklingTestCase(unittest.TestCase):
def test_nooptionerror(self):
import pickle
e1 = ConfigParser.NoOptionError('option', 'section')
pickled = pickle.dumps(e1)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
pickled = pickle.dumps(e1, proto)
e2 = pickle.loads(pickled)
self.assertEqual(e1.message, e2.message)
self.assertEqual(e1.args, e2.args)
......@@ -643,7 +646,8 @@ class ExceptionPicklingTestCase(unittest.TestCase):
def test_duplicatesectionerror(self):
import pickle
e1 = ConfigParser.DuplicateSectionError('section')
pickled = pickle.dumps(e1)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
pickled = pickle.dumps(e1, proto)
e2 = pickle.loads(pickled)
self.assertEqual(e1.message, e2.message)
self.assertEqual(e1.args, e2.args)
......@@ -653,7 +657,8 @@ class ExceptionPicklingTestCase(unittest.TestCase):
def test_interpolationerror(self):
import pickle
e1 = ConfigParser.InterpolationError('option', 'section', 'msg')
pickled = pickle.dumps(e1)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
pickled = pickle.dumps(e1, proto)
e2 = pickle.loads(pickled)
self.assertEqual(e1.message, e2.message)
self.assertEqual(e1.args, e2.args)
......@@ -665,7 +670,8 @@ class ExceptionPicklingTestCase(unittest.TestCase):
import pickle
e1 = ConfigParser.InterpolationMissingOptionError('option', 'section',
'rawval', 'reference')
pickled = pickle.dumps(e1)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
pickled = pickle.dumps(e1, proto)
e2 = pickle.loads(pickled)
self.assertEqual(e1.message, e2.message)
self.assertEqual(e1.args, e2.args)
......@@ -677,7 +683,8 @@ class ExceptionPicklingTestCase(unittest.TestCase):
def test_interpolationsyntaxerror(self):
import pickle
e1 = ConfigParser.InterpolationSyntaxError('option', 'section', 'msg')
pickled = pickle.dumps(e1)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
pickled = pickle.dumps(e1, proto)
e2 = pickle.loads(pickled)
self.assertEqual(e1.message, e2.message)
self.assertEqual(e1.args, e2.args)
......@@ -689,7 +696,8 @@ class ExceptionPicklingTestCase(unittest.TestCase):
import pickle
e1 = ConfigParser.InterpolationDepthError('option', 'section',
'rawval')
pickled = pickle.dumps(e1)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
pickled = pickle.dumps(e1, proto)
e2 = pickle.loads(pickled)
self.assertEqual(e1.message, e2.message)
self.assertEqual(e1.args, e2.args)
......@@ -703,7 +711,8 @@ class ExceptionPicklingTestCase(unittest.TestCase):
e1.append(1, 'line1')
e1.append(2, 'line2')
e1.append(3, 'line3')
pickled = pickle.dumps(e1)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
pickled = pickle.dumps(e1, proto)
e2 = pickle.loads(pickled)
self.assertEqual(e1.message, e2.message)
self.assertEqual(e1.args, e2.args)
......@@ -714,7 +723,8 @@ class ExceptionPicklingTestCase(unittest.TestCase):
def test_missingsectionheadererror(self):
import pickle
e1 = ConfigParser.MissingSectionHeaderError('filename', 123, 'line')
pickled = pickle.dumps(e1)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
pickled = pickle.dumps(e1, proto)
e2 = pickle.loads(pickled)
self.assertEqual(e1.message, e2.message)
self.assertEqual(e1.args, e2.args)
......
......@@ -1421,7 +1421,8 @@ class TestDateTime(TestDate):
def test_more_pickling(self):
a = self.theclass(2003, 2, 7, 16, 48, 37, 444116)
s = pickle.dumps(a)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
s = pickle.dumps(a, proto)
b = pickle.loads(s)
self.assertEqual(b.year, 2003)
self.assertEqual(b.month, 2)
......
......@@ -1664,7 +1664,8 @@ class DecimalPythonAPItests(unittest.TestCase):
def test_pickle(self):
d = Decimal('-3.141590000')
p = pickle.dumps(d)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
p = pickle.dumps(d, proto)
e = pickle.loads(p)
self.assertEqual(d, e)
......@@ -1750,8 +1751,9 @@ class DecimalPythonAPItests(unittest.TestCase):
class ContextAPItests(unittest.TestCase):
def test_pickle(self):
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
c = Context()
e = pickle.loads(pickle.dumps(c))
e = pickle.loads(pickle.dumps(c, proto))
for k in vars(c):
v1 = vars(c)[k]
v2 = vars(e)[k]
......
......@@ -600,7 +600,8 @@ class TestSubclass(unittest.TestCase):
self.assertEqual(type(d), type(e))
self.assertEqual(list(d), list(e))
s = pickle.dumps(d)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
s = pickle.dumps(d, proto)
e = pickle.loads(s)
self.assertNotEqual(id(d), id(e))
self.assertEqual(type(d), type(e))
......@@ -616,7 +617,8 @@ class TestSubclass(unittest.TestCase):
self.assertEqual(type(d), type(e))
self.assertEqual(list(d), list(e))
s = pickle.dumps(d)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
s = pickle.dumps(d, proto)
e = pickle.loads(s)
self.assertNotEqual(id(d), id(e))
self.assertEqual(type(d), type(e))
......
......@@ -3300,7 +3300,7 @@ order (MRO) for bases """
pass
for p in pickle, cPickle:
for bin in 0, 1:
for bin in range(p.HIGHEST_PROTOCOL + 1):
for cls in C, C1, C2:
s = p.dumps(cls, bin)
cls2 = p.loads(s)
......@@ -3358,26 +3358,27 @@ order (MRO) for bases """
__slots__ = ['a']
class D(C):
pass
for proto in range(2):
try:
pickle.dumps(C())
pickle.dumps(C(), proto)
except TypeError:
pass
else:
self.fail("should fail: pickle C instance - %s" % base)
try:
cPickle.dumps(C())
cPickle.dumps(C(), proto)
except TypeError:
pass
else:
self.fail("should fail: cPickle C instance - %s" % base)
try:
pickle.dumps(C())
pickle.dumps(C(), proto)
except TypeError:
pass
else:
self.fail("should fail: pickle D instance - %s" % base)
try:
cPickle.dumps(D())
cPickle.dumps(D(), proto)
except TypeError:
pass
else:
......@@ -3404,21 +3405,24 @@ order (MRO) for bases """
pass
# Now it should work
x = C()
y = pickle.loads(pickle.dumps(x))
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
y = pickle.loads(pickle.dumps(x, proto))
self.assertNotHasAttr(y, 'a')
y = cPickle.loads(cPickle.dumps(x))
y = cPickle.loads(cPickle.dumps(x, proto))
self.assertNotHasAttr(y, 'a')
x.a = 42
y = pickle.loads(pickle.dumps(x))
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
y = pickle.loads(pickle.dumps(x, proto))
self.assertEqual(y.a, 42)
y = cPickle.loads(cPickle.dumps(x))
y = cPickle.loads(cPickle.dumps(x, proto))
self.assertEqual(y.a, 42)
x = D()
x.a = 42
x.b = 100
y = pickle.loads(pickle.dumps(x))
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
y = pickle.loads(pickle.dumps(x, proto))
self.assertEqual(y.a + y.b, 142)
y = cPickle.loads(cPickle.dumps(x))
y = cPickle.loads(cPickle.dumps(x, proto))
self.assertEqual(y.a + y.b, 142)
# A subclass that adds a slot should also work
class E(C):
......@@ -3426,10 +3430,11 @@ order (MRO) for bases """
x = E()
x.a = 42
x.b = "foo"
y = pickle.loads(pickle.dumps(x))
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
y = pickle.loads(pickle.dumps(x, proto))
self.assertEqual(y.a, x.a)
self.assertEqual(y.b, x.b)
y = cPickle.loads(cPickle.dumps(x))
y = cPickle.loads(cPickle.dumps(x, proto))
self.assertEqual(y.a, x.a)
self.assertEqual(y.b, x.b)
......
......@@ -146,7 +146,8 @@ class TestPartial(unittest.TestCase):
def test_pickle(self):
f = self.thetype(signature, 'asdf', bar=True)
f.add_something_to__dict__ = True
f_copy = pickle.loads(pickle.dumps(f))
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
f_copy = pickle.loads(pickle.dumps(f, proto))
self.assertEqual(signature(f), signature(f_copy))
# Issue 6083: Reference counting bug
......
......@@ -358,7 +358,8 @@ class TestBasicOps(unittest.TestCase):
c = count(value)
self.assertEqual(next(copy.copy(c)), value)
self.assertEqual(next(copy.deepcopy(c)), value)
self.assertEqual(next(pickle.loads(pickle.dumps(c))), value)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
self.assertEqual(next(pickle.loads(pickle.dumps(c, proto))), value)
def test_count_with_stride(self):
self.assertEqual(zip('abc',count(2,3)), [('a', 2), ('b', 5), ('c', 8)])
......
......@@ -376,13 +376,14 @@ class MemoryTestMixin:
# the module-level.
import __main__
PickleTestMemIO.__module__ = '__main__'
PickleTestMemIO.__qualname__ = PickleTestMemIO.__name__
__main__.PickleTestMemIO = PickleTestMemIO
submemio = PickleTestMemIO(buf, 80)
submemio.seek(2)
# We only support pickle protocol 2 and onward since we use extended
# __reduce__ API of PEP 307 to provide pickling support.
for proto in range(2, pickle.HIGHEST_PROTOCOL):
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
for obj in (memio, submemio):
obj2 = pickle.loads(pickle.dumps(obj, protocol=proto))
self.assertEqual(obj.getvalue(), obj2.getvalue())
......
......@@ -1385,7 +1385,8 @@ class MinidomTest(unittest.TestCase):
" <!ENTITY ent SYSTEM 'http://xml.python.org/entity'>\n"
"]><doc attr='value'> text\n"
"<?pi sample?> <!-- comment --> <e/> </doc>")
s = pickle.dumps(doc)
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
s = pickle.dumps(doc, proto)
doc2 = pickle.loads(s)
stack = [(doc, doc2)]
while stack:
......
......@@ -140,7 +140,8 @@ class TestBasicOps(unittest.TestCase):
self.assertEqual(y1, y2)
def test_pickling(self):
state = pickle.dumps(self.gen)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
state = pickle.dumps(self.gen, proto)
origseq = [self.gen.random() for i in xrange(10)]
newgen = pickle.loads(state)
restoredseq = [newgen.random() for i in xrange(10)]
......@@ -226,7 +227,8 @@ class SystemRandom_TestBasicOps(TestBasicOps):
self.assertEqual(self.gen.gauss_next, None)
def test_pickling(self):
self.assertRaises(NotImplementedError, pickle.dumps, self.gen)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
self.assertRaises(NotImplementedError, pickle.dumps, self.gen, proto)
def test_53_bits_per_float(self):
# This should pass whenever a C double has 53 bit precision.
......
......@@ -233,7 +233,7 @@ class TestJointOps(unittest.TestCase):
self.assertEqual(self.s, dup, "%s != %s" % (self.s, dup))
if type(self.s) not in (set, frozenset):
self.s.x = 10
p = pickle.dumps(self.s)
p = pickle.dumps(self.s, i)
dup = pickle.loads(p)
self.assertEqual(self.s.x, dup.x)
......@@ -786,7 +786,8 @@ class TestBasicOps(unittest.TestCase):
self.assertEqual(setiter.__length_hint__(), len(self.set))
def test_pickling(self):
p = pickle.dumps(self.set)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
p = pickle.dumps(self.set, proto)
copy = pickle.loads(p)
self.assertEqual(self.set, copy,
"%s != %s" % (self.set, copy))
......
......@@ -75,7 +75,8 @@ class TestBasicOps(unittest.TestCase):
self.assertIn(v, self.values)
def test_pickling(self):
p = pickle.dumps(self.set)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
p = pickle.dumps(self.set, proto)
copy = pickle.loads(p)
self.assertEqual(self.set, copy,
"%s != %s" % (self.set, copy))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment