mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +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,8 +45,9 @@ 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):
|
||||||
self.assertEqual(pickle.loads(dump), params)
|
dump = pickle.dumps(params, proto)
|
||||||
|
self.assertEqual(pickle.loads(dump), params)
|
||||||
|
|
||||||
|
|
||||||
class AudioWriteTests(AudioTests):
|
class AudioWriteTests(AudioTests):
|
||||||
|
|
|
@ -1682,11 +1682,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
|
||||||
|
|
|
@ -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):
|
||||||
self.assertEqual(lst2, lst)
|
lst2 = pickle.loads(pickle.dumps(lst, proto))
|
||||||
self.assertNotEqual(id(lst2), id(lst))
|
self.assertEqual(lst2, lst)
|
||||||
|
self.assertNotEqual(id(lst2), id(lst))
|
||||||
|
|
|
@ -285,17 +285,18 @@ 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)
|
||||||
orgit = iter(data)
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
d = pickle.dumps(orgit)
|
orgit = iter(data)
|
||||||
it = pickle.loads(d)
|
d = pickle.dumps(orgit, proto)
|
||||||
self.assertEqual(type(orgit), type(it))
|
|
||||||
self.assertEqual(list(it), list(data))
|
|
||||||
|
|
||||||
if len(data):
|
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
next(it)
|
self.assertEqual(type(orgit), type(it))
|
||||||
d = pickle.dumps(it)
|
self.assertEqual(list(it), list(data))
|
||||||
self.assertEqual(list(it), list(data)[1:])
|
|
||||||
|
if len(data):
|
||||||
|
it = pickle.loads(d)
|
||||||
|
next(it)
|
||||||
|
d = pickle.dumps(it, proto)
|
||||||
|
self.assertEqual(list(it), list(data)[1:])
|
||||||
|
|
||||||
def test_insert(self):
|
def test_insert(self):
|
||||||
a = array.array(self.typecode, self.example)
|
a = array.array(self.typecode, self.example)
|
||||||
|
|
|
@ -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):
|
||||||
f1 = filter(filter_char, "abcdeabcde")
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
f2 = filter(filter_char, "abcdeabcde")
|
f1 = filter(filter_char, "abcdeabcde")
|
||||||
self.check_iter_pickle(f1, list(f2))
|
f2 = filter(filter_char, "abcdeabcde")
|
||||||
|
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):
|
||||||
m1 = map(map_char, "Is this the real life?")
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
m2 = map(map_char, "Is this the real life?")
|
m1 = map(map_char, "Is this the real life?")
|
||||||
self.check_iter_pickle(m1, list(m2))
|
m2 = map(map_char, "Is this the real life?")
|
||||||
|
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)]
|
||||||
z1 = zip(a, b)
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
self.check_iter_pickle(z1, t)
|
z1 = zip(a, b)
|
||||||
|
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,22 +555,23 @@ class BaseBytesTest:
|
||||||
self.assertEqual(b, q)
|
self.assertEqual(b, q)
|
||||||
|
|
||||||
def test_iterator_pickling(self):
|
def test_iterator_pickling(self):
|
||||||
for b in b"", b"a", b"abc", b"\xffab\x80", b"\0\0\377\0\0":
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
it = itorg = iter(self.type2test(b))
|
for b in b"", b"a", b"abc", b"\xffab\x80", b"\0\0\377\0\0":
|
||||||
data = list(self.type2test(b))
|
it = itorg = iter(self.type2test(b))
|
||||||
d = pickle.dumps(it)
|
data = list(self.type2test(b))
|
||||||
it = pickle.loads(d)
|
d = pickle.dumps(it, proto)
|
||||||
self.assertEqual(type(itorg), type(it))
|
it = pickle.loads(d)
|
||||||
self.assertEqual(list(it), data)
|
self.assertEqual(type(itorg), type(it))
|
||||||
|
self.assertEqual(list(it), data)
|
||||||
|
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
try:
|
try:
|
||||||
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_strip(self):
|
def test_strip(self):
|
||||||
b = self.type2test(b'mississippi')
|
b = self.type2test(b'mississippi')
|
||||||
|
|
|
@ -646,8 +646,9 @@ class BZ2CompressorTest(BaseTest):
|
||||||
data = None
|
data = None
|
||||||
|
|
||||||
def testPickle(self):
|
def testPickle(self):
|
||||||
with self.assertRaises(TypeError):
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
pickle.dumps(BZ2Compressor())
|
with self.assertRaises(TypeError):
|
||||||
|
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):
|
||||||
with self.assertRaises(TypeError):
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
pickle.dumps(BZ2Decompressor())
|
with self.assertRaises(TypeError):
|
||||||
|
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())
|
||||||
|
def check(dup):
|
||||||
|
msg = "\ncopy: %s\nwords: %s" % (dup, words)
|
||||||
|
self.assertIsNot(dup, words, msg)
|
||||||
|
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 = Counter()
|
||||||
update_test.update(words)
|
update_test.update(words)
|
||||||
for label, dup in [
|
check(update_test)
|
||||||
('words.copy()', words.copy()),
|
check(Counter(words))
|
||||||
('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)
|
|
||||||
self.assertIsNot(dup, words, msg)
|
|
||||||
self.assertEqual(dup, 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)
|
||||||
|
def check(dup):
|
||||||
|
msg = "\ncopy: %s\nod: %s" % (dup, od)
|
||||||
|
self.assertIsNot(dup, od, msg)
|
||||||
|
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 = OrderedDict()
|
||||||
update_test.update(od)
|
update_test.update(od)
|
||||||
for label, dup in [
|
check(update_test)
|
||||||
('od.copy()', od.copy()),
|
check(OrderedDict(od))
|
||||||
('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)
|
|
||||||
self.assertIsNot(dup, od, msg)
|
|
||||||
self.assertEqual(dup, 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,104 +1591,113 @@ 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', 'source', 123)
|
e1 = configparser.DuplicateSectionError('section', 'source', 123)
|
||||||
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.source, e2.source)
|
self.assertEqual(e1.section, e2.section)
|
||||||
self.assertEqual(e1.lineno, e2.lineno)
|
self.assertEqual(e1.source, e2.source)
|
||||||
self.assertEqual(repr(e1), repr(e2))
|
self.assertEqual(e1.lineno, e2.lineno)
|
||||||
|
self.assertEqual(repr(e1), repr(e2))
|
||||||
|
|
||||||
def test_duplicateoptionerror(self):
|
def test_duplicateoptionerror(self):
|
||||||
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):
|
||||||
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.source, e2.source)
|
self.assertEqual(e1.option, e2.option)
|
||||||
self.assertEqual(e1.lineno, e2.lineno)
|
self.assertEqual(e1.source, e2.source)
|
||||||
self.assertEqual(repr(e1), repr(e2))
|
self.assertEqual(e1.lineno, e2.lineno)
|
||||||
|
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
|
||||||
|
@ -1696,36 +1705,39 @@ 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.source, e2.source)
|
self.assertEqual(e1.args, e2.args)
|
||||||
self.assertEqual(e1.errors, e2.errors)
|
self.assertEqual(e1.source, e2.source)
|
||||||
self.assertEqual(repr(e1), repr(e2))
|
self.assertEqual(e1.errors, e2.errors)
|
||||||
|
self.assertEqual(repr(e1), repr(e2))
|
||||||
e1 = configparser.ParsingError(filename='filename')
|
e1 = configparser.ParsingError(filename='filename')
|
||||||
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.source, e2.source)
|
self.assertEqual(e1.args, e2.args)
|
||||||
self.assertEqual(e1.errors, e2.errors)
|
self.assertEqual(e1.source, e2.source)
|
||||||
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.source, e2.source)
|
self.assertEqual(e1.line, e2.line)
|
||||||
self.assertEqual(e1.lineno, e2.lineno)
|
self.assertEqual(e1.source, e2.source)
|
||||||
self.assertEqual(repr(e1), repr(e2))
|
self.assertEqual(e1.lineno, e2.lineno)
|
||||||
|
self.assertEqual(repr(e1), repr(e2))
|
||||||
|
|
||||||
|
|
||||||
class InlineCommentStrippingTestCase(unittest.TestCase):
|
class InlineCommentStrippingTestCase(unittest.TestCase):
|
||||||
|
|
|
@ -2406,54 +2406,55 @@ class PythonAPItests(unittest.TestCase):
|
||||||
self.assertNotIsInstance(Decimal(0), numbers.Real)
|
self.assertNotIsInstance(Decimal(0), numbers.Real)
|
||||||
|
|
||||||
def test_pickle(self):
|
def test_pickle(self):
|
||||||
Decimal = self.decimal.Decimal
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
Decimal = self.decimal.Decimal
|
||||||
|
|
||||||
savedecimal = sys.modules['decimal']
|
savedecimal = sys.modules['decimal']
|
||||||
|
|
||||||
# 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)
|
||||||
|
|
||||||
if C:
|
if C:
|
||||||
# Test interchangeability
|
# Test interchangeability
|
||||||
x = C.Decimal('-3.123e81723')
|
x = C.Decimal('-3.123e81723')
|
||||||
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)
|
||||||
self.assertEqual(r, x)
|
self.assertEqual(r, x)
|
||||||
|
|
||||||
x = C.Decimal('-3.123e81723').as_tuple()
|
x = C.Decimal('-3.123e81723').as_tuple()
|
||||||
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)
|
||||||
self.assertEqual(r, x)
|
self.assertEqual(r, x)
|
||||||
|
|
||||||
sys.modules['decimal'] = savedecimal
|
sys.modules['decimal'] = savedecimal
|
||||||
|
|
||||||
def test_int(self):
|
def test_int(self):
|
||||||
Decimal = self.decimal.Decimal
|
Decimal = self.decimal.Decimal
|
||||||
|
@ -2777,63 +2778,64 @@ class ContextAPItests(unittest.TestCase):
|
||||||
|
|
||||||
def test_pickle(self):
|
def test_pickle(self):
|
||||||
|
|
||||||
Context = self.decimal.Context
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
Context = self.decimal.Context
|
||||||
|
|
||||||
savedecimal = sys.modules['decimal']
|
savedecimal = sys.modules['decimal']
|
||||||
|
|
||||||
# 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)
|
||||||
self.assertEqual(c.Emax, e.Emax)
|
self.assertEqual(c.Emax, e.Emax)
|
||||||
self.assertEqual(c.rounding, e.rounding)
|
self.assertEqual(c.rounding, e.rounding)
|
||||||
self.assertEqual(c.capitals, e.capitals)
|
self.assertEqual(c.capitals, e.capitals)
|
||||||
self.assertEqual(c.clamp, e.clamp)
|
self.assertEqual(c.clamp, e.clamp)
|
||||||
self.assertEqual(c.flags, e.flags)
|
self.assertEqual(c.flags, e.flags)
|
||||||
self.assertEqual(c.traps, e.traps)
|
self.assertEqual(c.traps, e.traps)
|
||||||
|
|
||||||
# Test interchangeability
|
# Test interchangeability
|
||||||
combinations = [(C, P), (P, C)] if C else [(P, P)]
|
combinations = [(C, P), (P, C)] if C else [(P, P)]
|
||||||
for dumper, loader in combinations:
|
for dumper, loader in combinations:
|
||||||
for ri, _ in enumerate(RoundingModes):
|
for ri, _ in enumerate(RoundingModes):
|
||||||
for fi, _ in enumerate(OrderedSignals[dumper]):
|
for fi, _ in enumerate(OrderedSignals[dumper]):
|
||||||
for ti, _ in enumerate(OrderedSignals[dumper]):
|
for ti, _ in enumerate(OrderedSignals[dumper]):
|
||||||
|
|
||||||
prec = random.randrange(1, 100)
|
prec = random.randrange(1, 100)
|
||||||
emin = random.randrange(-100, 0)
|
emin = random.randrange(-100, 0)
|
||||||
emax = random.randrange(1, 100)
|
emax = random.randrange(1, 100)
|
||||||
caps = random.randrange(2)
|
caps = random.randrange(2)
|
||||||
clamp = random.randrange(2)
|
clamp = random.randrange(2)
|
||||||
|
|
||||||
# One module dumps
|
# One module dumps
|
||||||
sys.modules['decimal'] = dumper
|
sys.modules['decimal'] = dumper
|
||||||
c = dumper.Context(
|
c = dumper.Context(
|
||||||
prec=prec, Emin=emin, Emax=emax,
|
prec=prec, Emin=emin, Emax=emax,
|
||||||
rounding=RoundingModes[ri],
|
rounding=RoundingModes[ri],
|
||||||
capitals=caps, clamp=clamp,
|
capitals=caps, clamp=clamp,
|
||||||
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
|
||||||
d = pickle.loads(s)
|
d = pickle.loads(s)
|
||||||
self.assertIsInstance(d, loader.Context)
|
self.assertIsInstance(d, loader.Context)
|
||||||
|
|
||||||
self.assertEqual(d.prec, prec)
|
self.assertEqual(d.prec, prec)
|
||||||
self.assertEqual(d.Emin, emin)
|
self.assertEqual(d.Emin, emin)
|
||||||
self.assertEqual(d.Emax, emax)
|
self.assertEqual(d.Emax, emax)
|
||||||
self.assertEqual(d.rounding, RoundingModes[ri])
|
self.assertEqual(d.rounding, RoundingModes[ri])
|
||||||
self.assertEqual(d.capitals, caps)
|
self.assertEqual(d.capitals, caps)
|
||||||
self.assertEqual(d.clamp, clamp)
|
self.assertEqual(d.clamp, clamp)
|
||||||
assert_signals(self, d, 'flags', OrderedSignals[loader][:fi])
|
assert_signals(self, d, 'flags', OrderedSignals[loader][:fi])
|
||||||
assert_signals(self, d, 'traps', OrderedSignals[loader][:ti])
|
assert_signals(self, d, 'traps', OrderedSignals[loader][:ti])
|
||||||
|
|
||||||
sys.modules['decimal'] = savedecimal
|
sys.modules['decimal'] = savedecimal
|
||||||
|
|
||||||
def test_equality_with_other_types(self):
|
def test_equality_with_other_types(self):
|
||||||
Decimal = self.decimal.Decimal
|
Decimal = self.decimal.Decimal
|
||||||
|
|
|
@ -474,16 +474,17 @@ class TestBasic(unittest.TestCase):
|
||||||
|
|
||||||
def test_iterator_pickle(self):
|
def test_iterator_pickle(self):
|
||||||
data = deque(range(200))
|
data = deque(range(200))
|
||||||
it = itorg = iter(data)
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
d = pickle.dumps(it)
|
it = itorg = iter(data)
|
||||||
it = pickle.loads(d)
|
d = pickle.dumps(it, proto)
|
||||||
self.assertEqual(type(itorg), type(it))
|
it = pickle.loads(d)
|
||||||
self.assertEqual(list(it), list(data))
|
self.assertEqual(type(itorg), type(it))
|
||||||
|
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):
|
||||||
mut = [10]
|
mut = [10]
|
||||||
|
@ -614,11 +615,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)
|
||||||
|
|
||||||
|
@ -630,11 +632,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')
|
||||||
|
|
|
@ -837,57 +837,60 @@ class DictTest(unittest.TestCase):
|
||||||
self._tracked(MyDict())
|
self._tracked(MyDict())
|
||||||
|
|
||||||
def test_iterator_pickling(self):
|
def test_iterator_pickling(self):
|
||||||
data = {1:"a", 2:"b", 3:"c"}
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
it = iter(data)
|
data = {1:"a", 2:"b", 3:"c"}
|
||||||
d = pickle.dumps(it)
|
it = iter(data)
|
||||||
it = pickle.loads(d)
|
d = pickle.dumps(it, proto)
|
||||||
self.assertEqual(sorted(it), sorted(data))
|
it = pickle.loads(d)
|
||||||
|
self.assertEqual(sorted(it), sorted(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)
|
||||||
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):
|
||||||
data = {1:"a", 2:"b", 3:"c"}
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
# dictviews aren't picklable, only their iterators
|
data = {1:"a", 2:"b", 3:"c"}
|
||||||
itorg = iter(data.items())
|
# dictviews aren't picklable, only their iterators
|
||||||
d = pickle.dumps(itorg)
|
itorg = iter(data.items())
|
||||||
it = pickle.loads(d)
|
d = pickle.dumps(itorg, proto)
|
||||||
# note that the type of type of the unpickled iterator
|
it = pickle.loads(d)
|
||||||
# is not necessarily the same as the original. It is
|
# note that the type of type of the unpickled iterator
|
||||||
# merely an object supporting the iterator protocol, yielding
|
# is not necessarily the same as the original. It is
|
||||||
# the same objects as the original one.
|
# merely an object supporting the iterator protocol, yielding
|
||||||
# self.assertEqual(type(itorg), type(it))
|
# the same objects as the original one.
|
||||||
self.assertTrue(isinstance(it, collections.abc.Iterator))
|
# self.assertEqual(type(itorg), type(it))
|
||||||
self.assertEqual(dict(it), data)
|
self.assertIsInstance(it, collections.abc.Iterator)
|
||||||
|
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):
|
||||||
data = {1:"a", 2:"b", 3:"c"}
|
for proto in range(pickle.HIGHEST_PROTOCOL):
|
||||||
# data.values() isn't picklable, only its iterator
|
data = {1:"a", 2:"b", 3:"c"}
|
||||||
it = iter(data.values())
|
# data.values() isn't picklable, only its iterator
|
||||||
d = pickle.dumps(it)
|
it = iter(data.values())
|
||||||
it = pickle.loads(d)
|
d = pickle.dumps(it, proto)
|
||||||
self.assertEqual(sorted(list(it)), sorted(list(data.values())))
|
it = pickle.loads(d)
|
||||||
|
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())))
|
||||||
|
|
||||||
def test_instance_dict_getattr_str_subclass(self):
|
def test_instance_dict_getattr_str_subclass(self):
|
||||||
class Foo:
|
class Foo:
|
||||||
|
|
|
@ -30,9 +30,10 @@ 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):
|
||||||
h = pickle.loads(p)
|
p = pickle.dumps(header, proto)
|
||||||
self.assertEqual(str(h), str(header))
|
h = pickle.loads(p)
|
||||||
|
self.assertEqual(str(h), str(header))
|
||||||
|
|
||||||
|
|
||||||
@parameterize
|
@parameterize
|
||||||
|
@ -65,9 +66,10 @@ 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):
|
||||||
msg2 = pickle.loads(p)
|
p = pickle.dumps(msg, proto)
|
||||||
self.assertEqual(msg2.as_string(), msg.as_string())
|
msg2 = pickle.loads(p)
|
||||||
|
self.assertEqual(msg2.as_string(), msg.as_string())
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
@ -66,20 +66,21 @@ 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):
|
||||||
it = pickle.loads(d)
|
d = pickle.dumps(itorg, proto)
|
||||||
self.assertEqual(type(itorg), type(it))
|
it = pickle.loads(d)
|
||||||
self.assertEqual(list(it), seq)
|
self.assertEqual(type(itorg), type(it))
|
||||||
|
self.assertEqual(list(it), seq)
|
||||||
|
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
try:
|
try:
|
||||||
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:])
|
||||||
|
|
||||||
class EnumerateTestCase(unittest.TestCase, PickleTest):
|
class EnumerateTestCase(unittest.TestCase, PickleTest):
|
||||||
|
|
||||||
|
|
|
@ -182,8 +182,9 @@ 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):
|
||||||
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):
|
||||||
|
|
|
@ -76,22 +76,23 @@ 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):
|
||||||
it = pickle.loads(d)
|
d = pickle.dumps(itorg, proto)
|
||||||
# Cannot assert type equality because dict iterators unpickle as list
|
it = pickle.loads(d)
|
||||||
# iterators.
|
# Cannot assert type equality because dict iterators unpickle as list
|
||||||
# self.assertEqual(type(itorg), type(it))
|
# iterators.
|
||||||
self.assertTrue(isinstance(it, collections.abc.Iterator))
|
# self.assertEqual(type(itorg), type(it))
|
||||||
self.assertEqual(list(it), seq)
|
self.assertTrue(isinstance(it, collections.abc.Iterator))
|
||||||
|
self.assertEqual(list(it), seq)
|
||||||
|
|
||||||
it = pickle.loads(d)
|
it = pickle.loads(d)
|
||||||
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:])
|
||||||
|
|
||||||
# Test basic use of iter() function
|
# Test basic use of iter() function
|
||||||
def test_iter_basic(self):
|
def test_iter_basic(self):
|
||||||
|
|
|
@ -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):
|
||||||
next(c)
|
self.assertEqual(take(10, pickle.loads(pickle.dumps(c, proto))),
|
||||||
self.assertEqual(take(10, pickle.loads(pickle.dumps(c))), list('cabcabcabc'))
|
list('bcabcabcab'))
|
||||||
self.pickletest(cycle('abc'))
|
next(c)
|
||||||
|
self.assertEqual(take(10, pickle.loads(pickle.dumps(c, proto))),
|
||||||
|
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,12 +618,13 @@ class TestBasicOps(unittest.TestCase):
|
||||||
self.assertEqual(s, dup)
|
self.assertEqual(s, dup)
|
||||||
|
|
||||||
# Check normal pickled
|
# Check normal pickled
|
||||||
dup = []
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
for k, g in pickle.loads(pickle.dumps(groupby(s, testR))):
|
dup = []
|
||||||
for elem in g:
|
for k, g in pickle.loads(pickle.dumps(groupby(s, testR), proto)):
|
||||||
self.assertEqual(k, elem[0])
|
for elem in g:
|
||||||
dup.append(elem)
|
self.assertEqual(k, elem[0])
|
||||||
self.assertEqual(s, dup)
|
dup.append(elem)
|
||||||
|
self.assertEqual(s, dup)
|
||||||
|
|
||||||
# Check nested case
|
# Check nested case
|
||||||
dup = []
|
dup = []
|
||||||
|
@ -622,14 +637,15 @@ class TestBasicOps(unittest.TestCase):
|
||||||
self.assertEqual(s, dup)
|
self.assertEqual(s, dup)
|
||||||
|
|
||||||
# Check nested and pickled
|
# Check nested and pickled
|
||||||
dup = []
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
for k, g in pickle.loads(pickle.dumps(groupby(s, testR))):
|
dup = []
|
||||||
for ik, ig in pickle.loads(pickle.dumps(groupby(g, testR2))):
|
for k, g in pickle.loads(pickle.dumps(groupby(s, testR), proto)):
|
||||||
for elem in ig:
|
for ik, ig in pickle.loads(pickle.dumps(groupby(g, testR2), proto)):
|
||||||
self.assertEqual(k, elem[0])
|
for elem in ig:
|
||||||
self.assertEqual(ik, elem[2])
|
self.assertEqual(k, elem[0])
|
||||||
dup.append(elem)
|
self.assertEqual(ik, elem[2])
|
||||||
self.assertEqual(s, dup)
|
dup.append(elem)
|
||||||
|
self.assertEqual(s, dup)
|
||||||
|
|
||||||
|
|
||||||
# Check case where inner iterator is not used
|
# Check case where inner iterator is not used
|
||||||
|
@ -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)
|
||||||
c = filter(isEven, range(6))
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
self.assertEqual(list(pickle.loads(pickle.dumps(c))), ans)
|
c = filter(isEven, range(6))
|
||||||
next(c)
|
self.assertEqual(list(pickle.loads(pickle.dumps(c, proto))), ans)
|
||||||
self.assertEqual(list(pickle.loads(pickle.dumps(c))), ans[1:])
|
next(c)
|
||||||
c = filter(isEven, range(6))
|
self.assertEqual(list(pickle.loads(pickle.dumps(c, proto))), ans[1:])
|
||||||
self.pickletest(c)
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
c = filter(isEven, range(6))
|
||||||
|
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):
|
||||||
self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)])
|
ans = [(x,y) for x, y in pickle.loads(pickle.dumps(zip('abc',count()), proto))]
|
||||||
|
self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)])
|
||||||
|
|
||||||
testIntermediate = zip('abc',count())
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
next(testIntermediate)
|
testIntermediate = zip('abc',count())
|
||||||
ans = [(x,y) for x, y in pickle.loads(pickle.dumps(testIntermediate))]
|
next(testIntermediate)
|
||||||
self.assertEqual(ans, [('b', 1), ('c', 2)])
|
ans = [(x,y) for x, y in pickle.loads(pickle.dumps(testIntermediate, proto))]
|
||||||
|
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)
|
||||||
|
|
||||||
c = map(tupleize, 'abc', count())
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
self.pickletest(c)
|
c = map(tupleize, 'abc', count())
|
||||||
|
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)
|
||||||
|
|
||||||
c = starmap(operator.pow, zip(range(3), range(1,7)))
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
self.pickletest(c)
|
c = starmap(operator.pow, zip(range(3), range(1,7)))
|
||||||
|
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):
|
||||||
a, b = tee('abc')
|
self.pickletest(proto, iter(tee('abc')))
|
||||||
self.pickletest(a, compare=ans)
|
a, b = tee('abc')
|
||||||
self.pickletest(b, compare=ans)
|
self.pickletest(proto, a, 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,29 +74,31 @@ 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])
|
||||||
it = itorg = iter(data)
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
d = pickle.dumps(it)
|
it = itorg = iter(data)
|
||||||
it = pickle.loads(d)
|
d = pickle.dumps(it, proto)
|
||||||
self.assertEqual(type(itorg), type(it))
|
it = pickle.loads(d)
|
||||||
self.assertEqual(self.type2test(it), self.type2test(data))
|
self.assertEqual(type(itorg), type(it))
|
||||||
|
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])
|
||||||
it = itorg = reversed(data)
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
d = pickle.dumps(it)
|
it = itorg = reversed(data)
|
||||||
it = pickle.loads(d)
|
d = pickle.dumps(it, proto)
|
||||||
self.assertEqual(type(itorg), type(it))
|
it = pickle.loads(d)
|
||||||
self.assertEqual(self.type2test(it), self.type2test(reversed(data)))
|
self.assertEqual(type(itorg), type(it))
|
||||||
|
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):
|
||||||
# Issue 8847: In the PGO build, the MSVC linker's COMDAT folding
|
# Issue 8847: In the PGO build, the MSVC linker's COMDAT folding
|
||||||
|
|
|
@ -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):
|
||||||
with self.assertRaises(TypeError):
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
pickle.dumps(LZMACompressor())
|
with self.assertRaises(TypeError):
|
||||||
with self.assertRaises(TypeError):
|
pickle.dumps(LZMACompressor(), proto)
|
||||||
pickle.dumps(LZMADecompressor())
|
with self.assertRaises(TypeError):
|
||||||
|
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,43 +1468,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()
|
||||||
|
|
|
@ -265,10 +265,13 @@ 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)
|
||||||
unpickled = pickle.loads(p)
|
self.assertIn(b'stat_result', p)
|
||||||
self.assertEqual(result, unpickled)
|
if proto < 4:
|
||||||
|
self.assertIn(b'cos\nstat_result\n', p)
|
||||||
|
unpickled = pickle.loads(p)
|
||||||
|
self.assertEqual(result, unpickled)
|
||||||
|
|
||||||
@unittest.skipUnless(hasattr(os, 'statvfs'), 'test needs os.statvfs()')
|
@unittest.skipUnless(hasattr(os, 'statvfs'), 'test needs os.statvfs()')
|
||||||
def test_statvfs_attributes(self):
|
def test_statvfs_attributes(self):
|
||||||
|
@ -324,10 +327,13 @@ 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)
|
||||||
unpickled = pickle.loads(p)
|
self.assertIn(b'statvfs_result', p)
|
||||||
self.assertEqual(result, unpickled)
|
if proto < 4:
|
||||||
|
self.assertIn(b'cos\nstatvfs_result\n', p)
|
||||||
|
unpickled = pickle.loads(p)
|
||||||
|
self.assertEqual(result, unpickled)
|
||||||
|
|
||||||
def test_utime_dir(self):
|
def test_utime_dir(self):
|
||||||
delta = 1000000
|
delta = 1000000
|
||||||
|
|
|
@ -159,11 +159,12 @@ 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):
|
||||||
origseq = [self.gen.random() for i in range(10)]
|
state = pickle.dumps(self.gen, proto)
|
||||||
newgen = pickle.loads(state)
|
origseq = [self.gen.random() for i in range(10)]
|
||||||
restoredseq = [newgen.random() for i in range(10)]
|
newgen = pickle.loads(state)
|
||||||
self.assertEqual(origseq, restoredseq)
|
restoredseq = [newgen.random() for i in range(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
|
||||||
|
@ -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,33 +376,35 @@ 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):
|
||||||
r = range(2**65, 2**65+2)
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
i = iter(r)
|
r = range(2**65, 2**65+2)
|
||||||
while True:
|
i = iter(r)
|
||||||
r = next(i)
|
while True:
|
||||||
if r == 2**65+1:
|
r = next(i)
|
||||||
break
|
if r == 2**65+1:
|
||||||
d = pickle.dumps(i)
|
break
|
||||||
i2 = pickle.loads(d)
|
d = pickle.dumps(i, proto)
|
||||||
self.assertEqual(list(i), [])
|
i2 = pickle.loads(d)
|
||||||
self.assertEqual(list(i2), [])
|
self.assertEqual(list(i), [])
|
||||||
|
self.assertEqual(list(i2), [])
|
||||||
|
|
||||||
def test_large_exhausted_iterator_pickling(self):
|
def test_large_exhausted_iterator_pickling(self):
|
||||||
r = range(20)
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
i = iter(r)
|
r = range(20)
|
||||||
while True:
|
i = iter(r)
|
||||||
r = next(i)
|
while True:
|
||||||
if r == 19:
|
r = next(i)
|
||||||
break
|
if r == 19:
|
||||||
d = pickle.dumps(i)
|
break
|
||||||
i2 = pickle.loads(d)
|
d = pickle.dumps(i, proto)
|
||||||
self.assertEqual(list(i), [])
|
i2 = pickle.loads(d)
|
||||||
self.assertEqual(list(i2), [])
|
self.assertEqual(list(i), [])
|
||||||
|
self.assertEqual(list(i2), [])
|
||||||
|
|
||||||
def test_odd_bug(self):
|
def test_odd_bug(self):
|
||||||
# This used to raise a "SystemError: NULL result without error"
|
# This used to raise a "SystemError: NULL result without error"
|
||||||
|
|
|
@ -231,29 +231,30 @@ 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):
|
||||||
itorg = iter(self.s)
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
data = self.thetype(self.s)
|
itorg = iter(self.s)
|
||||||
d = pickle.dumps(itorg)
|
data = self.thetype(self.s)
|
||||||
it = pickle.loads(d)
|
d = pickle.dumps(itorg, proto)
|
||||||
# Set iterators unpickle as list iterators due to the
|
it = pickle.loads(d)
|
||||||
# undefined order of set items.
|
# Set iterators unpickle as list iterators due to the
|
||||||
# self.assertEqual(type(itorg), type(it))
|
# undefined order of set items.
|
||||||
self.assertTrue(isinstance(it, collections.abc.Iterator))
|
# self.assertEqual(type(itorg), type(it))
|
||||||
self.assertEqual(self.thetype(it), data)
|
self.assertIsInstance(it, collections.abc.Iterator)
|
||||||
|
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,)))
|
||||||
|
|
||||||
def test_deepcopy(self):
|
def test_deepcopy(self):
|
||||||
class Tracer:
|
class Tracer:
|
||||||
|
@ -851,10 +852,11 @@ 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):
|
||||||
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))
|
||||||
|
|
||||||
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
|
@ -169,29 +169,31 @@ 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])
|
||||||
itorg = iter(data)
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
d = pickle.dumps(itorg)
|
itorg = iter(data)
|
||||||
it = pickle.loads(d)
|
d = pickle.dumps(itorg, proto)
|
||||||
self.assertEqual(type(itorg), type(it))
|
it = pickle.loads(d)
|
||||||
self.assertEqual(self.type2test(it), self.type2test(data))
|
self.assertEqual(type(itorg), type(it))
|
||||||
|
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])
|
||||||
itorg = reversed(data)
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
d = pickle.dumps(itorg)
|
itorg = reversed(data)
|
||||||
it = pickle.loads(d)
|
d = pickle.dumps(itorg, proto)
|
||||||
self.assertEqual(type(itorg), type(it))
|
it = pickle.loads(d)
|
||||||
self.assertEqual(self.type2test(it), self.type2test(reversed(data)))
|
self.assertEqual(type(itorg), type(it))
|
||||||
|
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):
|
||||||
# Issue 8847: In the PGO build, the MSVC linker's COMDAT folding
|
# Issue 8847: In the PGO build, the MSVC linker's COMDAT folding
|
||||||
|
|
|
@ -84,18 +84,19 @@ 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.
|
||||||
|
|
||||||
# Empty NodeList.
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
node_list = NodeList()
|
# Empty NodeList.
|
||||||
pickled = pickle.dumps(node_list)
|
node_list = NodeList()
|
||||||
unpickled = pickle.loads(pickled)
|
pickled = pickle.dumps(node_list, proto)
|
||||||
self.assertEqual(unpickled, node_list)
|
unpickled = pickle.loads(pickled)
|
||||||
|
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)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -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,33 +1677,36 @@ 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 dumper, loader in product(self.modules, repeat=2):
|
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
|
||||||
e = dumper.Element('foo', bar=42)
|
for dumper, loader in product(self.modules, repeat=2):
|
||||||
e.text = "text goes here"
|
e = dumper.Element('foo', bar=42)
|
||||||
e.tail = "opposite of head"
|
e.text = "text goes here"
|
||||||
dumper.SubElement(e, 'child').append(dumper.Element('grandchild'))
|
e.tail = "opposite of head"
|
||||||
e.append(dumper.Element('child'))
|
dumper.SubElement(e, 'child').append(dumper.Element('grandchild'))
|
||||||
e.findall('.//grandchild')[0].set('attr', 'other value')
|
e.append(dumper.Element('child'))
|
||||||
|
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)
|
||||||
self.assertEqual(len(e2), 2)
|
self.assertEqual(len(e2), 2)
|
||||||
self.assertEqualElements(e, e2)
|
self.assertEqualElements(e, e2)
|
||||||
|
|
||||||
def test_pickle_issue18997(self):
|
def test_pickle_issue18997(self):
|
||||||
for dumper, loader in product(self.modules, repeat=2):
|
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
|
||||||
XMLTEXT = """<?xml version="1.0"?>
|
for dumper, loader in product(self.modules, repeat=2):
|
||||||
<group><dogs>4</dogs>
|
XMLTEXT = """<?xml version="1.0"?>
|
||||||
</group>"""
|
<group><dogs>4</dogs>
|
||||||
e1 = dumper.fromstring(XMLTEXT)
|
</group>"""
|
||||||
if hasattr(e1, '__getstate__'):
|
e1 = dumper.fromstring(XMLTEXT)
|
||||||
self.assertEqual(e1.__getstate__()['tag'], 'group')
|
if hasattr(e1, '__getstate__'):
|
||||||
e2 = self.pickleRoundTrip(e1, 'xml.etree.ElementTree', dumper, loader)
|
self.assertEqual(e1.__getstate__()['tag'], 'group')
|
||||||
self.assertEqual(e2.tag, 'group')
|
e2 = self.pickleRoundTrip(e1, 'xml.etree.ElementTree',
|
||||||
self.assertEqual(e2[0].tag, 'dogs')
|
dumper, loader, proto)
|
||||||
|
self.assertEqual(e2.tag, 'group')
|
||||||
|
self.assertEqual(e2[0].tag, 'dogs')
|
||||||
|
|
||||||
|
|
||||||
class ElementTreeTypeTest(unittest.TestCase):
|
class ElementTreeTypeTest(unittest.TestCase):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue