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,8 +61,9 @@ class AudioTests: ...@@ -61,8 +61,9 @@ 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):
self.assertEqual(pickle.loads(dump), params) dump = pickle.dumps(params, proto)
self.assertEqual(pickle.loads(dump), params)
class AudioWriteTests(AudioTests): class AudioWriteTests(AudioTests):
......
...@@ -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,88 +614,96 @@ class ExceptionPicklingTestCase(unittest.TestCase): ...@@ -614,88 +614,96 @@ 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):
e2 = pickle.loads(pickled) pickled = pickle.dumps(e1, proto)
self.assertEqual(e1.message, e2.message) e2 = pickle.loads(pickled)
self.assertEqual(repr(e1), repr(e2)) self.assertEqual(e1.message, e2.message)
self.assertEqual(repr(e1), repr(e2))
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):
e2 = pickle.loads(pickled) pickled = pickle.dumps(e1, proto)
self.assertEqual(e1.message, e2.message) e2 = pickle.loads(pickled)
self.assertEqual(e1.args, e2.args) self.assertEqual(e1.message, e2.message)
self.assertEqual(e1.section, e2.section) self.assertEqual(e1.args, e2.args)
self.assertEqual(repr(e1), repr(e2)) self.assertEqual(e1.section, e2.section)
self.assertEqual(repr(e1), repr(e2))
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):
e2 = pickle.loads(pickled) pickled = pickle.dumps(e1, proto)
self.assertEqual(e1.message, e2.message) e2 = pickle.loads(pickled)
self.assertEqual(e1.args, e2.args) self.assertEqual(e1.message, e2.message)
self.assertEqual(e1.section, e2.section) self.assertEqual(e1.args, e2.args)
self.assertEqual(e1.option, e2.option) self.assertEqual(e1.section, e2.section)
self.assertEqual(repr(e1), repr(e2)) self.assertEqual(e1.option, e2.option)
self.assertEqual(repr(e1), repr(e2))
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):
e2 = pickle.loads(pickled) pickled = pickle.dumps(e1, proto)
self.assertEqual(e1.message, e2.message) e2 = pickle.loads(pickled)
self.assertEqual(e1.args, e2.args) self.assertEqual(e1.message, e2.message)
self.assertEqual(e1.section, e2.section) self.assertEqual(e1.args, e2.args)
self.assertEqual(repr(e1), repr(e2)) self.assertEqual(e1.section, e2.section)
self.assertEqual(repr(e1), repr(e2))
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):
e2 = pickle.loads(pickled) pickled = pickle.dumps(e1, proto)
self.assertEqual(e1.message, e2.message) e2 = pickle.loads(pickled)
self.assertEqual(e1.args, e2.args) self.assertEqual(e1.message, e2.message)
self.assertEqual(e1.section, e2.section) self.assertEqual(e1.args, e2.args)
self.assertEqual(e1.option, e2.option) self.assertEqual(e1.section, e2.section)
self.assertEqual(repr(e1), repr(e2)) self.assertEqual(e1.option, e2.option)
self.assertEqual(repr(e1), repr(e2))
def test_interpolationmissingoptionerror(self): def test_interpolationmissingoptionerror(self):
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):
e2 = pickle.loads(pickled) pickled = pickle.dumps(e1, proto)
self.assertEqual(e1.message, e2.message) e2 = pickle.loads(pickled)
self.assertEqual(e1.args, e2.args) self.assertEqual(e1.message, e2.message)
self.assertEqual(e1.section, e2.section) self.assertEqual(e1.args, e2.args)
self.assertEqual(e1.option, e2.option) self.assertEqual(e1.section, e2.section)
self.assertEqual(e1.reference, e2.reference) self.assertEqual(e1.option, e2.option)
self.assertEqual(repr(e1), repr(e2)) self.assertEqual(e1.reference, e2.reference)
self.assertEqual(repr(e1), repr(e2))
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):
e2 = pickle.loads(pickled) pickled = pickle.dumps(e1, proto)
self.assertEqual(e1.message, e2.message) e2 = pickle.loads(pickled)
self.assertEqual(e1.args, e2.args) self.assertEqual(e1.message, e2.message)
self.assertEqual(e1.section, e2.section) self.assertEqual(e1.args, e2.args)
self.assertEqual(e1.option, e2.option) self.assertEqual(e1.section, e2.section)
self.assertEqual(repr(e1), repr(e2)) self.assertEqual(e1.option, e2.option)
self.assertEqual(repr(e1), repr(e2))
def test_interpolationdeptherror(self): def test_interpolationdeptherror(self):
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):
e2 = pickle.loads(pickled) pickled = pickle.dumps(e1, proto)
self.assertEqual(e1.message, e2.message) e2 = pickle.loads(pickled)
self.assertEqual(e1.args, e2.args) self.assertEqual(e1.message, e2.message)
self.assertEqual(e1.section, e2.section) self.assertEqual(e1.args, e2.args)
self.assertEqual(e1.option, e2.option) self.assertEqual(e1.section, e2.section)
self.assertEqual(repr(e1), repr(e2)) self.assertEqual(e1.option, e2.option)
self.assertEqual(repr(e1), repr(e2))
def test_parsingerror(self): def test_parsingerror(self):
import pickle import pickle
...@@ -703,25 +711,27 @@ class ExceptionPicklingTestCase(unittest.TestCase): ...@@ -703,25 +711,27 @@ 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):
e2 = pickle.loads(pickled) pickled = pickle.dumps(e1, proto)
self.assertEqual(e1.message, e2.message) e2 = pickle.loads(pickled)
self.assertEqual(e1.args, e2.args) self.assertEqual(e1.message, e2.message)
self.assertEqual(e1.filename, e2.filename) self.assertEqual(e1.args, e2.args)
self.assertEqual(e1.errors, e2.errors) self.assertEqual(e1.filename, e2.filename)
self.assertEqual(repr(e1), repr(e2)) self.assertEqual(e1.errors, e2.errors)
self.assertEqual(repr(e1), repr(e2))
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):
e2 = pickle.loads(pickled) pickled = pickle.dumps(e1, proto)
self.assertEqual(e1.message, e2.message) e2 = pickle.loads(pickled)
self.assertEqual(e1.args, e2.args) self.assertEqual(e1.message, e2.message)
self.assertEqual(e1.line, e2.line) self.assertEqual(e1.args, e2.args)
self.assertEqual(e1.filename, e2.filename) self.assertEqual(e1.line, e2.line)
self.assertEqual(e1.lineno, e2.lineno) self.assertEqual(e1.filename, e2.filename)
self.assertEqual(repr(e1), repr(e2)) self.assertEqual(e1.lineno, e2.lineno)
self.assertEqual(repr(e1), repr(e2))
def test_main(): def test_main():
......
...@@ -1421,11 +1421,12 @@ class TestDateTime(TestDate): ...@@ -1421,11 +1421,12 @@ 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):
b = pickle.loads(s) s = pickle.dumps(a, proto)
self.assertEqual(b.year, 2003) b = pickle.loads(s)
self.assertEqual(b.month, 2) self.assertEqual(b.year, 2003)
self.assertEqual(b.day, 7) self.assertEqual(b.month, 2)
self.assertEqual(b.day, 7)
def test_pickling_subclass_datetime(self): def test_pickling_subclass_datetime(self):
args = 6, 7, 23, 20, 59, 1, 64**2 args = 6, 7, 23, 20, 59, 1, 64**2
......
...@@ -1664,9 +1664,10 @@ class DecimalPythonAPItests(unittest.TestCase): ...@@ -1664,9 +1664,10 @@ 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):
e = pickle.loads(p) p = pickle.dumps(d, proto)
self.assertEqual(d, e) e = pickle.loads(p)
self.assertEqual(d, e)
def test_int(self): def test_int(self):
for x in range(-250, 250): for x in range(-250, 250):
...@@ -1750,12 +1751,13 @@ class DecimalPythonAPItests(unittest.TestCase): ...@@ -1750,12 +1751,13 @@ class DecimalPythonAPItests(unittest.TestCase):
class ContextAPItests(unittest.TestCase): class ContextAPItests(unittest.TestCase):
def test_pickle(self): def test_pickle(self):
c = Context() for proto in range(pickle.HIGHEST_PROTOCOL + 1):
e = pickle.loads(pickle.dumps(c)) c = Context()
for k in vars(c): e = pickle.loads(pickle.dumps(c, proto))
v1 = vars(c)[k] for k in vars(c):
v2 = vars(e)[k] v1 = vars(c)[k]
self.assertEqual(v1, v2) v2 = vars(e)[k]
self.assertEqual(v1, v2)
def test_equality_with_other_types(self): def test_equality_with_other_types(self):
self.assertIn(Decimal(10), ['a', 1.0, Decimal(10), (1,2), {}]) self.assertIn(Decimal(10), ['a', 1.0, Decimal(10), (1,2), {}])
......
...@@ -600,11 +600,12 @@ class TestSubclass(unittest.TestCase): ...@@ -600,11 +600,12 @@ 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):
e = pickle.loads(s) s = pickle.dumps(d, proto)
self.assertNotEqual(id(d), id(e)) e = pickle.loads(s)
self.assertEqual(type(d), type(e)) self.assertNotEqual(id(d), id(e))
self.assertEqual(list(d), list(e)) self.assertEqual(type(d), type(e))
self.assertEqual(list(d), list(e))
d = Deque('abcde', maxlen=4) d = Deque('abcde', maxlen=4)
...@@ -616,11 +617,12 @@ class TestSubclass(unittest.TestCase): ...@@ -616,11 +617,12 @@ 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):
e = pickle.loads(s) s = pickle.dumps(d, proto)
self.assertNotEqual(id(d), id(e)) e = pickle.loads(s)
self.assertEqual(type(d), type(e)) self.assertNotEqual(id(d), id(e))
self.assertEqual(list(d), list(e)) self.assertEqual(type(d), type(e))
self.assertEqual(list(d), list(e))
## def test_pickle(self): ## def test_pickle(self):
## d = Deque('abc') ## d = Deque('abc')
......
...@@ -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,30 +3358,31 @@ order (MRO) for bases """ ...@@ -3358,30 +3358,31 @@ order (MRO) for bases """
__slots__ = ['a'] __slots__ = ['a']
class D(C): class D(C):
pass pass
try: for proto in range(2):
pickle.dumps(C()) try:
except TypeError: pickle.dumps(C(), proto)
pass except TypeError:
else: pass
self.fail("should fail: pickle C instance - %s" % base) else:
try: self.fail("should fail: pickle C instance - %s" % base)
cPickle.dumps(C()) try:
except TypeError: cPickle.dumps(C(), proto)
pass except TypeError:
else: pass
self.fail("should fail: cPickle C instance - %s" % base) else:
try: self.fail("should fail: cPickle C instance - %s" % base)
pickle.dumps(C()) try:
except TypeError: pickle.dumps(C(), proto)
pass except TypeError:
else: pass
self.fail("should fail: pickle D instance - %s" % base) else:
try: self.fail("should fail: pickle D instance - %s" % base)
cPickle.dumps(D()) try:
except TypeError: cPickle.dumps(D(), proto)
pass except TypeError:
else: pass
self.fail("should fail: cPickle D instance - %s" % base) else:
self.fail("should fail: cPickle D instance - %s" % base)
# Give C a nice generic __getstate__ and __setstate__ # Give C a nice generic __getstate__ and __setstate__
class C(base): class C(base):
__slots__ = ['a'] __slots__ = ['a']
...@@ -3404,34 +3405,38 @@ order (MRO) for bases """ ...@@ -3404,34 +3405,38 @@ 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):
self.assertNotHasAttr(y, 'a') y = pickle.loads(pickle.dumps(x, proto))
y = cPickle.loads(cPickle.dumps(x)) self.assertNotHasAttr(y, 'a')
self.assertNotHasAttr(y, 'a') y = cPickle.loads(cPickle.dumps(x, proto))
self.assertNotHasAttr(y, 'a')
x.a = 42 x.a = 42
y = pickle.loads(pickle.dumps(x)) for proto in range(pickle.HIGHEST_PROTOCOL + 1):
self.assertEqual(y.a, 42) y = pickle.loads(pickle.dumps(x, proto))
y = cPickle.loads(cPickle.dumps(x)) self.assertEqual(y.a, 42)
self.assertEqual(y.a, 42) y = cPickle.loads(cPickle.dumps(x, proto))
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):
self.assertEqual(y.a + y.b, 142) y = pickle.loads(pickle.dumps(x, proto))
y = cPickle.loads(cPickle.dumps(x)) self.assertEqual(y.a + y.b, 142)
self.assertEqual(y.a + y.b, 142) y = cPickle.loads(cPickle.dumps(x, proto))
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):
__slots__ = ['b'] __slots__ = ['b']
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):
self.assertEqual(y.a, x.a) y = pickle.loads(pickle.dumps(x, proto))
self.assertEqual(y.b, x.b) self.assertEqual(y.a, x.a)
y = cPickle.loads(cPickle.dumps(x)) self.assertEqual(y.b, x.b)
self.assertEqual(y.a, x.a) y = cPickle.loads(cPickle.dumps(x, proto))
self.assertEqual(y.b, x.b) self.assertEqual(y.a, x.a)
self.assertEqual(y.b, x.b)
def test_binary_operator_override(self): def test_binary_operator_override(self):
# Testing overrides of binary operations... # Testing overrides of binary operations...
......
...@@ -146,8 +146,9 @@ class TestPartial(unittest.TestCase): ...@@ -146,8 +146,9 @@ 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):
self.assertEqual(signature(f), signature(f_copy)) f_copy = pickle.loads(pickle.dumps(f, proto))
self.assertEqual(signature(f), signature(f_copy))
# Issue 6083: Reference counting bug # Issue 6083: Reference counting bug
def test_setstate_refcount(self): def test_setstate_refcount(self):
......
...@@ -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,43 +1385,44 @@ class MinidomTest(unittest.TestCase): ...@@ -1385,43 +1385,44 @@ 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):
doc2 = pickle.loads(s) s = pickle.dumps(doc, proto)
stack = [(doc, doc2)] doc2 = pickle.loads(s)
while stack: stack = [(doc, doc2)]
n1, n2 = stack.pop() while stack:
self.confirm(n1.nodeType == n2.nodeType n1, n2 = stack.pop()
and len(n1.childNodes) == len(n2.childNodes) self.confirm(n1.nodeType == n2.nodeType
and n1.nodeName == n2.nodeName and len(n1.childNodes) == len(n2.childNodes)
and not n1.isSameNode(n2) and n1.nodeName == n2.nodeName
and not n2.isSameNode(n1)) and not n1.isSameNode(n2)
if n1.nodeType == Node.DOCUMENT_TYPE_NODE: and not n2.isSameNode(n1))
len(n1.entities) if n1.nodeType == Node.DOCUMENT_TYPE_NODE:
len(n2.entities) len(n1.entities)
len(n1.notations) len(n2.entities)
len(n2.notations) len(n1.notations)
self.confirm(len(n1.entities) == len(n2.entities) len(n2.notations)
and len(n1.notations) == len(n2.notations)) self.confirm(len(n1.entities) == len(n2.entities)
for i in range(len(n1.notations)): and len(n1.notations) == len(n2.notations))
# XXX this loop body doesn't seem to be executed? for i in range(len(n1.notations)):
no1 = n1.notations.item(i) # XXX this loop body doesn't seem to be executed?
no2 = n1.notations.item(i) no1 = n1.notations.item(i)
self.confirm(no1.name == no2.name no2 = n1.notations.item(i)
and no1.publicId == no2.publicId self.confirm(no1.name == no2.name
and no1.systemId == no2.systemId) and no1.publicId == no2.publicId
stack.append((no1, no2)) and no1.systemId == no2.systemId)
for i in range(len(n1.entities)): stack.append((no1, no2))
e1 = n1.entities.item(i) for i in range(len(n1.entities)):
e2 = n2.entities.item(i) e1 = n1.entities.item(i)
self.confirm(e1.notationName == e2.notationName e2 = n2.entities.item(i)
and e1.publicId == e2.publicId self.confirm(e1.notationName == e2.notationName
and e1.systemId == e2.systemId) and e1.publicId == e2.publicId
stack.append((e1, e2)) and e1.systemId == e2.systemId)
if n1.nodeType != Node.DOCUMENT_NODE: stack.append((e1, e2))
self.confirm(n1.ownerDocument.isSameNode(doc) if n1.nodeType != Node.DOCUMENT_NODE:
and n2.ownerDocument.isSameNode(doc2)) self.confirm(n1.ownerDocument.isSameNode(doc)
for i in range(len(n1.childNodes)): and n2.ownerDocument.isSameNode(doc2))
stack.append((n1.childNodes[i], n2.childNodes[i])) for i in range(len(n1.childNodes)):
stack.append((n1.childNodes[i], n2.childNodes[i]))
def testSerializeCommentNodeWithDoubleHyphen(self): def testSerializeCommentNodeWithDoubleHyphen(self):
doc = create_doc_without_doctype() doc = create_doc_without_doctype()
......
...@@ -140,11 +140,12 @@ class TestBasicOps(unittest.TestCase): ...@@ -140,11 +140,12 @@ 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):
origseq = [self.gen.random() for i in xrange(10)] state = pickle.dumps(self.gen, proto)
newgen = pickle.loads(state) origseq = [self.gen.random() for i in xrange(10)]
restoredseq = [newgen.random() for i in xrange(10)] newgen = pickle.loads(state)
self.assertEqual(origseq, restoredseq) restoredseq = [newgen.random() for i in xrange(10)]
self.assertEqual(origseq, restoredseq)
def test_bug_1727780(self): def test_bug_1727780(self):
# verify that version-2-pickles can be loaded # verify that version-2-pickles can be loaded
...@@ -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,10 +786,11 @@ class TestBasicOps(unittest.TestCase): ...@@ -786,10 +786,11 @@ 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):
copy = pickle.loads(p) p = pickle.dumps(self.set, proto)
self.assertEqual(self.set, copy, copy = pickle.loads(p)
"%s != %s" % (self.set, copy)) self.assertEqual(self.set, copy,
"%s != %s" % (self.set, copy))
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
......
...@@ -75,10 +75,11 @@ class TestBasicOps(unittest.TestCase): ...@@ -75,10 +75,11 @@ 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):
copy = pickle.loads(p) p = pickle.dumps(self.set, proto)
self.assertEqual(self.set, copy, copy = pickle.loads(p)
"%s != %s" % (self.set, copy)) 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