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