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

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

Issue 16510: Using appropriate checks in tests
Patch Set: Created 6 years, 9 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/test/multibytecodec_support.py ('k') | 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 io 1 import io
2 import unittest 2 import unittest
3 import pickle 3 import pickle
4 import pickletools 4 import pickletools
5 import sys 5 import sys
6 import copyreg 6 import copyreg
7 import weakref 7 import weakref
8 from http.cookies import SimpleCookie 8 from http.cookies import SimpleCookie
9 9
10 from test.support import ( 10 from test.support import (
(...skipping 535 matching lines...) Expand 10 before | Expand all | Expand 10 after
546 got = filelike.getvalue() 546 got = filelike.getvalue()
547 self.assertEqual(expected, got) 547 self.assertEqual(expected, got)
548 548
549 def test_recursive_list(self): 549 def test_recursive_list(self):
550 l = [] 550 l = []
551 l.append(l) 551 l.append(l)
552 for proto in protocols: 552 for proto in protocols:
553 s = self.dumps(l, proto) 553 s = self.dumps(l, proto)
554 x = self.loads(s) 554 x = self.loads(s)
555 self.assertEqual(len(x), 1) 555 self.assertEqual(len(x), 1)
556 self.assertTrue(x is x[0]) 556 self.assertIs(x, x[0])
557 557
558 def test_recursive_tuple(self): 558 def test_recursive_tuple(self):
559 t = ([],) 559 t = ([],)
560 t[0].append(t) 560 t[0].append(t)
561 for proto in protocols: 561 for proto in protocols:
562 s = self.dumps(t, proto) 562 s = self.dumps(t, proto)
563 x = self.loads(s) 563 x = self.loads(s)
564 self.assertEqual(len(x), 1) 564 self.assertEqual(len(x), 1)
565 self.assertEqual(len(x[0]), 1) 565 self.assertEqual(len(x[0]), 1)
566 self.assertTrue(x is x[0][0]) 566 self.assertIs(x, x[0][0])
567 567
568 def test_recursive_dict(self): 568 def test_recursive_dict(self):
569 d = {} 569 d = {}
570 d[1] = d 570 d[1] = d
571 for proto in protocols: 571 for proto in protocols:
572 s = self.dumps(d, proto) 572 s = self.dumps(d, proto)
573 x = self.loads(s) 573 x = self.loads(s)
574 self.assertEqual(list(x.keys()), [1]) 574 self.assertEqual(list(x.keys()), [1])
575 self.assertTrue(x[1] is x) 575 self.assertIs(x[1], x)
576 576
577 def test_recursive_inst(self): 577 def test_recursive_inst(self):
578 i = C() 578 i = C()
579 i.attr = i 579 i.attr = i
580 for proto in protocols: 580 for proto in protocols:
581 s = self.dumps(i, 2) 581 s = self.dumps(i, 2)
582 x = self.loads(s) 582 x = self.loads(s)
583 self.assertEqual(dir(x), dir(i)) 583 self.assertEqual(dir(x), dir(i))
584 self.assertTrue(x.attr is x) 584 self.assertIs(x.attr, x)
585 585
586 def test_recursive_multi(self): 586 def test_recursive_multi(self):
587 l = [] 587 l = []
588 d = {1:l} 588 d = {1:l}
589 i = C() 589 i = C()
590 i.attr = d 590 i.attr = d
591 l.append(i) 591 l.append(i)
592 for proto in protocols: 592 for proto in protocols:
593 s = self.dumps(l, proto) 593 s = self.dumps(l, proto)
594 x = self.loads(s) 594 x = self.loads(s)
595 self.assertEqual(len(x), 1) 595 self.assertEqual(len(x), 1)
596 self.assertEqual(dir(x[0]), dir(i)) 596 self.assertEqual(dir(x[0]), dir(i))
597 self.assertEqual(list(x[0].attr.keys()), [1]) 597 self.assertEqual(list(x[0].attr.keys()), [1])
598 self.assertTrue(x[0].attr[1] is x) 598 self.assertIs(x[0].attr[1], x)
599 599
600 def test_get(self): 600 def test_get(self):
601 self.assertRaises(KeyError, self.loads, b'g0\np0') 601 self.assertRaises(KeyError, self.loads, b'g0\np0')
602 self.assertEqual(self.loads(b'((Kdtp0\nh\x00l.))'), [(100,), (100,)]) 602 self.assertEqual(self.loads(b'((Kdtp0\nh\x00l.))'), [(100,), (100,)])
603 603
604 def test_insecure_strings(self): 604 def test_insecure_strings(self):
605 # XXX Some of these tests are temporarily disabled 605 # XXX Some of these tests are temporarily disabled
606 insecure = [b"abc", b"2 + 2", # not quoted 606 insecure = [b"abc", b"2 + 2", # not quoted
607 ## b"'abc' + 'def'", # not a single quoted string 607 ## b"'abc' + 'def'", # not a single quoted string
608 b"'abc", # quote is not closed 608 b"'abc", # quote is not closed
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
850 850
851 (0, False): pickle.INT, 851 (0, False): pickle.INT,
852 (1, False): pickle.INT, 852 (1, False): pickle.INT,
853 (2, False): pickle.NEWFALSE, 853 (2, False): pickle.NEWFALSE,
854 (3, False): pickle.NEWFALSE, 854 (3, False): pickle.NEWFALSE,
855 } 855 }
856 for proto in protocols: 856 for proto in protocols:
857 for x in None, False, True: 857 for x in None, False, True:
858 s = self.dumps(x, proto) 858 s = self.dumps(x, proto)
859 y = self.loads(s) 859 y = self.loads(s)
860 self.assertTrue(x is y, (proto, x, s, y)) 860 self.assertIs(x, y, (proto, x, s, y))
861 expected = expected_opcode[proto, x] 861 expected = expected_opcode[proto, x]
862 self.assertEqual(opcode_in_pickle(expected, s), True) 862 self.assertEqual(opcode_in_pickle(expected, s), True)
863 863
864 def test_newobj_tuple(self): 864 def test_newobj_tuple(self):
865 x = MyTuple([1, 2, 3]) 865 x = MyTuple([1, 2, 3])
866 x.foo = 42 866 x.foo = 42
867 x.bar = "hello" 867 x.bar = "hello"
868 for proto in protocols: 868 for proto in protocols:
869 s = self.dumps(x, proto) 869 s = self.dumps(x, proto)
870 y = self.loads(s) 870 y = self.loads(s)
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
974 n = 2500 # expect at least two chunks when proto > 0 974 n = 2500 # expect at least two chunks when proto > 0
975 x = list(range(n)) 975 x = list(range(n))
976 for proto in protocols: 976 for proto in protocols:
977 s = self.dumps(x, proto) 977 s = self.dumps(x, proto)
978 y = self.loads(s) 978 y = self.loads(s)
979 self.assertEqual(x, y) 979 self.assertEqual(x, y)
980 num_appends = count_opcode(pickle.APPENDS, s) 980 num_appends = count_opcode(pickle.APPENDS, s)
981 if proto == 0: 981 if proto == 0:
982 self.assertEqual(num_appends, 0) 982 self.assertEqual(num_appends, 0)
983 else: 983 else:
984 self.assertTrue(num_appends >= 2) 984 self.assertGreaterEqual(num_appends, 2)
985 985
986 def test_dict_chunking(self): 986 def test_dict_chunking(self):
987 n = 10 # too small to chunk 987 n = 10 # too small to chunk
988 x = dict.fromkeys(range(n)) 988 x = dict.fromkeys(range(n))
989 for proto in protocols: 989 for proto in protocols:
990 s = self.dumps(x, proto) 990 s = self.dumps(x, proto)
991 self.assertIsInstance(s, bytes_types) 991 self.assertIsInstance(s, bytes_types)
992 y = self.loads(s) 992 y = self.loads(s)
993 self.assertEqual(x, y) 993 self.assertEqual(x, y)
994 num_setitems = count_opcode(pickle.SETITEMS, s) 994 num_setitems = count_opcode(pickle.SETITEMS, s)
995 self.assertEqual(num_setitems, proto > 0) 995 self.assertEqual(num_setitems, proto > 0)
996 996
997 n = 2500 # expect at least two chunks when proto > 0 997 n = 2500 # expect at least two chunks when proto > 0
998 x = dict.fromkeys(range(n)) 998 x = dict.fromkeys(range(n))
999 for proto in protocols: 999 for proto in protocols:
1000 s = self.dumps(x, proto) 1000 s = self.dumps(x, proto)
1001 y = self.loads(s) 1001 y = self.loads(s)
1002 self.assertEqual(x, y) 1002 self.assertEqual(x, y)
1003 num_setitems = count_opcode(pickle.SETITEMS, s) 1003 num_setitems = count_opcode(pickle.SETITEMS, s)
1004 if proto == 0: 1004 if proto == 0:
1005 self.assertEqual(num_setitems, 0) 1005 self.assertEqual(num_setitems, 0)
1006 else: 1006 else:
1007 self.assertTrue(num_setitems >= 2) 1007 self.assertGreaterEqual(num_setitems, 2)
1008 1008
1009 def test_simple_newobj(self): 1009 def test_simple_newobj(self):
1010 x = object.__new__(SimpleNewObj) # avoid __init__ 1010 x = object.__new__(SimpleNewObj) # avoid __init__
1011 x.abc = 666 1011 x.abc = 666
1012 for proto in protocols: 1012 for proto in protocols:
1013 s = self.dumps(x, proto) 1013 s = self.dumps(x, proto)
1014 self.assertEqual(opcode_in_pickle(pickle.NEWOBJ, s), proto >= 2) 1014 self.assertEqual(opcode_in_pickle(pickle.NEWOBJ, s), proto >= 2)
1015 y = self.loads(s) # will raise TypeError if __init__ called 1015 y = self.loads(s) # will raise TypeError if __init__ called
1016 self.assertEqual(y.abc, 666) 1016 self.assertEqual(y.abc, 666)
1017 self.assertEqual(x.__dict__, y.__dict__) 1017 self.assertEqual(x.__dict__, y.__dict__)
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
1229 1229
1230 @bigmemtest(size=_2G, memuse=1 + 1, dry_run=False) 1230 @bigmemtest(size=_2G, memuse=1 + 1, dry_run=False)
1231 def test_huge_bytes_32b(self, size): 1231 def test_huge_bytes_32b(self, size):
1232 data = b"abcd" * (size // 4) 1232 data = b"abcd" * (size // 4)
1233 try: 1233 try:
1234 for proto in protocols: 1234 for proto in protocols:
1235 if proto < 3: 1235 if proto < 3:
1236 continue 1236 continue
1237 try: 1237 try:
1238 pickled = self.dumps(data, protocol=proto) 1238 pickled = self.dumps(data, protocol=proto)
1239 self.assertTrue(b"abcd" in pickled[:15]) 1239 self.assertIn(b"abcd", pickled[:15])
1240 self.assertTrue(b"abcd" in pickled[-15:]) 1240 self.assertIn(b"abcd", pickled[-15:])
1241 finally: 1241 finally:
1242 pickled = None 1242 pickled = None
1243 finally: 1243 finally:
1244 data = None 1244 data = None
1245 1245
1246 @bigmemtest(size=_4G, memuse=1 + 1, dry_run=False) 1246 @bigmemtest(size=_4G, memuse=1 + 1, dry_run=False)
1247 def test_huge_bytes_64b(self, size): 1247 def test_huge_bytes_64b(self, size):
1248 data = b"a" * size 1248 data = b"a" * size
1249 try: 1249 try:
1250 for proto in protocols: 1250 for proto in protocols:
1251 if proto < 3: 1251 if proto < 3:
1252 continue 1252 continue
1253 with self.assertRaises((ValueError, OverflowError)): 1253 with self.assertRaises((ValueError, OverflowError)):
1254 self.dumps(data, protocol=proto) 1254 self.dumps(data, protocol=proto)
1255 finally: 1255 finally:
1256 data = None 1256 data = None
1257 1257
1258 # All protocols use 1-byte per printable ASCII character; we add another 1258 # All protocols use 1-byte per printable ASCII character; we add another
1259 # byte because the encoded form has to be copied into the internal buffer. 1259 # byte because the encoded form has to be copied into the internal buffer.
1260 1260
1261 @bigmemtest(size=_2G, memuse=2 + ascii_char_size, dry_run=False) 1261 @bigmemtest(size=_2G, memuse=2 + ascii_char_size, dry_run=False)
1262 def test_huge_str_32b(self, size): 1262 def test_huge_str_32b(self, size):
1263 data = "abcd" * (size // 4) 1263 data = "abcd" * (size // 4)
1264 try: 1264 try:
1265 for proto in protocols: 1265 for proto in protocols:
1266 try: 1266 try:
1267 pickled = self.dumps(data, protocol=proto) 1267 pickled = self.dumps(data, protocol=proto)
1268 self.assertTrue(b"abcd" in pickled[:15]) 1268 self.assertIn(b"abcd", pickled[:15])
1269 self.assertTrue(b"abcd" in pickled[-15:]) 1269 self.assertIn(b"abcd", pickled[-15:])
1270 finally: 1270 finally:
1271 pickled = None 1271 pickled = None
1272 finally: 1272 finally:
1273 data = None 1273 data = None
1274 1274
1275 # BINUNICODE (protocols 1, 2 and 3) cannot carry more than 1275 # BINUNICODE (protocols 1, 2 and 3) cannot carry more than
1276 # 2**32 - 1 bytes of utf-8 encoded unicode. 1276 # 2**32 - 1 bytes of utf-8 encoded unicode.
1277 1277
1278 @bigmemtest(size=_4G, memuse=1 + ascii_char_size, dry_run=False) 1278 @bigmemtest(size=_4G, memuse=1 + ascii_char_size, dry_run=False)
1279 def test_huge_str_64b(self, size): 1279 def test_huge_str_64b(self, size):
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
1546 1546
1547 self.assertEqual(unpickled_data1, data) 1547 self.assertEqual(unpickled_data1, data)
1548 1548
1549 primed = self.unpickler_class(io.BytesIO(primed_pickled)) 1549 primed = self.unpickler_class(io.BytesIO(primed_pickled))
1550 primed.memo = unpickler.memo 1550 primed.memo = unpickler.memo
1551 unpickled_data2 = primed.load() 1551 unpickled_data2 = primed.load()
1552 1552
1553 primed.memo.clear() 1553 primed.memo.clear()
1554 1554
1555 self.assertEqual(unpickled_data2, data) 1555 self.assertEqual(unpickled_data2, data)
1556 self.assertTrue(unpickled_data2 is unpickled_data1) 1556 self.assertIs(unpickled_data2, unpickled_data1)
1557 1557
1558 def test_reusing_unpickler_objects(self): 1558 def test_reusing_unpickler_objects(self):
1559 data1 = ["abcdefg", "abcdefg", 44] 1559 data1 = ["abcdefg", "abcdefg", 44]
1560 f = io.BytesIO() 1560 f = io.BytesIO()
1561 pickler = self.pickler_class(f) 1561 pickler = self.pickler_class(f)
1562 pickler.dump(data1) 1562 pickler.dump(data1)
1563 pickled1 = f.getvalue() 1563 pickled1 = f.getvalue()
1564 1564
1565 data2 = ["abcdefg", 44, 44] 1565 data2 = ["abcdefg", 44, 44]
1566 f = io.BytesIO() 1566 f = io.BytesIO()
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
1728 for j in range(0, len(p), 20): 1728 for j in range(0, len(p), 20):
1729 b = bytes(p[j:j+20]) 1729 b = bytes(p[j:j+20])
1730 print(" {0!r}".format(b)) 1730 print(" {0!r}".format(b))
1731 print(")") 1731 print(")")
1732 print() 1732 print()
1733 print("# Disassembly of DATA{0}".format(i)) 1733 print("# Disassembly of DATA{0}".format(i))
1734 print("DATA{0}_DIS = \"\"\"\\".format(i)) 1734 print("DATA{0}_DIS = \"\"\"\\".format(i))
1735 dis(p) 1735 dis(p)
1736 print("\"\"\"") 1736 print("\"\"\"")
1737 print() 1737 print()
OLDNEW
« no previous file with comments | « Lib/test/multibytecodec_support.py ('k') | Lib/test/test_abc.py » ('j') | no next file with comments »

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