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