diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2014-12-15 12:06:02 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2014-12-15 12:06:02 (GMT) |
commit | 65ee4674e290ba893509f51f192dd1c9aa3a0422 (patch) | |
tree | c1e960162df4444ff18a2ad3aff11d713ef9ea12 /Lib/test/test_collections.py | |
parent | 0e32ea1089059a15b04a40c43bfe671e38ae16e7 (diff) | |
parent | bad1257c96943dcb39584a41e1a178542479df97 (diff) | |
download | cpython-65ee4674e290ba893509f51f192dd1c9aa3a0422.zip cpython-65ee4674e290ba893509f51f192dd1c9aa3a0422.tar.gz cpython-65ee4674e290ba893509f51f192dd1c9aa3a0422.tar.bz2 |
Issue #22777: Test pickling with all protocols.
Diffstat (limited to 'Lib/test/test_collections.py')
-rw-r--r-- | Lib/test/test_collections.py | 81 |
1 files changed, 36 insertions, 45 deletions
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index db44d66..a94b5da 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -63,10 +63,17 @@ class TestChainMap(unittest.TestCase): for m1, m2 in zip(d.maps[1:], e.maps[1:]): self.assertIs(m1, m2) - for e in [pickle.loads(pickle.dumps(d)), - copy.deepcopy(d), + # check deep copies + 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)) - ]: # check deep copies + ]: self.assertEqual(d, e) self.assertEqual(d.maps, e.maps) self.assertIsNot(d, e) @@ -1163,28 +1170,21 @@ class TestCounter(unittest.TestCase): # Check that counters are copyable, deepcopyable, picklable, and #have a repr/eval round-trip 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.update(words) - for label, dup in [ - ('words.copy()', words.copy()), - ('copy.copy(words)', copy.copy(words)), - ('copy.deepcopy(words)', copy.deepcopy(words)), - ('pickle.loads(pickle.dumps(words, 0))', - pickle.loads(pickle.dumps(words, 0))), - ('pickle.loads(pickle.dumps(words, 1))', - pickle.loads(pickle.dumps(words, 1))), - ('pickle.loads(pickle.dumps(words, 2))', - pickle.loads(pickle.dumps(words, 2))), - ('pickle.loads(pickle.dumps(words, -1))', - pickle.loads(pickle.dumps(words, -1))), - ('eval(repr(words))', eval(repr(words))), - ('update_test', update_test), - ('Counter(words)', Counter(words)), - ]: - with self.subTest(label=label): - msg = "\ncopy: %s\nwords: %s" % (dup, words) - self.assertIsNot(dup, words, msg) - self.assertEqual(dup, words) + check(update_test) + check(Counter(words)) def test_copy_subclass(self): class MyCounter(Counter): @@ -1501,30 +1501,21 @@ class TestOrderedDict(unittest.TestCase): # and have a repr/eval round-trip pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] 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.update(od) - for label, dup in [ - ('od.copy()', od.copy()), - ('copy.copy(od)', copy.copy(od)), - ('copy.deepcopy(od)', copy.deepcopy(od)), - ('pickle.loads(pickle.dumps(od, 0))', - pickle.loads(pickle.dumps(od, 0))), - ('pickle.loads(pickle.dumps(od, 1))', - pickle.loads(pickle.dumps(od, 1))), - ('pickle.loads(pickle.dumps(od, 2))', - pickle.loads(pickle.dumps(od, 2))), - ('pickle.loads(pickle.dumps(od, 3))', - pickle.loads(pickle.dumps(od, 3))), - ('pickle.loads(pickle.dumps(od, -1))', - pickle.loads(pickle.dumps(od, -1))), - ('eval(repr(od))', eval(repr(od))), - ('update_test', update_test), - ('OrderedDict(od)', OrderedDict(od)), - ]: - with self.subTest(label=label): - msg = "\ncopy: %s\nod: %s" % (dup, od) - self.assertIsNot(dup, od, msg) - self.assertEqual(dup, od) + check(update_test) + check(OrderedDict(od)) def test_yaml_linkage(self): # Verify that __reduce__ is setup in a way that supports PyYAML's dump() feature. |