ยปCore Development>Code coverage>Lib/test/test_pickletools.py

Python code coverage for Lib/test/test_pickletools.py

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