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

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

Issue 16510: Using appropriate checks in tests
Patch Set: Created 5 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 | « no previous file | Lib/test/test_abc.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import copyreg 1 import copyreg
2 import io 2 import io
3 import pickle 3 import pickle
4 import pickletools 4 import pickletools
5 import random 5 import random
6 import struct 6 import struct
7 import sys 7 import sys
8 import unittest 8 import unittest
9 import weakref 9 import weakref
10 from http.cookies import SimpleCookie 10 from http.cookies import SimpleCookie
(...skipping 627 matching lines...) Expand 10 before | Expand all | Expand 10 after
638 self.assertEqual(expected, got) 638 self.assertEqual(expected, got)
639 639
640 def test_recursive_list(self): 640 def test_recursive_list(self):
641 l = [] 641 l = []
642 l.append(l) 642 l.append(l)
643 for proto in protocols: 643 for proto in protocols:
644 s = self.dumps(l, proto) 644 s = self.dumps(l, proto)
645 x = self.loads(s) 645 x = self.loads(s)
646 self.assertIsInstance(x, list) 646 self.assertIsInstance(x, list)
647 self.assertEqual(len(x), 1) 647 self.assertEqual(len(x), 1)
648 self.assertTrue(x is x[0]) 648 self.assertIs(x, x[0])
649 649
650 def test_recursive_tuple(self): 650 def test_recursive_tuple(self):
651 t = ([],) 651 t = ([],)
652 t[0].append(t) 652 t[0].append(t)
653 for proto in protocols: 653 for proto in protocols:
654 s = self.dumps(t, proto) 654 s = self.dumps(t, proto)
655 x = self.loads(s) 655 x = self.loads(s)
656 self.assertIsInstance(x, tuple) 656 self.assertIsInstance(x, tuple)
657 self.assertEqual(len(x), 1) 657 self.assertEqual(len(x), 1)
658 self.assertEqual(len(x[0]), 1) 658 self.assertEqual(len(x[0]), 1)
659 self.assertTrue(x is x[0][0]) 659 self.assertIs(x, x[0][0])
660 660
661 def test_recursive_dict(self): 661 def test_recursive_dict(self):
662 d = {} 662 d = {}
663 d[1] = d 663 d[1] = d
664 for proto in protocols: 664 for proto in protocols:
665 s = self.dumps(d, proto) 665 s = self.dumps(d, proto)
666 x = self.loads(s) 666 x = self.loads(s)
667 self.assertIsInstance(x, dict) 667 self.assertIsInstance(x, dict)
668 self.assertEqual(list(x.keys()), [1]) 668 self.assertEqual(list(x.keys()), [1])
669 self.assertTrue(x[1] is x) 669 self.assertIs(x[1], x)
670 670
671 def test_recursive_set(self): 671 def test_recursive_set(self):
672 h = H() 672 h = H()
673 y = set({h}) 673 y = set({h})
674 h.attr = y 674 h.attr = y
675 for proto in protocols: 675 for proto in protocols:
676 s = self.dumps(y, proto) 676 s = self.dumps(y, proto)
677 x = self.loads(s) 677 x = self.loads(s)
678 self.assertIsInstance(x, set) 678 self.assertIsInstance(x, set)
679 self.assertIs(list(x)[0].attr, x) 679 self.assertIs(list(x)[0].attr, x)
(...skipping 26 matching lines...) Expand all
706 i = C() 706 i = C()
707 i.attr = d 707 i.attr = d
708 l.append(i) 708 l.append(i)
709 for proto in protocols: 709 for proto in protocols:
710 s = self.dumps(l, proto) 710 s = self.dumps(l, proto)
711 x = self.loads(s) 711 x = self.loads(s)
712 self.assertIsInstance(x, list) 712 self.assertIsInstance(x, list)
713 self.assertEqual(len(x), 1) 713 self.assertEqual(len(x), 1)
714 self.assertEqual(dir(x[0]), dir(i)) 714 self.assertEqual(dir(x[0]), dir(i))
715 self.assertEqual(list(x[0].attr.keys()), [1]) 715 self.assertEqual(list(x[0].attr.keys()), [1])
716 self.assertTrue(x[0].attr[1] is x) 716 self.assertIs(x[0].attr[1], x)
717 717
718 def test_get(self): 718 def test_get(self):
719 self.assertRaises(KeyError, self.loads, b'g0\np0') 719 self.assertRaises(KeyError, self.loads, b'g0\np0')
720 self.assert_is_copy([(100,), (100,)], 720 self.assert_is_copy([(100,), (100,)],
721 self.loads(b'((Kdtp0\nh\x00l.))')) 721 self.loads(b'((Kdtp0\nh\x00l.))'))
722 722
723 def test_unicode(self): 723 def test_unicode(self):
724 endcases = ['', '<\\u>', '<\\\u1234>', '<\n>', 724 endcases = ['', '<\\u>', '<\\\u1234>', '<\n>',
725 '<\\>', '<\\\U00012345>', 725 '<\\>', '<\\\U00012345>',
726 # surrogates 726 # surrogates
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
975 975
976 (0, False): pickle.INT, 976 (0, False): pickle.INT,
977 (1, False): pickle.INT, 977 (1, False): pickle.INT,
978 (2, False): pickle.NEWFALSE, 978 (2, False): pickle.NEWFALSE,
979 (3, False): pickle.NEWFALSE, 979 (3, False): pickle.NEWFALSE,
980 } 980 }
981 for proto in protocols: 981 for proto in protocols:
982 for x in None, False, True: 982 for x in None, False, True:
983 s = self.dumps(x, proto) 983 s = self.dumps(x, proto)
984 y = self.loads(s) 984 y = self.loads(s)
985 self.assertTrue(x is y, (proto, x, s, y)) 985 self.assertIs(x, y, (proto, x, s, y))
986 expected = expected_opcode[min(proto, 3), x] 986 expected = expected_opcode[min(proto, 3), x]
987 self.assertTrue(opcode_in_pickle(expected, s)) 987 self.assertEqual(opcode_in_pickle(expected, s), True)
988 988
989 def test_newobj_tuple(self): 989 def test_newobj_tuple(self):
990 x = MyTuple([1, 2, 3]) 990 x = MyTuple([1, 2, 3])
991 x.foo = 42 991 x.foo = 42
992 x.bar = "hello" 992 x.bar = "hello"
993 for proto in protocols: 993 for proto in protocols:
994 s = self.dumps(x, proto) 994 s = self.dumps(x, proto)
995 y = self.loads(s) 995 y = self.loads(s)
996 self.assert_is_copy(x, y) 996 self.assert_is_copy(x, y)
997 997
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
1095 n = 2500 # expect at least two chunks when proto > 0 1095 n = 2500 # expect at least two chunks when proto > 0
1096 x = list(range(n)) 1096 x = list(range(n))
1097 for proto in protocols: 1097 for proto in protocols:
1098 s = self.dumps(x, proto) 1098 s = self.dumps(x, proto)
1099 y = self.loads(s) 1099 y = self.loads(s)
1100 self.assert_is_copy(x, y) 1100 self.assert_is_copy(x, y)
1101 num_appends = count_opcode(pickle.APPENDS, s) 1101 num_appends = count_opcode(pickle.APPENDS, s)
1102 if proto == 0: 1102 if proto == 0:
1103 self.assertEqual(num_appends, 0) 1103 self.assertEqual(num_appends, 0)
1104 else: 1104 else:
1105 self.assertTrue(num_appends >= 2) 1105 self.assertGreaterEqual(num_appends, 2)
1106 1106
1107 def test_dict_chunking(self): 1107 def test_dict_chunking(self):
1108 n = 10 # too small to chunk 1108 n = 10 # too small to chunk
1109 x = dict.fromkeys(range(n)) 1109 x = dict.fromkeys(range(n))
1110 for proto in protocols: 1110 for proto in protocols:
1111 s = self.dumps(x, proto) 1111 s = self.dumps(x, proto)
1112 self.assertIsInstance(s, bytes_types) 1112 self.assertIsInstance(s, bytes_types)
1113 y = self.loads(s) 1113 y = self.loads(s)
1114 self.assert_is_copy(x, y) 1114 self.assert_is_copy(x, y)
1115 num_setitems = count_opcode(pickle.SETITEMS, s) 1115 num_setitems = count_opcode(pickle.SETITEMS, s)
1116 self.assertEqual(num_setitems, proto > 0) 1116 self.assertEqual(num_setitems, proto > 0)
1117 1117
1118 n = 2500 # expect at least two chunks when proto > 0 1118 n = 2500 # expect at least two chunks when proto > 0
1119 x = dict.fromkeys(range(n)) 1119 x = dict.fromkeys(range(n))
1120 for proto in protocols: 1120 for proto in protocols:
1121 s = self.dumps(x, proto) 1121 s = self.dumps(x, proto)
1122 y = self.loads(s) 1122 y = self.loads(s)
1123 self.assert_is_copy(x, y) 1123 self.assert_is_copy(x, y)
1124 num_setitems = count_opcode(pickle.SETITEMS, s) 1124 num_setitems = count_opcode(pickle.SETITEMS, s)
1125 if proto == 0: 1125 if proto == 0:
1126 self.assertEqual(num_setitems, 0) 1126 self.assertEqual(num_setitems, 0)
1127 else: 1127 else:
1128 self.assertTrue(num_setitems >= 2) 1128 self.assertGreaterEqual(num_setitems, 2)
1129 1129
1130 def test_set_chunking(self): 1130 def test_set_chunking(self):
1131 n = 10 # too small to chunk 1131 n = 10 # too small to chunk
1132 x = set(range(n)) 1132 x = set(range(n))
1133 for proto in protocols: 1133 for proto in protocols:
1134 s = self.dumps(x, proto) 1134 s = self.dumps(x, proto)
1135 y = self.loads(s) 1135 y = self.loads(s)
1136 self.assert_is_copy(x, y) 1136 self.assert_is_copy(x, y)
1137 num_additems = count_opcode(pickle.ADDITEMS, s) 1137 num_additems = count_opcode(pickle.ADDITEMS, s)
1138 if proto < 4: 1138 if proto < 4:
(...skipping 922 matching lines...) Expand 10 before | Expand all | Expand 10 after
2061 2061
2062 self.assertEqual(unpickled_data1, data) 2062 self.assertEqual(unpickled_data1, data)
2063 2063
2064 primed = self.unpickler_class(io.BytesIO(primed_pickled)) 2064 primed = self.unpickler_class(io.BytesIO(primed_pickled))
2065 primed.memo = unpickler.memo 2065 primed.memo = unpickler.memo
2066 unpickled_data2 = primed.load() 2066 unpickled_data2 = primed.load()
2067 2067
2068 primed.memo.clear() 2068 primed.memo.clear()
2069 2069
2070 self.assertEqual(unpickled_data2, data) 2070 self.assertEqual(unpickled_data2, data)
2071 self.assertTrue(unpickled_data2 is unpickled_data1) 2071 self.assertIs(unpickled_data2, unpickled_data1)
2072 2072
2073 def test_reusing_unpickler_objects(self): 2073 def test_reusing_unpickler_objects(self):
2074 data1 = ["abcdefg", "abcdefg", 44] 2074 data1 = ["abcdefg", "abcdefg", 44]
2075 f = io.BytesIO() 2075 f = io.BytesIO()
2076 pickler = self.pickler_class(f) 2076 pickler = self.pickler_class(f)
2077 pickler.dump(data1) 2077 pickler.dump(data1)
2078 pickled1 = f.getvalue() 2078 pickled1 = f.getvalue()
2079 2079
2080 data2 = ["abcdefg", 44, 44] 2080 data2 = ["abcdefg", 44, 44]
2081 f = io.BytesIO() 2081 f = io.BytesIO()
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
2244 for j in range(0, len(p), 20): 2244 for j in range(0, len(p), 20):
2245 b = bytes(p[j:j+20]) 2245 b = bytes(p[j:j+20])
2246 print(" {0!r}".format(b)) 2246 print(" {0!r}".format(b))
2247 print(")") 2247 print(")")
2248 print() 2248 print()
2249 print("# Disassembly of DATA{0}".format(i)) 2249 print("# Disassembly of DATA{0}".format(i))
2250 print("DATA{0}_DIS = \"\"\"\\".format(i)) 2250 print("DATA{0}_DIS = \"\"\"\\".format(i))
2251 dis(p) 2251 dis(p)
2252 print("\"\"\"") 2252 print("\"\"\"")
2253 print() 2253 print()
OLDNEW
« no previous file with comments | « no previous file | Lib/test/test_abc.py » ('j') | no next file with comments »

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