Debian Codegraph @

Lib/test/test__opcode.py

raw
1 import dis
2 from test.support.import_helper import import_module
3 import unittest
4 import opcode
5
6 _opcode = import_module("_opcode")
7 from _opcode import stack_effect
8
9
10 class OpListTests(unittest.TestCase):
11 def check_bool_function_result(self, func, ops, expected):
12 for op in ops:
13 if isinstance(op, str):
14 op = dis.opmap[op]
15 with self.subTest(opcode=op, func=func):
16 self.assertIsInstance(func(op), bool)
17 self.assertEqual(func(op), expected)
18
19 def test_invalid_opcodes(self):
20 invalid = [-100, -1, 512, 513, 1000]
21 self.check_bool_function_result(_opcode.is_valid, invalid, False)
22 self.check_bool_function_result(_opcode.has_arg, invalid, False)
23 self.check_bool_function_result(_opcode.has_const, invalid, False)
24 self.check_bool_function_result(_opcode.has_name, invalid, False)
25 self.check_bool_function_result(_opcode.has_jump, invalid, False)
26 self.check_bool_function_result(_opcode.has_free, invalid, False)
27 self.check_bool_function_result(_opcode.has_local, invalid, False)
28 self.check_bool_function_result(_opcode.has_exc, invalid, False)
29
30 def test_is_valid(self):
31 names = [
32 'CACHE',
33 'POP_TOP',
34 'IMPORT_NAME',
35 'JUMP',
36 'INSTRUMENTED_RETURN_VALUE',
37 ]
38 opcodes = [dis.opmap[opname] for opname in names]
39 self.check_bool_function_result(_opcode.is_valid, opcodes, True)
40
41 def test_opmaps(self):
42 def check_roundtrip(name, map):
43 return self.assertEqual(opcode.opname[map[name]], name)
44
45 check_roundtrip('BINARY_OP', opcode.opmap)
46 check_roundtrip('BINARY_OP_ADD_INT', opcode._specialized_opmap)
47
48 def test_oplists(self):
49 def check_function(self, func, expected):
50 for op in [-10, 520]:
51 with self.subTest(opcode=op, func=func):
52 res = func(op)
53 self.assertIsInstance(res, bool)
54 self.assertEqual(res, op in expected)
55
56 check_function(self, _opcode.has_arg, dis.hasarg)
57 check_function(self, _opcode.has_const, dis.hasconst)
58 check_function(self, _opcode.has_name, dis.hasname)
59 check_function(self, _opcode.has_jump, dis.hasjump)
60 check_function(self, _opcode.has_free, dis.hasfree)
61 check_function(self, _opcode.has_local, dis.haslocal)
62 check_function(self, _opcode.has_exc, dis.hasexc)
63
64
65 class StackEffectTests(unittest.TestCase):
66 def test_stack_effect(self):
67 self.assertEqual(stack_effect(dis.opmap['POP_TOP']), -1)
68 self.assertEqual(stack_effect(dis.opmap['BUILD_SLICE'], 2), -1)
69 self.assertEqual(stack_effect(dis.opmap['BUILD_SLICE'], 3), -2)
70 self.assertRaises(ValueError, stack_effect, 30000)
71 # All defined opcodes
72 has_arg = dis.hasarg
73 for name, code in filter(lambda item: item[0] not in dis.deoptmap, dis.opmap.items()):
74 if code >= opcode.MIN_INSTRUMENTED_OPCODE:
75 continue
76 with self.subTest(opname=name):
77 stack_effect(code)
78 stack_effect(code, 0)
79 # All not defined opcodes
80 for code in set(range(256)) - set(dis.opmap.values()):
81 with self.subTest(opcode=code):
82 self.assertRaises(ValueError, stack_effect, code)
83 self.assertRaises(ValueError, stack_effect, code, 0)
84
85 def test_stack_effect_jump(self):
86 FOR_ITER = dis.opmap['FOR_ITER']
87 self.assertEqual(stack_effect(FOR_ITER, 0), 1)
88 self.assertEqual(stack_effect(FOR_ITER, 0, jump=True), 1)
89 self.assertEqual(stack_effect(FOR_ITER, 0, jump=False), 1)
90 JUMP_FORWARD = dis.opmap['JUMP_FORWARD']
91 self.assertEqual(stack_effect(JUMP_FORWARD, 0), 0)
92 self.assertEqual(stack_effect(JUMP_FORWARD, 0, jump=True), 0)
93 self.assertEqual(stack_effect(JUMP_FORWARD, 0, jump=False), 0)
94 # All defined opcodes
95 has_arg = dis.hasarg
96 has_exc = dis.hasexc
97 has_jump = dis.hasjabs + dis.hasjrel
98 for name, code in filter(lambda item: item[0] not in dis.deoptmap, dis.opmap.items()):
99 if code >= opcode.MIN_INSTRUMENTED_OPCODE:
100 continue
101 with self.subTest(opname=name):
102 if code not in has_arg:
103 common = stack_effect(code)
104 jump = stack_effect(code, jump=True)
105 nojump = stack_effect(code, jump=False)
106 else:
107 common = stack_effect(code, 0)
108 jump = stack_effect(code, 0, jump=True)
109 nojump = stack_effect(code, 0, jump=False)
110 if code in has_jump or code in has_exc:
111 self.assertEqual(common, max(jump, nojump))
112 else:
113 self.assertEqual(jump, common)
114 self.assertEqual(nojump, common)
115
116
117 class SpecializationStatsTests(unittest.TestCase):
118 def test_specialization_stats(self):
119 stat_names = ["success", "failure", "hit", "deferred", "miss", "deopt"]
120 specialized_opcodes = [
121 op.lower()
122 for op in opcode._specializations
123 if opcode._inline_cache_entries.get(op, 0)
124 ]
125 self.assertIn('load_attr', specialized_opcodes)
126 self.assertIn('binary_op', specialized_opcodes)
127
128 stats = _opcode.get_specialization_stats()
129 if stats is not None:
130 self.assertIsInstance(stats, dict)
131 self.assertCountEqual(stats.keys(), specialized_opcodes)
132 self.assertCountEqual(
133 stats['load_attr'].keys(),
134 stat_names + ['failure_kinds'])
135 for sn in stat_names:
136 self.assertIsInstance(stats['load_attr'][sn], int)
137 self.assertIsInstance(
138 stats['load_attr']['failure_kinds'],
139 tuple)
140 for v in stats['load_attr']['failure_kinds']:
141 self.assertIsInstance(v, int)
142
143
144 if __name__ == "__main__":
145 unittest.main()