mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
Issue #22777: Test pickling with all protocols.
This commit is contained in:
parent
79b81738ef
commit
bad1257c96
30 changed files with 701 additions and 621 deletions
|
@ -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):
|
||||||
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(b"x"))
|
self.dumps(c_char(b"x"))
|
||||||
# Issue 5049
|
# Issue 5049
|
||||||
pickle.dumps(c_wchar("x"))
|
self.dumps(c_wchar("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()
|
||||||
|
|
|
@ -45,7 +45,8 @@ class AudioTests:
|
||||||
self.assertEqual(params.comptype, comptype)
|
self.assertEqual(params.comptype, comptype)
|
||||||
self.assertEqual(params.compname, compname)
|
self.assertEqual(params.compname, 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)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1682,7 +1682,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)
|
||||||
|
|
|
@ -392,6 +392,7 @@ class CommonTest(unittest.TestCase):
|
||||||
|
|
||||||
def test_pickle(self):
|
def test_pickle(self):
|
||||||
lst = self.type2test([4, 5, 6, 7])
|
lst = self.type2test([4, 5, 6, 7])
|
||||||
lst2 = pickle.loads(pickle.dumps(lst))
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
lst2 = pickle.loads(pickle.dumps(lst, proto))
|
||||||
self.assertEqual(lst2, lst)
|
self.assertEqual(lst2, lst)
|
||||||
self.assertNotEqual(id(lst2), id(lst))
|
self.assertNotEqual(id(lst2), id(lst))
|
||||||
|
|
|
@ -285,8 +285,9 @@ class BaseTest:
|
||||||
|
|
||||||
def test_iterator_pickle(self):
|
def test_iterator_pickle(self):
|
||||||
data = array.array(self.typecode, self.example)
|
data = array.array(self.typecode, self.example)
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
orgit = iter(data)
|
orgit = iter(data)
|
||||||
d = pickle.dumps(orgit)
|
d = pickle.dumps(orgit, proto)
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
self.assertEqual(type(orgit), type(it))
|
self.assertEqual(type(orgit), type(it))
|
||||||
self.assertEqual(list(it), list(data))
|
self.assertEqual(list(it), list(data))
|
||||||
|
@ -294,7 +295,7 @@ class BaseTest:
|
||||||
if len(data):
|
if len(data):
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
next(it)
|
next(it)
|
||||||
d = pickle.dumps(it)
|
d = pickle.dumps(it, proto)
|
||||||
self.assertEqual(list(it), list(data)[1:])
|
self.assertEqual(list(it), list(data)[1:])
|
||||||
|
|
||||||
def test_insert(self):
|
def test_insert(self):
|
||||||
|
|
|
@ -269,10 +269,9 @@ 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_picklevalues(self):
|
def test_picklevalues(self):
|
||||||
# Test for specific backwards-compatible pickle values
|
# Test for specific backwards-compatible pickle values
|
||||||
|
|
|
@ -121,9 +121,9 @@ def map_char(arg):
|
||||||
|
|
||||||
class BuiltinTest(unittest.TestCase):
|
class BuiltinTest(unittest.TestCase):
|
||||||
# Helper to check picklability
|
# Helper to check picklability
|
||||||
def check_iter_pickle(self, it, seq):
|
def check_iter_pickle(self, it, seq, proto):
|
||||||
itorg = it
|
itorg = it
|
||||||
d = pickle.dumps(it)
|
d = pickle.dumps(it, proto)
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
self.assertEqual(type(itorg), type(it))
|
self.assertEqual(type(itorg), type(it))
|
||||||
self.assertEqual(list(it), seq)
|
self.assertEqual(list(it), seq)
|
||||||
|
@ -134,7 +134,7 @@ class BuiltinTest(unittest.TestCase):
|
||||||
next(it)
|
next(it)
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
return
|
return
|
||||||
d = pickle.dumps(it)
|
d = pickle.dumps(it, proto)
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
self.assertEqual(list(it), seq[1:])
|
self.assertEqual(list(it), seq[1:])
|
||||||
|
|
||||||
|
@ -636,9 +636,10 @@ class BuiltinTest(unittest.TestCase):
|
||||||
self.assertRaises(TypeError, list, filter(42, (1, 2)))
|
self.assertRaises(TypeError, list, filter(42, (1, 2)))
|
||||||
|
|
||||||
def test_filter_pickle(self):
|
def test_filter_pickle(self):
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
f1 = filter(filter_char, "abcdeabcde")
|
f1 = filter(filter_char, "abcdeabcde")
|
||||||
f2 = filter(filter_char, "abcdeabcde")
|
f2 = filter(filter_char, "abcdeabcde")
|
||||||
self.check_iter_pickle(f1, list(f2))
|
self.check_iter_pickle(f1, list(f2), proto)
|
||||||
|
|
||||||
def test_getattr(self):
|
def test_getattr(self):
|
||||||
self.assertTrue(getattr(sys, 'stdout') is sys.stdout)
|
self.assertTrue(getattr(sys, 'stdout') is sys.stdout)
|
||||||
|
@ -834,9 +835,10 @@ class BuiltinTest(unittest.TestCase):
|
||||||
self.assertRaises(RuntimeError, list, map(badfunc, range(5)))
|
self.assertRaises(RuntimeError, list, map(badfunc, range(5)))
|
||||||
|
|
||||||
def test_map_pickle(self):
|
def test_map_pickle(self):
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
m1 = map(map_char, "Is this the real life?")
|
m1 = map(map_char, "Is this the real life?")
|
||||||
m2 = map(map_char, "Is this the real life?")
|
m2 = map(map_char, "Is this the real life?")
|
||||||
self.check_iter_pickle(m1, list(m2))
|
self.check_iter_pickle(m1, list(m2), proto)
|
||||||
|
|
||||||
def test_max(self):
|
def test_max(self):
|
||||||
self.assertEqual(max('123123'), '3')
|
self.assertEqual(max('123123'), '3')
|
||||||
|
@ -1433,8 +1435,9 @@ class BuiltinTest(unittest.TestCase):
|
||||||
a = (1, 2, 3)
|
a = (1, 2, 3)
|
||||||
b = (4, 5, 6)
|
b = (4, 5, 6)
|
||||||
t = [(1, 4), (2, 5), (3, 6)]
|
t = [(1, 4), (2, 5), (3, 6)]
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
z1 = zip(a, b)
|
z1 = zip(a, b)
|
||||||
self.check_iter_pickle(z1, t)
|
self.check_iter_pickle(z1, t, proto)
|
||||||
|
|
||||||
def test_format(self):
|
def test_format(self):
|
||||||
# Test the basic machinery of the format() builtin. Don't test
|
# Test the basic machinery of the format() builtin. Don't test
|
||||||
|
|
|
@ -555,10 +555,11 @@ class BaseBytesTest:
|
||||||
self.assertEqual(b, q)
|
self.assertEqual(b, q)
|
||||||
|
|
||||||
def test_iterator_pickling(self):
|
def test_iterator_pickling(self):
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
for b in b"", b"a", b"abc", b"\xffab\x80", b"\0\0\377\0\0":
|
for b in b"", b"a", b"abc", b"\xffab\x80", b"\0\0\377\0\0":
|
||||||
it = itorg = iter(self.type2test(b))
|
it = itorg = iter(self.type2test(b))
|
||||||
data = list(self.type2test(b))
|
data = list(self.type2test(b))
|
||||||
d = pickle.dumps(it)
|
d = pickle.dumps(it, proto)
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
self.assertEqual(type(itorg), type(it))
|
self.assertEqual(type(itorg), type(it))
|
||||||
self.assertEqual(list(it), data)
|
self.assertEqual(list(it), data)
|
||||||
|
@ -568,7 +569,7 @@ class BaseBytesTest:
|
||||||
next(it)
|
next(it)
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
continue
|
continue
|
||||||
d = pickle.dumps(it)
|
d = pickle.dumps(it, proto)
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
self.assertEqual(list(it), data[1:])
|
self.assertEqual(list(it), data[1:])
|
||||||
|
|
||||||
|
|
|
@ -646,8 +646,9 @@ class BZ2CompressorTest(BaseTest):
|
||||||
data = None
|
data = None
|
||||||
|
|
||||||
def testPickle(self):
|
def testPickle(self):
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
pickle.dumps(BZ2Compressor())
|
pickle.dumps(BZ2Compressor(), proto)
|
||||||
|
|
||||||
|
|
||||||
class BZ2DecompressorTest(BaseTest):
|
class BZ2DecompressorTest(BaseTest):
|
||||||
|
@ -702,8 +703,9 @@ class BZ2DecompressorTest(BaseTest):
|
||||||
decompressed = None
|
decompressed = None
|
||||||
|
|
||||||
def testPickle(self):
|
def testPickle(self):
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
pickle.dumps(BZ2Decompressor())
|
pickle.dumps(BZ2Decompressor(), proto)
|
||||||
|
|
||||||
|
|
||||||
class CompressDecompressTest(BaseTest):
|
class CompressDecompressTest(BaseTest):
|
||||||
|
|
|
@ -63,10 +63,17 @@ class TestChainMap(unittest.TestCase):
|
||||||
for m1, m2 in zip(d.maps[1:], e.maps[1:]):
|
for m1, m2 in zip(d.maps[1:], e.maps[1:]):
|
||||||
self.assertIs(m1, m2)
|
self.assertIs(m1, m2)
|
||||||
|
|
||||||
for e in [pickle.loads(pickle.dumps(d)),
|
# check deep copies
|
||||||
copy.deepcopy(d),
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
e = pickle.loads(pickle.dumps(d, proto))
|
||||||
|
self.assertEqual(d, e)
|
||||||
|
self.assertEqual(d.maps, e.maps)
|
||||||
|
self.assertIsNot(d, e)
|
||||||
|
for m1, m2 in zip(d.maps, e.maps):
|
||||||
|
self.assertIsNot(m1, m2, e)
|
||||||
|
for e in [copy.deepcopy(d),
|
||||||
eval(repr(d))
|
eval(repr(d))
|
||||||
]: # check deep copies
|
]:
|
||||||
self.assertEqual(d, e)
|
self.assertEqual(d, e)
|
||||||
self.assertEqual(d.maps, e.maps)
|
self.assertEqual(d.maps, e.maps)
|
||||||
self.assertIsNot(d, e)
|
self.assertIsNot(d, e)
|
||||||
|
@ -1110,28 +1117,21 @@ class TestCounter(unittest.TestCase):
|
||||||
# Check that counters are copyable, deepcopyable, picklable, and
|
# Check that counters are copyable, deepcopyable, picklable, and
|
||||||
#have a repr/eval round-trip
|
#have a repr/eval round-trip
|
||||||
words = Counter('which witch had which witches wrist watch'.split())
|
words = Counter('which witch had which witches wrist watch'.split())
|
||||||
update_test = Counter()
|
def check(dup):
|
||||||
update_test.update(words)
|
|
||||||
for label, dup in [
|
|
||||||
('words.copy()', words.copy()),
|
|
||||||
('copy.copy(words)', copy.copy(words)),
|
|
||||||
('copy.deepcopy(words)', copy.deepcopy(words)),
|
|
||||||
('pickle.loads(pickle.dumps(words, 0))',
|
|
||||||
pickle.loads(pickle.dumps(words, 0))),
|
|
||||||
('pickle.loads(pickle.dumps(words, 1))',
|
|
||||||
pickle.loads(pickle.dumps(words, 1))),
|
|
||||||
('pickle.loads(pickle.dumps(words, 2))',
|
|
||||||
pickle.loads(pickle.dumps(words, 2))),
|
|
||||||
('pickle.loads(pickle.dumps(words, -1))',
|
|
||||||
pickle.loads(pickle.dumps(words, -1))),
|
|
||||||
('eval(repr(words))', eval(repr(words))),
|
|
||||||
('update_test', update_test),
|
|
||||||
('Counter(words)', Counter(words)),
|
|
||||||
]:
|
|
||||||
with self.subTest(label=label):
|
|
||||||
msg = "\ncopy: %s\nwords: %s" % (dup, words)
|
msg = "\ncopy: %s\nwords: %s" % (dup, words)
|
||||||
self.assertIsNot(dup, words, msg)
|
self.assertIsNot(dup, words, msg)
|
||||||
self.assertEqual(dup, words)
|
self.assertEqual(dup, words)
|
||||||
|
check(words.copy())
|
||||||
|
check(copy.copy(words))
|
||||||
|
check(copy.deepcopy(words))
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
with self.subTest(proto=proto):
|
||||||
|
check(pickle.loads(pickle.dumps(words, proto)))
|
||||||
|
check(eval(repr(words)))
|
||||||
|
update_test = Counter()
|
||||||
|
update_test.update(words)
|
||||||
|
check(update_test)
|
||||||
|
check(Counter(words))
|
||||||
|
|
||||||
def test_copy_subclass(self):
|
def test_copy_subclass(self):
|
||||||
class MyCounter(Counter):
|
class MyCounter(Counter):
|
||||||
|
@ -1433,30 +1433,21 @@ class TestOrderedDict(unittest.TestCase):
|
||||||
# and have a repr/eval round-trip
|
# and have a repr/eval round-trip
|
||||||
pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
|
pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
|
||||||
od = OrderedDict(pairs)
|
od = OrderedDict(pairs)
|
||||||
update_test = OrderedDict()
|
def check(dup):
|
||||||
update_test.update(od)
|
|
||||||
for label, dup in [
|
|
||||||
('od.copy()', od.copy()),
|
|
||||||
('copy.copy(od)', copy.copy(od)),
|
|
||||||
('copy.deepcopy(od)', copy.deepcopy(od)),
|
|
||||||
('pickle.loads(pickle.dumps(od, 0))',
|
|
||||||
pickle.loads(pickle.dumps(od, 0))),
|
|
||||||
('pickle.loads(pickle.dumps(od, 1))',
|
|
||||||
pickle.loads(pickle.dumps(od, 1))),
|
|
||||||
('pickle.loads(pickle.dumps(od, 2))',
|
|
||||||
pickle.loads(pickle.dumps(od, 2))),
|
|
||||||
('pickle.loads(pickle.dumps(od, 3))',
|
|
||||||
pickle.loads(pickle.dumps(od, 3))),
|
|
||||||
('pickle.loads(pickle.dumps(od, -1))',
|
|
||||||
pickle.loads(pickle.dumps(od, -1))),
|
|
||||||
('eval(repr(od))', eval(repr(od))),
|
|
||||||
('update_test', update_test),
|
|
||||||
('OrderedDict(od)', OrderedDict(od)),
|
|
||||||
]:
|
|
||||||
with self.subTest(label=label):
|
|
||||||
msg = "\ncopy: %s\nod: %s" % (dup, od)
|
msg = "\ncopy: %s\nod: %s" % (dup, od)
|
||||||
self.assertIsNot(dup, od, msg)
|
self.assertIsNot(dup, od, msg)
|
||||||
self.assertEqual(dup, od)
|
self.assertEqual(dup, od)
|
||||||
|
check(od.copy())
|
||||||
|
check(copy.copy(od))
|
||||||
|
check(copy.deepcopy(od))
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
with self.subTest(proto=proto):
|
||||||
|
check(pickle.loads(pickle.dumps(od, proto)))
|
||||||
|
check(eval(repr(od)))
|
||||||
|
update_test = OrderedDict()
|
||||||
|
update_test.update(od)
|
||||||
|
check(update_test)
|
||||||
|
check(OrderedDict(od))
|
||||||
|
|
||||||
def test_yaml_linkage(self):
|
def test_yaml_linkage(self):
|
||||||
# Verify that __reduce__ is setup in a way that supports PyYAML's dump() feature.
|
# Verify that __reduce__ is setup in a way that supports PyYAML's dump() feature.
|
||||||
|
|
|
@ -1591,7 +1591,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))
|
||||||
|
@ -1599,7 +1600,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)
|
||||||
|
@ -1609,7 +1611,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)
|
||||||
|
@ -1620,7 +1623,8 @@ class ExceptionPicklingTestCase(unittest.TestCase):
|
||||||
def test_duplicatesectionerror(self):
|
def test_duplicatesectionerror(self):
|
||||||
import pickle
|
import pickle
|
||||||
e1 = configparser.DuplicateSectionError('section', 'source', 123)
|
e1 = configparser.DuplicateSectionError('section', 'source', 123)
|
||||||
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)
|
||||||
|
@ -1633,7 +1637,8 @@ class ExceptionPicklingTestCase(unittest.TestCase):
|
||||||
import pickle
|
import pickle
|
||||||
e1 = configparser.DuplicateOptionError('section', 'option', 'source',
|
e1 = configparser.DuplicateOptionError('section', 'option', 'source',
|
||||||
123)
|
123)
|
||||||
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)
|
||||||
|
@ -1646,7 +1651,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)
|
||||||
|
@ -1658,7 +1664,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)
|
||||||
|
@ -1670,7 +1677,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)
|
||||||
|
@ -1682,7 +1690,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)
|
||||||
|
@ -1696,7 +1705,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)
|
||||||
|
@ -1707,7 +1717,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)
|
||||||
|
@ -1718,7 +1729,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)
|
||||||
|
|
|
@ -2406,6 +2406,7 @@ class PythonAPItests(unittest.TestCase):
|
||||||
self.assertNotIsInstance(Decimal(0), numbers.Real)
|
self.assertNotIsInstance(Decimal(0), numbers.Real)
|
||||||
|
|
||||||
def test_pickle(self):
|
def test_pickle(self):
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
Decimal = self.decimal.Decimal
|
Decimal = self.decimal.Decimal
|
||||||
|
|
||||||
savedecimal = sys.modules['decimal']
|
savedecimal = sys.modules['decimal']
|
||||||
|
@ -2413,7 +2414,7 @@ class PythonAPItests(unittest.TestCase):
|
||||||
# Round trip
|
# Round trip
|
||||||
sys.modules['decimal'] = self.decimal
|
sys.modules['decimal'] = self.decimal
|
||||||
d = Decimal('-3.141590000')
|
d = Decimal('-3.141590000')
|
||||||
p = pickle.dumps(d)
|
p = pickle.dumps(d, proto)
|
||||||
e = pickle.loads(p)
|
e = pickle.loads(p)
|
||||||
self.assertEqual(d, e)
|
self.assertEqual(d, e)
|
||||||
|
|
||||||
|
@ -2423,14 +2424,14 @@ class PythonAPItests(unittest.TestCase):
|
||||||
y = P.Decimal('-3.123e81723')
|
y = P.Decimal('-3.123e81723')
|
||||||
|
|
||||||
sys.modules['decimal'] = C
|
sys.modules['decimal'] = C
|
||||||
sx = pickle.dumps(x)
|
sx = pickle.dumps(x, proto)
|
||||||
sys.modules['decimal'] = P
|
sys.modules['decimal'] = P
|
||||||
r = pickle.loads(sx)
|
r = pickle.loads(sx)
|
||||||
self.assertIsInstance(r, P.Decimal)
|
self.assertIsInstance(r, P.Decimal)
|
||||||
self.assertEqual(r, y)
|
self.assertEqual(r, y)
|
||||||
|
|
||||||
sys.modules['decimal'] = P
|
sys.modules['decimal'] = P
|
||||||
sy = pickle.dumps(y)
|
sy = pickle.dumps(y, proto)
|
||||||
sys.modules['decimal'] = C
|
sys.modules['decimal'] = C
|
||||||
r = pickle.loads(sy)
|
r = pickle.loads(sy)
|
||||||
self.assertIsInstance(r, C.Decimal)
|
self.assertIsInstance(r, C.Decimal)
|
||||||
|
@ -2440,14 +2441,14 @@ class PythonAPItests(unittest.TestCase):
|
||||||
y = P.Decimal('-3.123e81723').as_tuple()
|
y = P.Decimal('-3.123e81723').as_tuple()
|
||||||
|
|
||||||
sys.modules['decimal'] = C
|
sys.modules['decimal'] = C
|
||||||
sx = pickle.dumps(x)
|
sx = pickle.dumps(x, proto)
|
||||||
sys.modules['decimal'] = P
|
sys.modules['decimal'] = P
|
||||||
r = pickle.loads(sx)
|
r = pickle.loads(sx)
|
||||||
self.assertIsInstance(r, P.DecimalTuple)
|
self.assertIsInstance(r, P.DecimalTuple)
|
||||||
self.assertEqual(r, y)
|
self.assertEqual(r, y)
|
||||||
|
|
||||||
sys.modules['decimal'] = P
|
sys.modules['decimal'] = P
|
||||||
sy = pickle.dumps(y)
|
sy = pickle.dumps(y, proto)
|
||||||
sys.modules['decimal'] = C
|
sys.modules['decimal'] = C
|
||||||
r = pickle.loads(sy)
|
r = pickle.loads(sy)
|
||||||
self.assertIsInstance(r, C.DecimalTuple)
|
self.assertIsInstance(r, C.DecimalTuple)
|
||||||
|
@ -2777,6 +2778,7 @@ class ContextAPItests(unittest.TestCase):
|
||||||
|
|
||||||
def test_pickle(self):
|
def test_pickle(self):
|
||||||
|
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
Context = self.decimal.Context
|
Context = self.decimal.Context
|
||||||
|
|
||||||
savedecimal = sys.modules['decimal']
|
savedecimal = sys.modules['decimal']
|
||||||
|
@ -2784,7 +2786,7 @@ class ContextAPItests(unittest.TestCase):
|
||||||
# Round trip
|
# Round trip
|
||||||
sys.modules['decimal'] = self.decimal
|
sys.modules['decimal'] = self.decimal
|
||||||
c = Context()
|
c = Context()
|
||||||
e = pickle.loads(pickle.dumps(c))
|
e = pickle.loads(pickle.dumps(c, proto))
|
||||||
|
|
||||||
self.assertEqual(c.prec, e.prec)
|
self.assertEqual(c.prec, e.prec)
|
||||||
self.assertEqual(c.Emin, e.Emin)
|
self.assertEqual(c.Emin, e.Emin)
|
||||||
|
@ -2817,7 +2819,7 @@ class ContextAPItests(unittest.TestCase):
|
||||||
flags=OrderedSignals[dumper][:fi],
|
flags=OrderedSignals[dumper][:fi],
|
||||||
traps=OrderedSignals[dumper][:ti]
|
traps=OrderedSignals[dumper][:ti]
|
||||||
)
|
)
|
||||||
s = pickle.dumps(c)
|
s = pickle.dumps(c, proto)
|
||||||
|
|
||||||
# The other module loads
|
# The other module loads
|
||||||
sys.modules['decimal'] = loader
|
sys.modules['decimal'] = loader
|
||||||
|
|
|
@ -474,15 +474,16 @@ class TestBasic(unittest.TestCase):
|
||||||
|
|
||||||
def test_iterator_pickle(self):
|
def test_iterator_pickle(self):
|
||||||
data = deque(range(200))
|
data = deque(range(200))
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
it = itorg = iter(data)
|
it = itorg = iter(data)
|
||||||
d = pickle.dumps(it)
|
d = pickle.dumps(it, proto)
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
self.assertEqual(type(itorg), type(it))
|
self.assertEqual(type(itorg), type(it))
|
||||||
self.assertEqual(list(it), list(data))
|
self.assertEqual(list(it), list(data))
|
||||||
|
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
next(it)
|
next(it)
|
||||||
d = pickle.dumps(it)
|
d = pickle.dumps(it, proto)
|
||||||
self.assertEqual(list(it), list(data)[1:])
|
self.assertEqual(list(it), list(data)[1:])
|
||||||
|
|
||||||
def test_deepcopy(self):
|
def test_deepcopy(self):
|
||||||
|
@ -614,7 +615,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))
|
||||||
|
@ -630,7 +632,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))
|
||||||
|
|
|
@ -837,9 +837,10 @@ class DictTest(unittest.TestCase):
|
||||||
self._tracked(MyDict())
|
self._tracked(MyDict())
|
||||||
|
|
||||||
def test_iterator_pickling(self):
|
def test_iterator_pickling(self):
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
data = {1:"a", 2:"b", 3:"c"}
|
data = {1:"a", 2:"b", 3:"c"}
|
||||||
it = iter(data)
|
it = iter(data)
|
||||||
d = pickle.dumps(it)
|
d = pickle.dumps(it, proto)
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
self.assertEqual(sorted(it), sorted(data))
|
self.assertEqual(sorted(it), sorted(data))
|
||||||
|
|
||||||
|
@ -847,44 +848,46 @@ class DictTest(unittest.TestCase):
|
||||||
try:
|
try:
|
||||||
drop = next(it)
|
drop = next(it)
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
return
|
continue
|
||||||
d = pickle.dumps(it)
|
d = pickle.dumps(it, proto)
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
del data[drop]
|
del data[drop]
|
||||||
self.assertEqual(sorted(it), sorted(data))
|
self.assertEqual(sorted(it), sorted(data))
|
||||||
|
|
||||||
def test_itemiterator_pickling(self):
|
def test_itemiterator_pickling(self):
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
data = {1:"a", 2:"b", 3:"c"}
|
data = {1:"a", 2:"b", 3:"c"}
|
||||||
# dictviews aren't picklable, only their iterators
|
# dictviews aren't picklable, only their iterators
|
||||||
itorg = iter(data.items())
|
itorg = iter(data.items())
|
||||||
d = pickle.dumps(itorg)
|
d = pickle.dumps(itorg, proto)
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
# note that the type of type of the unpickled iterator
|
# note that the type of type of the unpickled iterator
|
||||||
# is not necessarily the same as the original. It is
|
# is not necessarily the same as the original. It is
|
||||||
# merely an object supporting the iterator protocol, yielding
|
# merely an object supporting the iterator protocol, yielding
|
||||||
# the same objects as the original one.
|
# the same objects as the original one.
|
||||||
# self.assertEqual(type(itorg), type(it))
|
# self.assertEqual(type(itorg), type(it))
|
||||||
self.assertTrue(isinstance(it, collections.abc.Iterator))
|
self.assertIsInstance(it, collections.abc.Iterator)
|
||||||
self.assertEqual(dict(it), data)
|
self.assertEqual(dict(it), data)
|
||||||
|
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
drop = next(it)
|
drop = next(it)
|
||||||
d = pickle.dumps(it)
|
d = pickle.dumps(it, proto)
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
del data[drop[0]]
|
del data[drop[0]]
|
||||||
self.assertEqual(dict(it), data)
|
self.assertEqual(dict(it), data)
|
||||||
|
|
||||||
def test_valuesiterator_pickling(self):
|
def test_valuesiterator_pickling(self):
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL):
|
||||||
data = {1:"a", 2:"b", 3:"c"}
|
data = {1:"a", 2:"b", 3:"c"}
|
||||||
# data.values() isn't picklable, only its iterator
|
# data.values() isn't picklable, only its iterator
|
||||||
it = iter(data.values())
|
it = iter(data.values())
|
||||||
d = pickle.dumps(it)
|
d = pickle.dumps(it, proto)
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
self.assertEqual(sorted(list(it)), sorted(list(data.values())))
|
self.assertEqual(sorted(list(it)), sorted(list(data.values())))
|
||||||
|
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
drop = next(it)
|
drop = next(it)
|
||||||
d = pickle.dumps(it)
|
d = pickle.dumps(it, proto)
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
values = list(it) + [drop]
|
values = list(it) + [drop]
|
||||||
self.assertEqual(sorted(values), sorted(list(data.values())))
|
self.assertEqual(sorted(values), sorted(list(data.values())))
|
||||||
|
|
|
@ -30,7 +30,8 @@ class TestPickleCopyHeader(TestEmailBase):
|
||||||
|
|
||||||
def header_as_pickle(self, name, value):
|
def header_as_pickle(self, name, value):
|
||||||
header = self.header_factory(name, value)
|
header = self.header_factory(name, value)
|
||||||
p = pickle.dumps(header)
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
p = pickle.dumps(header, proto)
|
||||||
h = pickle.loads(p)
|
h = pickle.loads(p)
|
||||||
self.assertEqual(str(h), str(header))
|
self.assertEqual(str(h), str(header))
|
||||||
|
|
||||||
|
@ -65,7 +66,8 @@ class TestPickleCopyMessage(TestEmailBase):
|
||||||
self.assertEqual(msg2.as_string(), msg.as_string())
|
self.assertEqual(msg2.as_string(), msg.as_string())
|
||||||
|
|
||||||
def msg_as_pickle(self, msg):
|
def msg_as_pickle(self, msg):
|
||||||
p = pickle.dumps(msg)
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
p = pickle.dumps(msg, proto)
|
||||||
msg2 = pickle.loads(p)
|
msg2 = pickle.loads(p)
|
||||||
self.assertEqual(msg2.as_string(), msg.as_string())
|
self.assertEqual(msg2.as_string(), msg.as_string())
|
||||||
|
|
||||||
|
|
|
@ -66,7 +66,8 @@ class N:
|
||||||
class PickleTest:
|
class PickleTest:
|
||||||
# Helper to check picklability
|
# Helper to check picklability
|
||||||
def check_pickle(self, itorg, seq):
|
def check_pickle(self, itorg, seq):
|
||||||
d = pickle.dumps(itorg)
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
d = pickle.dumps(itorg, proto)
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
self.assertEqual(type(itorg), type(it))
|
self.assertEqual(type(itorg), type(it))
|
||||||
self.assertEqual(list(it), seq)
|
self.assertEqual(list(it), seq)
|
||||||
|
@ -76,8 +77,8 @@ class PickleTest:
|
||||||
next(it)
|
next(it)
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
self.assertFalse(seq[1:])
|
self.assertFalse(seq[1:])
|
||||||
return
|
continue
|
||||||
d = pickle.dumps(it)
|
d = pickle.dumps(it, proto)
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
self.assertEqual(list(it), seq[1:])
|
self.assertEqual(list(it), seq[1:])
|
||||||
|
|
||||||
|
|
|
@ -182,7 +182,8 @@ class TestPartialC(TestPartial, unittest.TestCase):
|
||||||
def test_pickle(self):
|
def test_pickle(self):
|
||||||
f = self.partial(signature, 'asdf', bar=True)
|
f = self.partial(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
|
||||||
|
|
|
@ -76,7 +76,8 @@ class TestCase(unittest.TestCase):
|
||||||
|
|
||||||
# Helper to check picklability
|
# Helper to check picklability
|
||||||
def check_pickle(self, itorg, seq):
|
def check_pickle(self, itorg, seq):
|
||||||
d = pickle.dumps(itorg)
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
d = pickle.dumps(itorg, proto)
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
# Cannot assert type equality because dict iterators unpickle as list
|
# Cannot assert type equality because dict iterators unpickle as list
|
||||||
# iterators.
|
# iterators.
|
||||||
|
@ -88,8 +89,8 @@ class TestCase(unittest.TestCase):
|
||||||
try:
|
try:
|
||||||
next(it)
|
next(it)
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
return
|
continue
|
||||||
d = pickle.dumps(it)
|
d = pickle.dumps(it, proto)
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
self.assertEqual(list(it), seq[1:])
|
self.assertEqual(list(it), seq[1:])
|
||||||
|
|
||||||
|
|
|
@ -74,9 +74,12 @@ def testR2(r):
|
||||||
def underten(x):
|
def underten(x):
|
||||||
return x<10
|
return x<10
|
||||||
|
|
||||||
|
picklecopiers = [lambda s, proto=proto: pickle.loads(pickle.dumps(s, proto))
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1)]
|
||||||
|
|
||||||
class TestBasicOps(unittest.TestCase):
|
class TestBasicOps(unittest.TestCase):
|
||||||
|
|
||||||
def pickletest(self, it, stop=4, take=1, compare=None):
|
def pickletest(self, protocol, it, stop=4, take=1, compare=None):
|
||||||
"""Test that an iterator is the same after pickling, also when part-consumed"""
|
"""Test that an iterator is the same after pickling, also when part-consumed"""
|
||||||
def expand(it, i=0):
|
def expand(it, i=0):
|
||||||
# Recursively expand iterables, within sensible bounds
|
# Recursively expand iterables, within sensible bounds
|
||||||
|
@ -91,7 +94,7 @@ class TestBasicOps(unittest.TestCase):
|
||||||
return [expand(e, i+1) for e in l]
|
return [expand(e, i+1) for e in l]
|
||||||
|
|
||||||
# Test the initial copy against the original
|
# Test the initial copy against the original
|
||||||
dump = pickle.dumps(it)
|
dump = pickle.dumps(it, protocol)
|
||||||
i2 = pickle.loads(dump)
|
i2 = pickle.loads(dump)
|
||||||
self.assertEqual(type(it), type(i2))
|
self.assertEqual(type(it), type(i2))
|
||||||
a, b = expand(it), expand(i2)
|
a, b = expand(it), expand(i2)
|
||||||
|
@ -109,7 +112,7 @@ class TestBasicOps(unittest.TestCase):
|
||||||
took += 1
|
took += 1
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
pass #in case there is less data than 'take'
|
pass #in case there is less data than 'take'
|
||||||
dump = pickle.dumps(i3)
|
dump = pickle.dumps(i3, protocol)
|
||||||
i4 = pickle.loads(dump)
|
i4 = pickle.loads(dump)
|
||||||
a, b = expand(i3), expand(i4)
|
a, b = expand(i3), expand(i4)
|
||||||
self.assertEqual(a, b)
|
self.assertEqual(a, b)
|
||||||
|
@ -143,7 +146,8 @@ class TestBasicOps(unittest.TestCase):
|
||||||
[2, 16, 144, 720, 5040, 0, 0, 0, 0, 0])
|
[2, 16, 144, 720, 5040, 0, 0, 0, 0, 0])
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
list(accumulate(s, chr)) # unary-operation
|
list(accumulate(s, chr)) # unary-operation
|
||||||
self.pickletest(accumulate(range(10))) # test pickling
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
self.pickletest(proto, accumulate(range(10))) # test pickling
|
||||||
|
|
||||||
def test_chain(self):
|
def test_chain(self):
|
||||||
|
|
||||||
|
@ -168,9 +172,7 @@ class TestBasicOps(unittest.TestCase):
|
||||||
self.assertRaises(TypeError, list, chain.from_iterable([2, 3]))
|
self.assertRaises(TypeError, list, chain.from_iterable([2, 3]))
|
||||||
|
|
||||||
def test_chain_reducible(self):
|
def test_chain_reducible(self):
|
||||||
operators = [copy.deepcopy,
|
for oper in [copy.deepcopy] + picklecopiers:
|
||||||
lambda s: pickle.loads(pickle.dumps(s))]
|
|
||||||
for oper in operators:
|
|
||||||
it = chain('abc', 'def')
|
it = chain('abc', 'def')
|
||||||
self.assertEqual(list(oper(it)), list('abcdef'))
|
self.assertEqual(list(oper(it)), list('abcdef'))
|
||||||
self.assertEqual(next(it), 'a')
|
self.assertEqual(next(it), 'a')
|
||||||
|
@ -179,7 +181,8 @@ class TestBasicOps(unittest.TestCase):
|
||||||
self.assertEqual(list(oper(chain(''))), [])
|
self.assertEqual(list(oper(chain(''))), [])
|
||||||
self.assertEqual(take(4, oper(chain('abc', 'def'))), list('abcd'))
|
self.assertEqual(take(4, oper(chain('abc', 'def'))), list('abcd'))
|
||||||
self.assertRaises(TypeError, list, oper(chain(2, 3)))
|
self.assertRaises(TypeError, list, oper(chain(2, 3)))
|
||||||
self.pickletest(chain('abc', 'def'), compare=list('abcdef'))
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
self.pickletest(proto, chain('abc', 'def'), compare=list('abcdef'))
|
||||||
|
|
||||||
def test_combinations(self):
|
def test_combinations(self):
|
||||||
self.assertRaises(TypeError, combinations, 'abc') # missing r argument
|
self.assertRaises(TypeError, combinations, 'abc') # missing r argument
|
||||||
|
@ -187,7 +190,7 @@ class TestBasicOps(unittest.TestCase):
|
||||||
self.assertRaises(TypeError, combinations, None) # pool is not iterable
|
self.assertRaises(TypeError, combinations, None) # pool is not iterable
|
||||||
self.assertRaises(ValueError, combinations, 'abc', -2) # r is negative
|
self.assertRaises(ValueError, combinations, 'abc', -2) # r is negative
|
||||||
|
|
||||||
for op in (lambda a:a, lambda a:pickle.loads(pickle.dumps(a))):
|
for op in [lambda a:a] + picklecopiers:
|
||||||
self.assertEqual(list(op(combinations('abc', 32))), []) # r > n
|
self.assertEqual(list(op(combinations('abc', 32))), []) # r > n
|
||||||
|
|
||||||
self.assertEqual(list(op(combinations('ABCD', 2))),
|
self.assertEqual(list(op(combinations('ABCD', 2))),
|
||||||
|
@ -258,7 +261,8 @@ class TestBasicOps(unittest.TestCase):
|
||||||
self.assertEqual(result, list(combinations2(values, r))) # matches second pure python version
|
self.assertEqual(result, list(combinations2(values, r))) # matches second pure python version
|
||||||
self.assertEqual(result, list(combinations3(values, r))) # matches second pure python version
|
self.assertEqual(result, list(combinations3(values, r))) # matches second pure python version
|
||||||
|
|
||||||
self.pickletest(combinations(values, r)) # test pickling
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
self.pickletest(proto, combinations(values, r)) # test pickling
|
||||||
|
|
||||||
# Test implementation detail: tuple re-use
|
# Test implementation detail: tuple re-use
|
||||||
@support.impl_detail("tuple reuse is specific to CPython")
|
@support.impl_detail("tuple reuse is specific to CPython")
|
||||||
|
@ -273,7 +277,7 @@ class TestBasicOps(unittest.TestCase):
|
||||||
self.assertRaises(TypeError, cwr, None) # pool is not iterable
|
self.assertRaises(TypeError, cwr, None) # pool is not iterable
|
||||||
self.assertRaises(ValueError, cwr, 'abc', -2) # r is negative
|
self.assertRaises(ValueError, cwr, 'abc', -2) # r is negative
|
||||||
|
|
||||||
for op in (lambda a:a, lambda a:pickle.loads(pickle.dumps(a))):
|
for op in [lambda a:a] + picklecopiers:
|
||||||
self.assertEqual(list(op(cwr('ABC', 2))),
|
self.assertEqual(list(op(cwr('ABC', 2))),
|
||||||
[('A','A'), ('A','B'), ('A','C'), ('B','B'), ('B','C'), ('C','C')])
|
[('A','A'), ('A','B'), ('A','C'), ('B','B'), ('B','C'), ('C','C')])
|
||||||
testIntermediate = cwr('ABC', 2)
|
testIntermediate = cwr('ABC', 2)
|
||||||
|
@ -339,7 +343,8 @@ class TestBasicOps(unittest.TestCase):
|
||||||
self.assertEqual(result, list(cwr1(values, r))) # matches first pure python version
|
self.assertEqual(result, list(cwr1(values, r))) # matches first pure python version
|
||||||
self.assertEqual(result, list(cwr2(values, r))) # matches second pure python version
|
self.assertEqual(result, list(cwr2(values, r))) # matches second pure python version
|
||||||
|
|
||||||
self.pickletest(cwr(values,r)) # test pickling
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
self.pickletest(proto, cwr(values,r)) # test pickling
|
||||||
|
|
||||||
# Test implementation detail: tuple re-use
|
# Test implementation detail: tuple re-use
|
||||||
|
|
||||||
|
@ -409,7 +414,8 @@ class TestBasicOps(unittest.TestCase):
|
||||||
self.assertEqual(result, list(permutations(values, None))) # test r as None
|
self.assertEqual(result, list(permutations(values, None))) # test r as None
|
||||||
self.assertEqual(result, list(permutations(values))) # test default r
|
self.assertEqual(result, list(permutations(values))) # test default r
|
||||||
|
|
||||||
self.pickletest(permutations(values, r)) # test pickling
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
self.pickletest(proto, permutations(values, r)) # test pickling
|
||||||
|
|
||||||
@support.impl_detail("tuple reuse is specific to CPython")
|
@support.impl_detail("tuple reuse is specific to CPython")
|
||||||
def test_permutations_tuple_reuse(self):
|
def test_permutations_tuple_reuse(self):
|
||||||
|
@ -466,7 +472,7 @@ class TestBasicOps(unittest.TestCase):
|
||||||
self.assertRaises(TypeError, compress, range(6), None) # too many args
|
self.assertRaises(TypeError, compress, range(6), None) # too many args
|
||||||
|
|
||||||
# check copy, deepcopy, pickle
|
# check copy, deepcopy, pickle
|
||||||
for op in (lambda a:copy.copy(a), lambda a:copy.deepcopy(a), lambda a:pickle.loads(pickle.dumps(a))):
|
for op in [lambda a:copy.copy(a), lambda a:copy.deepcopy(a)] + picklecopiers:
|
||||||
for data, selectors, result1, result2 in [
|
for data, selectors, result1, result2 in [
|
||||||
('ABCDEF', [1,0,1,0,1,1], 'ACEF', 'CEF'),
|
('ABCDEF', [1,0,1,0,1,1], 'ACEF', 'CEF'),
|
||||||
('ABCDEF', [0,0,0,0,0,0], '', ''),
|
('ABCDEF', [0,0,0,0,0,0], '', ''),
|
||||||
|
@ -517,7 +523,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.pickletest(count(value))
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
self.pickletest(proto, count(value))
|
||||||
|
|
||||||
#check proper internal error handling for large "step' sizes
|
#check proper internal error handling for large "step' sizes
|
||||||
count(1, maxsize+5); sys.exc_info()
|
count(1, maxsize+5); sys.exc_info()
|
||||||
|
@ -564,7 +571,8 @@ class TestBasicOps(unittest.TestCase):
|
||||||
else:
|
else:
|
||||||
r2 = ('count(%r, %r)' % (i, j))
|
r2 = ('count(%r, %r)' % (i, j))
|
||||||
self.assertEqual(r1, r2)
|
self.assertEqual(r1, r2)
|
||||||
self.pickletest(count(i, j))
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
self.pickletest(proto, count(i, j))
|
||||||
|
|
||||||
def test_cycle(self):
|
def test_cycle(self):
|
||||||
self.assertEqual(take(10, cycle('abc')), list('abcabcabca'))
|
self.assertEqual(take(10, cycle('abc')), list('abcabcabca'))
|
||||||
|
@ -580,10 +588,16 @@ class TestBasicOps(unittest.TestCase):
|
||||||
#an internal iterator
|
#an internal iterator
|
||||||
#self.assertEqual(take(10, copy.copy(c)), list('bcabcabcab'))
|
#self.assertEqual(take(10, copy.copy(c)), list('bcabcabcab'))
|
||||||
self.assertEqual(take(10, copy.deepcopy(c)), list('bcabcabcab'))
|
self.assertEqual(take(10, copy.deepcopy(c)), list('bcabcabcab'))
|
||||||
self.assertEqual(take(10, pickle.loads(pickle.dumps(c))), list('bcabcabcab'))
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
self.assertEqual(take(10, pickle.loads(pickle.dumps(c, proto))),
|
||||||
|
list('bcabcabcab'))
|
||||||
next(c)
|
next(c)
|
||||||
self.assertEqual(take(10, pickle.loads(pickle.dumps(c))), list('cabcabcabc'))
|
self.assertEqual(take(10, pickle.loads(pickle.dumps(c, proto))),
|
||||||
self.pickletest(cycle('abc'))
|
list('cabcabcabc'))
|
||||||
|
next(c)
|
||||||
|
next(c)
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
self.pickletest(proto, cycle('abc'))
|
||||||
|
|
||||||
def test_groupby(self):
|
def test_groupby(self):
|
||||||
# Check whether it accepts arguments correctly
|
# Check whether it accepts arguments correctly
|
||||||
|
@ -604,8 +618,9 @@ class TestBasicOps(unittest.TestCase):
|
||||||
self.assertEqual(s, dup)
|
self.assertEqual(s, dup)
|
||||||
|
|
||||||
# Check normal pickled
|
# Check normal pickled
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
dup = []
|
dup = []
|
||||||
for k, g in pickle.loads(pickle.dumps(groupby(s, testR))):
|
for k, g in pickle.loads(pickle.dumps(groupby(s, testR), proto)):
|
||||||
for elem in g:
|
for elem in g:
|
||||||
self.assertEqual(k, elem[0])
|
self.assertEqual(k, elem[0])
|
||||||
dup.append(elem)
|
dup.append(elem)
|
||||||
|
@ -622,9 +637,10 @@ class TestBasicOps(unittest.TestCase):
|
||||||
self.assertEqual(s, dup)
|
self.assertEqual(s, dup)
|
||||||
|
|
||||||
# Check nested and pickled
|
# Check nested and pickled
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
dup = []
|
dup = []
|
||||||
for k, g in pickle.loads(pickle.dumps(groupby(s, testR))):
|
for k, g in pickle.loads(pickle.dumps(groupby(s, testR), proto)):
|
||||||
for ik, ig in pickle.loads(pickle.dumps(groupby(g, testR2))):
|
for ik, ig in pickle.loads(pickle.dumps(groupby(g, testR2), proto)):
|
||||||
for elem in ig:
|
for elem in ig:
|
||||||
self.assertEqual(k, elem[0])
|
self.assertEqual(k, elem[0])
|
||||||
self.assertEqual(ik, elem[2])
|
self.assertEqual(ik, elem[2])
|
||||||
|
@ -711,12 +727,14 @@ class TestBasicOps(unittest.TestCase):
|
||||||
self.assertEqual(list(copy.copy(c)), ans)
|
self.assertEqual(list(copy.copy(c)), ans)
|
||||||
c = filter(isEven, range(6))
|
c = filter(isEven, range(6))
|
||||||
self.assertEqual(list(copy.deepcopy(c)), ans)
|
self.assertEqual(list(copy.deepcopy(c)), ans)
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
c = filter(isEven, range(6))
|
c = filter(isEven, range(6))
|
||||||
self.assertEqual(list(pickle.loads(pickle.dumps(c))), ans)
|
self.assertEqual(list(pickle.loads(pickle.dumps(c, proto))), ans)
|
||||||
next(c)
|
next(c)
|
||||||
self.assertEqual(list(pickle.loads(pickle.dumps(c))), ans[1:])
|
self.assertEqual(list(pickle.loads(pickle.dumps(c, proto))), ans[1:])
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
c = filter(isEven, range(6))
|
c = filter(isEven, range(6))
|
||||||
self.pickletest(c)
|
self.pickletest(proto, c)
|
||||||
|
|
||||||
def test_filterfalse(self):
|
def test_filterfalse(self):
|
||||||
self.assertEqual(list(filterfalse(isEven, range(6))), [1,3,5])
|
self.assertEqual(list(filterfalse(isEven, range(6))), [1,3,5])
|
||||||
|
@ -728,7 +746,8 @@ class TestBasicOps(unittest.TestCase):
|
||||||
self.assertRaises(TypeError, filterfalse, lambda x:x, range(6), 7)
|
self.assertRaises(TypeError, filterfalse, lambda x:x, range(6), 7)
|
||||||
self.assertRaises(TypeError, filterfalse, isEven, 3)
|
self.assertRaises(TypeError, filterfalse, isEven, 3)
|
||||||
self.assertRaises(TypeError, next, filterfalse(range(6), range(6)))
|
self.assertRaises(TypeError, next, filterfalse(range(6), range(6)))
|
||||||
self.pickletest(filterfalse(isEven, range(6)))
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
self.pickletest(proto, filterfalse(isEven, range(6)))
|
||||||
|
|
||||||
def test_zip(self):
|
def test_zip(self):
|
||||||
# XXX This is rather silly now that builtin zip() calls zip()...
|
# XXX This is rather silly now that builtin zip() calls zip()...
|
||||||
|
@ -760,15 +779,18 @@ class TestBasicOps(unittest.TestCase):
|
||||||
ans = [(x,y) for x, y in copy.deepcopy(zip('abc',count()))]
|
ans = [(x,y) for x, y in copy.deepcopy(zip('abc',count()))]
|
||||||
self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)])
|
self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)])
|
||||||
|
|
||||||
ans = [(x,y) for x, y in pickle.loads(pickle.dumps(zip('abc',count())))]
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
ans = [(x,y) for x, y in pickle.loads(pickle.dumps(zip('abc',count()), proto))]
|
||||||
self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)])
|
self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)])
|
||||||
|
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
testIntermediate = zip('abc',count())
|
testIntermediate = zip('abc',count())
|
||||||
next(testIntermediate)
|
next(testIntermediate)
|
||||||
ans = [(x,y) for x, y in pickle.loads(pickle.dumps(testIntermediate))]
|
ans = [(x,y) for x, y in pickle.loads(pickle.dumps(testIntermediate, proto))]
|
||||||
self.assertEqual(ans, [('b', 1), ('c', 2)])
|
self.assertEqual(ans, [('b', 1), ('c', 2)])
|
||||||
|
|
||||||
self.pickletest(zip('abc', count()))
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
self.pickletest(proto, zip('abc', count()))
|
||||||
|
|
||||||
def test_ziplongest(self):
|
def test_ziplongest(self):
|
||||||
for args in [
|
for args in [
|
||||||
|
@ -820,10 +842,11 @@ class TestBasicOps(unittest.TestCase):
|
||||||
self.assertEqual(len(dict.fromkeys(ids)), len(ids))
|
self.assertEqual(len(dict.fromkeys(ids)), len(ids))
|
||||||
|
|
||||||
def test_zip_longest_pickling(self):
|
def test_zip_longest_pickling(self):
|
||||||
self.pickletest(zip_longest("abc", "def"))
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
self.pickletest(zip_longest("abc", "defgh"))
|
self.pickletest(proto, zip_longest("abc", "def"))
|
||||||
self.pickletest(zip_longest("abc", "defgh", fillvalue=1))
|
self.pickletest(proto, zip_longest("abc", "defgh"))
|
||||||
self.pickletest(zip_longest("", "defgh"))
|
self.pickletest(proto, zip_longest("abc", "defgh", fillvalue=1))
|
||||||
|
self.pickletest(proto, zip_longest("", "defgh"))
|
||||||
|
|
||||||
def test_bug_7244(self):
|
def test_bug_7244(self):
|
||||||
|
|
||||||
|
@ -940,7 +963,8 @@ class TestBasicOps(unittest.TestCase):
|
||||||
]:
|
]:
|
||||||
self.assertEqual(list(copy.copy(product(*args))), result)
|
self.assertEqual(list(copy.copy(product(*args))), result)
|
||||||
self.assertEqual(list(copy.deepcopy(product(*args))), result)
|
self.assertEqual(list(copy.deepcopy(product(*args))), result)
|
||||||
self.pickletest(product(*args))
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
self.pickletest(proto, product(*args))
|
||||||
|
|
||||||
def test_repeat(self):
|
def test_repeat(self):
|
||||||
self.assertEqual(list(repeat(object='a', times=3)), ['a', 'a', 'a'])
|
self.assertEqual(list(repeat(object='a', times=3)), ['a', 'a', 'a'])
|
||||||
|
@ -965,7 +989,8 @@ class TestBasicOps(unittest.TestCase):
|
||||||
self.assertEqual(next(c), 'a')
|
self.assertEqual(next(c), 'a')
|
||||||
self.assertEqual(take(2, copy.copy(c)), list('a' * 2))
|
self.assertEqual(take(2, copy.copy(c)), list('a' * 2))
|
||||||
self.assertEqual(take(2, copy.deepcopy(c)), list('a' * 2))
|
self.assertEqual(take(2, copy.deepcopy(c)), list('a' * 2))
|
||||||
self.pickletest(repeat(object='a', times=10))
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
self.pickletest(proto, repeat(object='a', times=10))
|
||||||
|
|
||||||
def test_repeat_with_negative_times(self):
|
def test_repeat_with_negative_times(self):
|
||||||
self.assertEqual(repr(repeat('a', -1)), "repeat('a', 0)")
|
self.assertEqual(repr(repeat('a', -1)), "repeat('a', 0)")
|
||||||
|
@ -999,8 +1024,9 @@ class TestBasicOps(unittest.TestCase):
|
||||||
c = map(tupleize, 'abc', count())
|
c = map(tupleize, 'abc', count())
|
||||||
self.assertEqual(list(copy.deepcopy(c)), ans)
|
self.assertEqual(list(copy.deepcopy(c)), ans)
|
||||||
|
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
c = map(tupleize, 'abc', count())
|
c = map(tupleize, 'abc', count())
|
||||||
self.pickletest(c)
|
self.pickletest(proto, c)
|
||||||
|
|
||||||
def test_starmap(self):
|
def test_starmap(self):
|
||||||
self.assertEqual(list(starmap(operator.pow, zip(range(3), range(1,7)))),
|
self.assertEqual(list(starmap(operator.pow, zip(range(3), range(1,7)))),
|
||||||
|
@ -1025,8 +1051,9 @@ class TestBasicOps(unittest.TestCase):
|
||||||
c = starmap(operator.pow, zip(range(3), range(1,7)))
|
c = starmap(operator.pow, zip(range(3), range(1,7)))
|
||||||
self.assertEqual(list(copy.deepcopy(c)), ans)
|
self.assertEqual(list(copy.deepcopy(c)), ans)
|
||||||
|
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
c = starmap(operator.pow, zip(range(3), range(1,7)))
|
c = starmap(operator.pow, zip(range(3), range(1,7)))
|
||||||
self.pickletest(c)
|
self.pickletest(proto, c)
|
||||||
|
|
||||||
def test_islice(self):
|
def test_islice(self):
|
||||||
for args in [ # islice(args) should agree with range(args)
|
for args in [ # islice(args) should agree with range(args)
|
||||||
|
@ -1091,7 +1118,8 @@ class TestBasicOps(unittest.TestCase):
|
||||||
list(range(*args)))
|
list(range(*args)))
|
||||||
self.assertEqual(list(copy.deepcopy(islice(range(100), *args))),
|
self.assertEqual(list(copy.deepcopy(islice(range(100), *args))),
|
||||||
list(range(*args)))
|
list(range(*args)))
|
||||||
self.pickletest(islice(range(100), *args))
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
self.pickletest(proto, islice(range(100), *args))
|
||||||
|
|
||||||
# Issue #21321: check source iterator is not referenced
|
# Issue #21321: check source iterator is not referenced
|
||||||
# from islice() after the latter has been exhausted
|
# from islice() after the latter has been exhausted
|
||||||
|
@ -1120,7 +1148,8 @@ class TestBasicOps(unittest.TestCase):
|
||||||
self.assertEqual(list(copy.copy(takewhile(underten, data))), [1, 3, 5])
|
self.assertEqual(list(copy.copy(takewhile(underten, data))), [1, 3, 5])
|
||||||
self.assertEqual(list(copy.deepcopy(takewhile(underten, data))),
|
self.assertEqual(list(copy.deepcopy(takewhile(underten, data))),
|
||||||
[1, 3, 5])
|
[1, 3, 5])
|
||||||
self.pickletest(takewhile(underten, data))
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
self.pickletest(proto, takewhile(underten, data))
|
||||||
|
|
||||||
def test_dropwhile(self):
|
def test_dropwhile(self):
|
||||||
data = [1, 3, 5, 20, 2, 4, 6, 8]
|
data = [1, 3, 5, 20, 2, 4, 6, 8]
|
||||||
|
@ -1136,7 +1165,8 @@ class TestBasicOps(unittest.TestCase):
|
||||||
self.assertEqual(list(copy.copy(dropwhile(underten, data))), [20, 2, 4, 6, 8])
|
self.assertEqual(list(copy.copy(dropwhile(underten, data))), [20, 2, 4, 6, 8])
|
||||||
self.assertEqual(list(copy.deepcopy(dropwhile(underten, data))),
|
self.assertEqual(list(copy.deepcopy(dropwhile(underten, data))),
|
||||||
[20, 2, 4, 6, 8])
|
[20, 2, 4, 6, 8])
|
||||||
self.pickletest(dropwhile(underten, data))
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
self.pickletest(proto, dropwhile(underten, data))
|
||||||
|
|
||||||
def test_tee(self):
|
def test_tee(self):
|
||||||
n = 200
|
n = 200
|
||||||
|
@ -1280,10 +1310,11 @@ class TestBasicOps(unittest.TestCase):
|
||||||
self.assertEqual(list(b), long_ans[60:])
|
self.assertEqual(list(b), long_ans[60:])
|
||||||
|
|
||||||
# check pickle
|
# check pickle
|
||||||
self.pickletest(iter(tee('abc')))
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
self.pickletest(proto, iter(tee('abc')))
|
||||||
a, b = tee('abc')
|
a, b = tee('abc')
|
||||||
self.pickletest(a, compare=ans)
|
self.pickletest(proto, a, compare=ans)
|
||||||
self.pickletest(b, compare=ans)
|
self.pickletest(proto, b, compare=ans)
|
||||||
|
|
||||||
# Issue 13454: Crash when deleting backward iterator from tee()
|
# Issue 13454: Crash when deleting backward iterator from tee()
|
||||||
def test_tee_del_backward(self):
|
def test_tee_del_backward(self):
|
||||||
|
@ -1323,11 +1354,14 @@ class TestExamples(unittest.TestCase):
|
||||||
# check copy, deepcopy, pickle
|
# check copy, deepcopy, pickle
|
||||||
data = [1, 2, 3, 4, 5]
|
data = [1, 2, 3, 4, 5]
|
||||||
accumulated = [1, 3, 6, 10, 15]
|
accumulated = [1, 3, 6, 10, 15]
|
||||||
it = accumulate(data)
|
|
||||||
|
|
||||||
self.assertEqual(list(pickle.loads(pickle.dumps(it))), accumulated[:])
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
it = accumulate(data)
|
||||||
|
self.assertEqual(list(pickle.loads(pickle.dumps(it, proto))), accumulated[:])
|
||||||
|
self.assertEqual(next(it), 1)
|
||||||
|
self.assertEqual(list(pickle.loads(pickle.dumps(it, proto))), accumulated[1:])
|
||||||
|
it = accumulate(data)
|
||||||
self.assertEqual(next(it), 1)
|
self.assertEqual(next(it), 1)
|
||||||
self.assertEqual(list(pickle.loads(pickle.dumps(it))), accumulated[1:])
|
|
||||||
self.assertEqual(list(copy.deepcopy(it)), accumulated[1:])
|
self.assertEqual(list(copy.deepcopy(it)), accumulated[1:])
|
||||||
self.assertEqual(list(copy.copy(it)), accumulated[1:])
|
self.assertEqual(list(copy.copy(it)), accumulated[1:])
|
||||||
|
|
||||||
|
|
|
@ -74,28 +74,30 @@ class ListTest(list_tests.CommonTest):
|
||||||
# Userlist iterators don't support pickling yet since
|
# Userlist iterators don't support pickling yet since
|
||||||
# they are based on generators.
|
# they are based on generators.
|
||||||
data = self.type2test([4, 5, 6, 7])
|
data = self.type2test([4, 5, 6, 7])
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
it = itorg = iter(data)
|
it = itorg = iter(data)
|
||||||
d = pickle.dumps(it)
|
d = pickle.dumps(it, proto)
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
self.assertEqual(type(itorg), type(it))
|
self.assertEqual(type(itorg), type(it))
|
||||||
self.assertEqual(self.type2test(it), self.type2test(data))
|
self.assertEqual(self.type2test(it), self.type2test(data))
|
||||||
|
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
next(it)
|
next(it)
|
||||||
d = pickle.dumps(it)
|
d = pickle.dumps(it, proto)
|
||||||
self.assertEqual(self.type2test(it), self.type2test(data)[1:])
|
self.assertEqual(self.type2test(it), self.type2test(data)[1:])
|
||||||
|
|
||||||
def test_reversed_pickle(self):
|
def test_reversed_pickle(self):
|
||||||
data = self.type2test([4, 5, 6, 7])
|
data = self.type2test([4, 5, 6, 7])
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
it = itorg = reversed(data)
|
it = itorg = reversed(data)
|
||||||
d = pickle.dumps(it)
|
d = pickle.dumps(it, proto)
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
self.assertEqual(type(itorg), type(it))
|
self.assertEqual(type(itorg), type(it))
|
||||||
self.assertEqual(self.type2test(it), self.type2test(reversed(data)))
|
self.assertEqual(self.type2test(it), self.type2test(reversed(data)))
|
||||||
|
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
next(it)
|
next(it)
|
||||||
d = pickle.dumps(it)
|
d = pickle.dumps(it, proto)
|
||||||
self.assertEqual(self.type2test(it), self.type2test(reversed(data))[1:])
|
self.assertEqual(self.type2test(it), self.type2test(reversed(data))[1:])
|
||||||
|
|
||||||
def test_no_comdat_folding(self):
|
def test_no_comdat_folding(self):
|
||||||
|
|
|
@ -220,10 +220,11 @@ class CompressorDecompressorTestCase(unittest.TestCase):
|
||||||
# Pickling raises an exception; there's no way to serialize an lzma_stream.
|
# Pickling raises an exception; there's no way to serialize an lzma_stream.
|
||||||
|
|
||||||
def test_pickle(self):
|
def test_pickle(self):
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
pickle.dumps(LZMACompressor())
|
pickle.dumps(LZMACompressor(), proto)
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
pickle.dumps(LZMADecompressor())
|
pickle.dumps(LZMADecompressor(), proto)
|
||||||
|
|
||||||
|
|
||||||
class CompressDecompressFunctionTestCase(unittest.TestCase):
|
class CompressDecompressFunctionTestCase(unittest.TestCase):
|
||||||
|
|
|
@ -366,13 +366,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())
|
||||||
|
|
|
@ -1468,7 +1468,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:
|
||||||
|
|
|
@ -265,8 +265,11 @@ class StatAttributeTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_stat_result_pickle(self):
|
def test_stat_result_pickle(self):
|
||||||
result = os.stat(self.fname)
|
result = os.stat(self.fname)
|
||||||
p = pickle.dumps(result)
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
self.assertIn(b'\x03cos\nstat_result\n', p)
|
p = pickle.dumps(result, proto)
|
||||||
|
self.assertIn(b'stat_result', p)
|
||||||
|
if proto < 4:
|
||||||
|
self.assertIn(b'cos\nstat_result\n', p)
|
||||||
unpickled = pickle.loads(p)
|
unpickled = pickle.loads(p)
|
||||||
self.assertEqual(result, unpickled)
|
self.assertEqual(result, unpickled)
|
||||||
|
|
||||||
|
@ -324,8 +327,11 @@ class StatAttributeTests(unittest.TestCase):
|
||||||
if e.errno == errno.ENOSYS:
|
if e.errno == errno.ENOSYS:
|
||||||
self.skipTest('os.statvfs() failed with ENOSYS')
|
self.skipTest('os.statvfs() failed with ENOSYS')
|
||||||
|
|
||||||
p = pickle.dumps(result)
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
self.assertIn(b'\x03cos\nstatvfs_result\n', p)
|
p = pickle.dumps(result, proto)
|
||||||
|
self.assertIn(b'statvfs_result', p)
|
||||||
|
if proto < 4:
|
||||||
|
self.assertIn(b'cos\nstatvfs_result\n', p)
|
||||||
unpickled = pickle.loads(p)
|
unpickled = pickle.loads(p)
|
||||||
self.assertEqual(result, unpickled)
|
self.assertEqual(result, unpickled)
|
||||||
|
|
||||||
|
|
|
@ -159,7 +159,8 @@ class TestBasicOps:
|
||||||
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 range(10)]
|
origseq = [self.gen.random() for i in range(10)]
|
||||||
newgen = pickle.loads(state)
|
newgen = pickle.loads(state)
|
||||||
restoredseq = [newgen.random() for i in range(10)]
|
restoredseq = [newgen.random() for i in range(10)]
|
||||||
|
@ -215,7 +216,8 @@ class SystemRandom_TestBasicOps(TestBasicOps, unittest.TestCase):
|
||||||
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.
|
||||||
|
|
|
@ -366,7 +366,7 @@ class RangeTest(unittest.TestCase):
|
||||||
it = itorg = iter(range(*t))
|
it = itorg = iter(range(*t))
|
||||||
data = list(range(*t))
|
data = list(range(*t))
|
||||||
|
|
||||||
d = pickle.dumps(it)
|
d = pickle.dumps(it, proto)
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
self.assertEqual(type(itorg), type(it))
|
self.assertEqual(type(itorg), type(it))
|
||||||
self.assertEqual(list(it), data)
|
self.assertEqual(list(it), data)
|
||||||
|
@ -376,30 +376,32 @@ class RangeTest(unittest.TestCase):
|
||||||
next(it)
|
next(it)
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
continue
|
continue
|
||||||
d = pickle.dumps(it)
|
d = pickle.dumps(it, proto)
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
self.assertEqual(list(it), data[1:])
|
self.assertEqual(list(it), data[1:])
|
||||||
|
|
||||||
def test_exhausted_iterator_pickling(self):
|
def test_exhausted_iterator_pickling(self):
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
r = range(2**65, 2**65+2)
|
r = range(2**65, 2**65+2)
|
||||||
i = iter(r)
|
i = iter(r)
|
||||||
while True:
|
while True:
|
||||||
r = next(i)
|
r = next(i)
|
||||||
if r == 2**65+1:
|
if r == 2**65+1:
|
||||||
break
|
break
|
||||||
d = pickle.dumps(i)
|
d = pickle.dumps(i, proto)
|
||||||
i2 = pickle.loads(d)
|
i2 = pickle.loads(d)
|
||||||
self.assertEqual(list(i), [])
|
self.assertEqual(list(i), [])
|
||||||
self.assertEqual(list(i2), [])
|
self.assertEqual(list(i2), [])
|
||||||
|
|
||||||
def test_large_exhausted_iterator_pickling(self):
|
def test_large_exhausted_iterator_pickling(self):
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
r = range(20)
|
r = range(20)
|
||||||
i = iter(r)
|
i = iter(r)
|
||||||
while True:
|
while True:
|
||||||
r = next(i)
|
r = next(i)
|
||||||
if r == 19:
|
if r == 19:
|
||||||
break
|
break
|
||||||
d = pickle.dumps(i)
|
d = pickle.dumps(i, proto)
|
||||||
i2 = pickle.loads(d)
|
i2 = pickle.loads(d)
|
||||||
self.assertEqual(list(i), [])
|
self.assertEqual(list(i), [])
|
||||||
self.assertEqual(list(i2), [])
|
self.assertEqual(list(i2), [])
|
||||||
|
|
|
@ -231,27 +231,28 @@ class TestJointOps:
|
||||||
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)
|
||||||
|
|
||||||
def test_iterator_pickling(self):
|
def test_iterator_pickling(self):
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
itorg = iter(self.s)
|
itorg = iter(self.s)
|
||||||
data = self.thetype(self.s)
|
data = self.thetype(self.s)
|
||||||
d = pickle.dumps(itorg)
|
d = pickle.dumps(itorg, proto)
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
# Set iterators unpickle as list iterators due to the
|
# Set iterators unpickle as list iterators due to the
|
||||||
# undefined order of set items.
|
# undefined order of set items.
|
||||||
# self.assertEqual(type(itorg), type(it))
|
# self.assertEqual(type(itorg), type(it))
|
||||||
self.assertTrue(isinstance(it, collections.abc.Iterator))
|
self.assertIsInstance(it, collections.abc.Iterator)
|
||||||
self.assertEqual(self.thetype(it), data)
|
self.assertEqual(self.thetype(it), data)
|
||||||
|
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
try:
|
try:
|
||||||
drop = next(it)
|
drop = next(it)
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
return
|
continue
|
||||||
d = pickle.dumps(it)
|
d = pickle.dumps(it, proto)
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
self.assertEqual(self.thetype(it), data - self.thetype((drop,)))
|
self.assertEqual(self.thetype(it), data - self.thetype((drop,)))
|
||||||
|
|
||||||
|
@ -851,7 +852,8 @@ class TestBasicOps:
|
||||||
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))
|
||||||
|
|
|
@ -169,28 +169,30 @@ class TupleTest(seq_tests.CommonTest):
|
||||||
# Userlist iterators don't support pickling yet since
|
# Userlist iterators don't support pickling yet since
|
||||||
# they are based on generators.
|
# they are based on generators.
|
||||||
data = self.type2test([4, 5, 6, 7])
|
data = self.type2test([4, 5, 6, 7])
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
itorg = iter(data)
|
itorg = iter(data)
|
||||||
d = pickle.dumps(itorg)
|
d = pickle.dumps(itorg, proto)
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
self.assertEqual(type(itorg), type(it))
|
self.assertEqual(type(itorg), type(it))
|
||||||
self.assertEqual(self.type2test(it), self.type2test(data))
|
self.assertEqual(self.type2test(it), self.type2test(data))
|
||||||
|
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
next(it)
|
next(it)
|
||||||
d = pickle.dumps(it)
|
d = pickle.dumps(it, proto)
|
||||||
self.assertEqual(self.type2test(it), self.type2test(data)[1:])
|
self.assertEqual(self.type2test(it), self.type2test(data)[1:])
|
||||||
|
|
||||||
def test_reversed_pickle(self):
|
def test_reversed_pickle(self):
|
||||||
data = self.type2test([4, 5, 6, 7])
|
data = self.type2test([4, 5, 6, 7])
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
itorg = reversed(data)
|
itorg = reversed(data)
|
||||||
d = pickle.dumps(itorg)
|
d = pickle.dumps(itorg, proto)
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
self.assertEqual(type(itorg), type(it))
|
self.assertEqual(type(itorg), type(it))
|
||||||
self.assertEqual(self.type2test(it), self.type2test(reversed(data)))
|
self.assertEqual(self.type2test(it), self.type2test(reversed(data)))
|
||||||
|
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
next(it)
|
next(it)
|
||||||
d = pickle.dumps(it)
|
d = pickle.dumps(it, proto)
|
||||||
self.assertEqual(self.type2test(it), self.type2test(reversed(data))[1:])
|
self.assertEqual(self.type2test(it), self.type2test(reversed(data))[1:])
|
||||||
|
|
||||||
def test_no_comdat_folding(self):
|
def test_no_comdat_folding(self):
|
||||||
|
|
|
@ -84,16 +84,17 @@ class NodeListTestCase(unittest.TestCase):
|
||||||
def test_nodelist_pickle_roundtrip(self):
|
def test_nodelist_pickle_roundtrip(self):
|
||||||
# Test pickling and unpickling of a NodeList.
|
# Test pickling and unpickling of a NodeList.
|
||||||
|
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
# Empty NodeList.
|
# Empty NodeList.
|
||||||
node_list = NodeList()
|
node_list = NodeList()
|
||||||
pickled = pickle.dumps(node_list)
|
pickled = pickle.dumps(node_list, proto)
|
||||||
unpickled = pickle.loads(pickled)
|
unpickled = pickle.loads(pickled)
|
||||||
self.assertEqual(unpickled, node_list)
|
self.assertEqual(unpickled, node_list)
|
||||||
|
|
||||||
# Non-empty NodeList.
|
# Non-empty NodeList.
|
||||||
node_list.append(1)
|
node_list.append(1)
|
||||||
node_list.append(2)
|
node_list.append(2)
|
||||||
pickled = pickle.dumps(node_list)
|
pickled = pickle.dumps(node_list, proto)
|
||||||
unpickled = pickle.loads(pickled)
|
unpickled = pickle.loads(pickled)
|
||||||
self.assertEqual(unpickled, node_list)
|
self.assertEqual(unpickled, node_list)
|
||||||
|
|
||||||
|
|
|
@ -121,11 +121,11 @@ class ElementTestCase:
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.modules = {pyET, ET}
|
cls.modules = {pyET, ET}
|
||||||
|
|
||||||
def pickleRoundTrip(self, obj, name, dumper, loader):
|
def pickleRoundTrip(self, obj, name, dumper, loader, proto):
|
||||||
save_m = sys.modules[name]
|
save_m = sys.modules[name]
|
||||||
try:
|
try:
|
||||||
sys.modules[name] = dumper
|
sys.modules[name] = dumper
|
||||||
temp = pickle.dumps(obj)
|
temp = pickle.dumps(obj, proto)
|
||||||
sys.modules[name] = loader
|
sys.modules[name] = loader
|
||||||
result = pickle.loads(temp)
|
result = pickle.loads(temp)
|
||||||
except pickle.PicklingError as pe:
|
except pickle.PicklingError as pe:
|
||||||
|
@ -1677,6 +1677,7 @@ class BasicElementTest(ElementTestCase, unittest.TestCase):
|
||||||
|
|
||||||
def test_pickle(self):
|
def test_pickle(self):
|
||||||
# issue #16076: the C implementation wasn't pickleable.
|
# issue #16076: the C implementation wasn't pickleable.
|
||||||
|
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
|
||||||
for dumper, loader in product(self.modules, repeat=2):
|
for dumper, loader in product(self.modules, repeat=2):
|
||||||
e = dumper.Element('foo', bar=42)
|
e = dumper.Element('foo', bar=42)
|
||||||
e.text = "text goes here"
|
e.text = "text goes here"
|
||||||
|
@ -1686,7 +1687,7 @@ class BasicElementTest(ElementTestCase, unittest.TestCase):
|
||||||
e.findall('.//grandchild')[0].set('attr', 'other value')
|
e.findall('.//grandchild')[0].set('attr', 'other value')
|
||||||
|
|
||||||
e2 = self.pickleRoundTrip(e, 'xml.etree.ElementTree',
|
e2 = self.pickleRoundTrip(e, 'xml.etree.ElementTree',
|
||||||
dumper, loader)
|
dumper, loader, proto)
|
||||||
|
|
||||||
self.assertEqual(e2.tag, 'foo')
|
self.assertEqual(e2.tag, 'foo')
|
||||||
self.assertEqual(e2.attrib['bar'], 42)
|
self.assertEqual(e2.attrib['bar'], 42)
|
||||||
|
@ -1694,6 +1695,7 @@ class BasicElementTest(ElementTestCase, unittest.TestCase):
|
||||||
self.assertEqualElements(e, e2)
|
self.assertEqualElements(e, e2)
|
||||||
|
|
||||||
def test_pickle_issue18997(self):
|
def test_pickle_issue18997(self):
|
||||||
|
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
|
||||||
for dumper, loader in product(self.modules, repeat=2):
|
for dumper, loader in product(self.modules, repeat=2):
|
||||||
XMLTEXT = """<?xml version="1.0"?>
|
XMLTEXT = """<?xml version="1.0"?>
|
||||||
<group><dogs>4</dogs>
|
<group><dogs>4</dogs>
|
||||||
|
@ -1701,7 +1703,8 @@ class BasicElementTest(ElementTestCase, unittest.TestCase):
|
||||||
e1 = dumper.fromstring(XMLTEXT)
|
e1 = dumper.fromstring(XMLTEXT)
|
||||||
if hasattr(e1, '__getstate__'):
|
if hasattr(e1, '__getstate__'):
|
||||||
self.assertEqual(e1.__getstate__()['tag'], 'group')
|
self.assertEqual(e1.__getstate__()['tag'], 'group')
|
||||||
e2 = self.pickleRoundTrip(e1, 'xml.etree.ElementTree', dumper, loader)
|
e2 = self.pickleRoundTrip(e1, 'xml.etree.ElementTree',
|
||||||
|
dumper, loader, proto)
|
||||||
self.assertEqual(e2.tag, 'group')
|
self.assertEqual(e2.tag, 'group')
|
||||||
self.assertEqual(e2[0].tag, 'dogs')
|
self.assertEqual(e2[0].tag, 'dogs')
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue