Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(186977)

Side by Side Diff: Lib/test/test_collections.py

Issue 17900: Recursive OrderedDict pickling (Closed)
Patch Set: Created 4 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/collections/__init__.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """Unit tests for collections.py.""" 1 """Unit tests for collections.py."""
2 2
3 import unittest, doctest, operator 3 import unittest, doctest, operator
4 from test.support import TESTFN, forget, unlink 4 from test.support import TESTFN, forget, unlink
5 import inspect 5 import inspect
6 from test import support 6 from test import support
7 from collections import namedtuple, Counter, OrderedDict, _count_elements 7 from collections import namedtuple, Counter, OrderedDict, _count_elements
8 from test import mapping_tests 8 from test import mapping_tests
9 import pickle, copy 9 import pickle, copy
10 from random import randrange, shuffle 10 from random import randrange, shuffle
(...skipping 1472 matching lines...) Expand 10 before | Expand all | Expand 10 after
1483 ('OrderedDict(od)', OrderedDict(od)), 1483 ('OrderedDict(od)', OrderedDict(od)),
1484 ]: 1484 ]:
1485 with self.subTest(label=label): 1485 with self.subTest(label=label):
1486 msg = "\ncopy: %s\nod: %s" % (dup, od) 1486 msg = "\ncopy: %s\nod: %s" % (dup, od)
1487 self.assertIsNot(dup, od, msg) 1487 self.assertIsNot(dup, od, msg)
1488 self.assertEqual(dup, od) 1488 self.assertEqual(dup, od)
1489 1489
1490 def test_yaml_linkage(self): 1490 def test_yaml_linkage(self):
1491 # Verify that __reduce__ is setup in a way that supports PyYAML's dump() feature. 1491 # Verify that __reduce__ is setup in a way that supports PyYAML's dump() feature.
1492 # In yaml, lists are native but tuples are not. 1492 # In yaml, lists are native but tuples are not.
1493 pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] 1493 pairs = [['c', 1], ['b', 2], ['a', 3], ['d', 4], ['e', 5], ['f', 6]]
1494 od = OrderedDict(pairs) 1494 od = OrderedDict(pairs)
1495 # yaml.dump(od) --> 1495 # yaml.dump(od) -->
1496 # '!!python/object/apply:__main__.OrderedDict\n- - [a, 1]\n - [b, 2]\n' 1496 # '!!python/object/apply:__main__.OrderedDict\n- - [a, 1]\n - [b, 2]\n'
1497 self.assertTrue(all(type(pair)==list for pair in od.__reduce__()[1])) 1497 for rv in (od.__reduce__(), od.__reduce_ex__(2)):
1498 self.assertEqual(len(rv), 2)
1499 self.assertEqual(len(rv[1]), 1)
1500 for i, pair in enumerate(rv[1][0]):
1501 self.assertEqual(type(pair), list, (i, pair))
1502 self.assertEqual(rv[1][0], pairs)
1498 1503
1499 def test_reduce_not_too_fat(self): 1504 def test_reduce_not_too_fat(self):
1500 # do not save instance dictionary if not needed 1505 # do not save instance dictionary if not needed
1501 pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] 1506 pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
1502 od = OrderedDict(pairs) 1507 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1503 self.assertIsNone(od.__reduce__()[2]) 1508 od = OrderedDict(pairs)
1504 od.x = 10 1509 rv = od.__reduce_ex__(proto)
1505 self.assertIsNotNone(od.__reduce__()[2]) 1510 if len(rv) > 2:
1511 self.assertIsNone(rv[2])
1512 od.x = 10
1513 rv = od.__reduce_ex__(proto)
1514 self.assertIsNotNone(rv[2])
1506 1515
1507 def test_pickle_recursive(self): 1516 def test_pickle_recursive(self):
1508 od = OrderedDict() 1517 od = OrderedDict()
1509 od[1] = od 1518 od[1] = od
1510 for proto in range(-1, pickle.HIGHEST_PROTOCOL + 1): 1519 for proto in range(3, pickle.HIGHEST_PROTOCOL + 1):
1511 dup = pickle.loads(pickle.dumps(od, proto)) 1520 dup = pickle.loads(pickle.dumps(od, proto))
1512 self.assertIsNot(dup, od) 1521 self.assertIsNot(dup, od)
1513 self.assertEqual(list(dup.keys()), [1]) 1522 self.assertEqual(list(dup.keys()), [1])
1514 self.assertIs(dup[1], dup) 1523 self.assertIs(dup[1], dup)
1515 1524
1516 def test_repr(self): 1525 def test_repr(self):
1517 od = OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f' , 6)]) 1526 od = OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f' , 6)])
1518 self.assertEqual(repr(od), 1527 self.assertEqual(repr(od),
1519 "OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f' , 6)])") 1528 "OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f' , 6)])")
1520 self.assertEqual(eval(repr(od)), od) 1529 self.assertEqual(eval(repr(od)), od)
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
1611 NamedTupleDocs = doctest.DocTestSuite(module=collections) 1620 NamedTupleDocs = doctest.DocTestSuite(module=collections)
1612 test_classes = [TestNamedTuple, NamedTupleDocs, TestOneTrickPonyABCs, 1621 test_classes = [TestNamedTuple, NamedTupleDocs, TestOneTrickPonyABCs,
1613 TestCollectionABCs, TestCounter, TestChainMap, 1622 TestCollectionABCs, TestCounter, TestChainMap,
1614 TestOrderedDict, GeneralMappingTests, SubclassMappingTests] 1623 TestOrderedDict, GeneralMappingTests, SubclassMappingTests]
1615 support.run_unittest(*test_classes) 1624 support.run_unittest(*test_classes)
1616 support.run_doctest(collections, verbose) 1625 support.run_doctest(collections, verbose)
1617 1626
1618 1627
1619 if __name__ == "__main__": 1628 if __name__ == "__main__":
1620 test_main(verbose=True) 1629 test_main(verbose=True)
OLDNEW
« no previous file with comments | « Lib/collections/__init__.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+