1 | n/a | import pickle |
---|
2 | n/a | import pickletools |
---|
3 | n/a | from test import support |
---|
4 | n/a | from test.pickletester import AbstractPickleTests |
---|
5 | n/a | from test.pickletester import AbstractPickleModuleTests |
---|
6 | n/a | import unittest |
---|
7 | n/a | |
---|
8 | n/a | class OptimizedPickleTests(AbstractPickleTests, AbstractPickleModuleTests): |
---|
9 | n/a | |
---|
10 | n/a | def dumps(self, arg, proto=None): |
---|
11 | n/a | return pickletools.optimize(pickle.dumps(arg, proto)) |
---|
12 | n/a | |
---|
13 | n/a | def loads(self, buf, **kwds): |
---|
14 | n/a | return pickle.loads(buf, **kwds) |
---|
15 | n/a | |
---|
16 | n/a | # Test relies on precise output of dumps() |
---|
17 | n/a | test_pickle_to_2x = None |
---|
18 | n/a | |
---|
19 | n/a | def test_optimize_long_binget(self): |
---|
20 | n/a | data = [str(i) for i in range(257)] |
---|
21 | n/a | data.append(data[-1]) |
---|
22 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
23 | n/a | pickled = pickle.dumps(data, proto) |
---|
24 | n/a | unpickled = pickle.loads(pickled) |
---|
25 | n/a | self.assertEqual(unpickled, data) |
---|
26 | n/a | self.assertIs(unpickled[-1], unpickled[-2]) |
---|
27 | n/a | |
---|
28 | n/a | pickled2 = pickletools.optimize(pickled) |
---|
29 | n/a | unpickled2 = pickle.loads(pickled2) |
---|
30 | n/a | self.assertEqual(unpickled2, data) |
---|
31 | n/a | self.assertIs(unpickled2[-1], unpickled2[-2]) |
---|
32 | n/a | self.assertNotIn(pickle.LONG_BINGET, pickled2) |
---|
33 | n/a | self.assertNotIn(pickle.LONG_BINPUT, pickled2) |
---|
34 | n/a | |
---|
35 | n/a | def test_optimize_binput_and_memoize(self): |
---|
36 | n/a | pickled = (b'\x80\x04\x95\x15\x00\x00\x00\x00\x00\x00\x00' |
---|
37 | n/a | b']\x94(\x8c\x04spamq\x01\x8c\x03ham\x94h\x02e.') |
---|
38 | n/a | # 0: \x80 PROTO 4 |
---|
39 | n/a | # 2: \x95 FRAME 21 |
---|
40 | n/a | # 11: ] EMPTY_LIST |
---|
41 | n/a | # 12: \x94 MEMOIZE |
---|
42 | n/a | # 13: ( MARK |
---|
43 | n/a | # 14: \x8c SHORT_BINUNICODE 'spam' |
---|
44 | n/a | # 20: q BINPUT 1 |
---|
45 | n/a | # 22: \x8c SHORT_BINUNICODE 'ham' |
---|
46 | n/a | # 27: \x94 MEMOIZE |
---|
47 | n/a | # 28: h BINGET 2 |
---|
48 | n/a | # 30: e APPENDS (MARK at 13) |
---|
49 | n/a | # 31: . STOP |
---|
50 | n/a | self.assertIn(pickle.BINPUT, pickled) |
---|
51 | n/a | unpickled = pickle.loads(pickled) |
---|
52 | n/a | self.assertEqual(unpickled, ['spam', 'ham', 'ham']) |
---|
53 | n/a | self.assertIs(unpickled[1], unpickled[2]) |
---|
54 | n/a | |
---|
55 | n/a | pickled2 = pickletools.optimize(pickled) |
---|
56 | n/a | unpickled2 = pickle.loads(pickled2) |
---|
57 | n/a | self.assertEqual(unpickled2, ['spam', 'ham', 'ham']) |
---|
58 | n/a | self.assertIs(unpickled2[1], unpickled2[2]) |
---|
59 | n/a | self.assertNotIn(pickle.BINPUT, pickled2) |
---|
60 | n/a | |
---|
61 | n/a | |
---|
62 | n/a | class MiscTestCase(unittest.TestCase): |
---|
63 | n/a | def test__all__(self): |
---|
64 | n/a | blacklist = {'bytes_types', |
---|
65 | n/a | 'UP_TO_NEWLINE', 'TAKEN_FROM_ARGUMENT1', |
---|
66 | n/a | 'TAKEN_FROM_ARGUMENT4', 'TAKEN_FROM_ARGUMENT4U', |
---|
67 | n/a | 'TAKEN_FROM_ARGUMENT8U', 'ArgumentDescriptor', |
---|
68 | n/a | 'read_uint1', 'read_uint2', 'read_int4', 'read_uint4', |
---|
69 | n/a | 'read_uint8', 'read_stringnl', 'read_stringnl_noescape', |
---|
70 | n/a | 'read_stringnl_noescape_pair', 'read_string1', |
---|
71 | n/a | 'read_string4', 'read_bytes1', 'read_bytes4', |
---|
72 | n/a | 'read_bytes8', 'read_unicodestringnl', |
---|
73 | n/a | 'read_unicodestring1', 'read_unicodestring4', |
---|
74 | n/a | 'read_unicodestring8', 'read_decimalnl_short', |
---|
75 | n/a | 'read_decimalnl_long', 'read_floatnl', 'read_float8', |
---|
76 | n/a | 'read_long1', 'read_long4', |
---|
77 | n/a | 'uint1', 'uint2', 'int4', 'uint4', 'uint8', 'stringnl', |
---|
78 | n/a | 'stringnl_noescape', 'stringnl_noescape_pair', 'string1', |
---|
79 | n/a | 'string4', 'bytes1', 'bytes4', 'bytes8', |
---|
80 | n/a | 'unicodestringnl', 'unicodestring1', 'unicodestring4', |
---|
81 | n/a | 'unicodestring8', 'decimalnl_short', 'decimalnl_long', |
---|
82 | n/a | 'floatnl', 'float8', 'long1', 'long4', |
---|
83 | n/a | 'StackObject', |
---|
84 | n/a | 'pyint', 'pylong', 'pyinteger_or_bool', 'pybool', 'pyfloat', |
---|
85 | n/a | 'pybytes_or_str', 'pystring', 'pybytes', 'pyunicode', |
---|
86 | n/a | 'pynone', 'pytuple', 'pylist', 'pydict', 'pyset', |
---|
87 | n/a | 'pyfrozenset', 'anyobject', 'markobject', 'stackslice', |
---|
88 | n/a | 'OpcodeInfo', 'opcodes', 'code2op', |
---|
89 | n/a | } |
---|
90 | n/a | support.check__all__(self, pickletools, blacklist=blacklist) |
---|
91 | n/a | |
---|
92 | n/a | |
---|
93 | n/a | def test_main(): |
---|
94 | n/a | support.run_unittest(OptimizedPickleTests) |
---|
95 | n/a | support.run_unittest(MiscTestCase) |
---|
96 | n/a | support.run_doctest(pickletools) |
---|
97 | n/a | |
---|
98 | n/a | |
---|
99 | n/a | if __name__ == "__main__": |
---|
100 | n/a | test_main() |
---|