1 | n/a | # Minimal tests for dis module |
---|
2 | n/a | |
---|
3 | n/a | from test.support import captured_stdout |
---|
4 | n/a | from test.bytecode_helper import BytecodeTestCase |
---|
5 | n/a | import unittest |
---|
6 | n/a | import sys |
---|
7 | n/a | import dis |
---|
8 | n/a | import io |
---|
9 | n/a | import re |
---|
10 | n/a | import types |
---|
11 | n/a | import contextlib |
---|
12 | n/a | |
---|
13 | n/a | def get_tb(): |
---|
14 | n/a | def _error(): |
---|
15 | n/a | try: |
---|
16 | n/a | 1 / 0 |
---|
17 | n/a | except Exception as e: |
---|
18 | n/a | tb = e.__traceback__ |
---|
19 | n/a | return tb |
---|
20 | n/a | |
---|
21 | n/a | tb = _error() |
---|
22 | n/a | while tb.tb_next: |
---|
23 | n/a | tb = tb.tb_next |
---|
24 | n/a | return tb |
---|
25 | n/a | |
---|
26 | n/a | TRACEBACK_CODE = get_tb().tb_frame.f_code |
---|
27 | n/a | |
---|
28 | n/a | class _C: |
---|
29 | n/a | def __init__(self, x): |
---|
30 | n/a | self.x = x == 1 |
---|
31 | n/a | |
---|
32 | n/a | @staticmethod |
---|
33 | n/a | def sm(x): |
---|
34 | n/a | x = x == 1 |
---|
35 | n/a | |
---|
36 | n/a | @classmethod |
---|
37 | n/a | def cm(cls, x): |
---|
38 | n/a | cls.x = x == 1 |
---|
39 | n/a | |
---|
40 | n/a | dis_c_instance_method = """\ |
---|
41 | n/a | %3d 0 LOAD_FAST 1 (x) |
---|
42 | n/a | 2 LOAD_CONST 1 (1) |
---|
43 | n/a | 4 COMPARE_OP 2 (==) |
---|
44 | n/a | 6 LOAD_FAST 0 (self) |
---|
45 | n/a | 8 STORE_ATTR 0 (x) |
---|
46 | n/a | 10 LOAD_CONST 0 (None) |
---|
47 | n/a | 12 RETURN_VALUE |
---|
48 | n/a | """ % (_C.__init__.__code__.co_firstlineno + 1,) |
---|
49 | n/a | |
---|
50 | n/a | dis_c_instance_method_bytes = """\ |
---|
51 | n/a | 0 LOAD_FAST 1 (1) |
---|
52 | n/a | 2 LOAD_CONST 1 (1) |
---|
53 | n/a | 4 COMPARE_OP 2 (==) |
---|
54 | n/a | 6 LOAD_FAST 0 (0) |
---|
55 | n/a | 8 STORE_ATTR 0 (0) |
---|
56 | n/a | 10 LOAD_CONST 0 (0) |
---|
57 | n/a | 12 RETURN_VALUE |
---|
58 | n/a | """ |
---|
59 | n/a | |
---|
60 | n/a | dis_c_class_method = """\ |
---|
61 | n/a | %3d 0 LOAD_FAST 1 (x) |
---|
62 | n/a | 2 LOAD_CONST 1 (1) |
---|
63 | n/a | 4 COMPARE_OP 2 (==) |
---|
64 | n/a | 6 LOAD_FAST 0 (cls) |
---|
65 | n/a | 8 STORE_ATTR 0 (x) |
---|
66 | n/a | 10 LOAD_CONST 0 (None) |
---|
67 | n/a | 12 RETURN_VALUE |
---|
68 | n/a | """ % (_C.cm.__code__.co_firstlineno + 2,) |
---|
69 | n/a | |
---|
70 | n/a | dis_c_static_method = """\ |
---|
71 | n/a | %3d 0 LOAD_FAST 0 (x) |
---|
72 | n/a | 2 LOAD_CONST 1 (1) |
---|
73 | n/a | 4 COMPARE_OP 2 (==) |
---|
74 | n/a | 6 STORE_FAST 0 (x) |
---|
75 | n/a | 8 LOAD_CONST 0 (None) |
---|
76 | n/a | 10 RETURN_VALUE |
---|
77 | n/a | """ % (_C.sm.__code__.co_firstlineno + 2,) |
---|
78 | n/a | |
---|
79 | n/a | # Class disassembling info has an extra newline at end. |
---|
80 | n/a | dis_c = """\ |
---|
81 | n/a | Disassembly of %s: |
---|
82 | n/a | %s |
---|
83 | n/a | Disassembly of %s: |
---|
84 | n/a | %s |
---|
85 | n/a | Disassembly of %s: |
---|
86 | n/a | %s |
---|
87 | n/a | """ % (_C.__init__.__name__, dis_c_instance_method, |
---|
88 | n/a | _C.cm.__name__, dis_c_class_method, |
---|
89 | n/a | _C.sm.__name__, dis_c_static_method) |
---|
90 | n/a | |
---|
91 | n/a | def _f(a): |
---|
92 | n/a | print(a) |
---|
93 | n/a | return 1 |
---|
94 | n/a | |
---|
95 | n/a | dis_f = """\ |
---|
96 | n/a | %3d 0 LOAD_GLOBAL 0 (print) |
---|
97 | n/a | 2 LOAD_FAST 0 (a) |
---|
98 | n/a | 4 CALL_FUNCTION 1 |
---|
99 | n/a | 6 POP_TOP |
---|
100 | n/a | |
---|
101 | n/a | %3d 8 LOAD_CONST 1 (1) |
---|
102 | n/a | 10 RETURN_VALUE |
---|
103 | n/a | """ % (_f.__code__.co_firstlineno + 1, |
---|
104 | n/a | _f.__code__.co_firstlineno + 2) |
---|
105 | n/a | |
---|
106 | n/a | |
---|
107 | n/a | dis_f_co_code = """\ |
---|
108 | n/a | 0 LOAD_GLOBAL 0 (0) |
---|
109 | n/a | 2 LOAD_FAST 0 (0) |
---|
110 | n/a | 4 CALL_FUNCTION 1 |
---|
111 | n/a | 6 POP_TOP |
---|
112 | n/a | 8 LOAD_CONST 1 (1) |
---|
113 | n/a | 10 RETURN_VALUE |
---|
114 | n/a | """ |
---|
115 | n/a | |
---|
116 | n/a | |
---|
117 | n/a | def bug708901(): |
---|
118 | n/a | for res in range(1, |
---|
119 | n/a | 10): |
---|
120 | n/a | pass |
---|
121 | n/a | |
---|
122 | n/a | dis_bug708901 = """\ |
---|
123 | n/a | %3d 0 SETUP_LOOP 18 (to 20) |
---|
124 | n/a | 2 LOAD_GLOBAL 0 (range) |
---|
125 | n/a | 4 LOAD_CONST 1 (1) |
---|
126 | n/a | |
---|
127 | n/a | %3d 6 LOAD_CONST 2 (10) |
---|
128 | n/a | 8 CALL_FUNCTION 2 |
---|
129 | n/a | 10 GET_ITER |
---|
130 | n/a | >> 12 FOR_ITER 4 (to 18) |
---|
131 | n/a | 14 STORE_FAST 0 (res) |
---|
132 | n/a | |
---|
133 | n/a | %3d 16 JUMP_ABSOLUTE 12 |
---|
134 | n/a | >> 18 POP_BLOCK |
---|
135 | n/a | >> 20 LOAD_CONST 0 (None) |
---|
136 | n/a | 22 RETURN_VALUE |
---|
137 | n/a | """ % (bug708901.__code__.co_firstlineno + 1, |
---|
138 | n/a | bug708901.__code__.co_firstlineno + 2, |
---|
139 | n/a | bug708901.__code__.co_firstlineno + 3) |
---|
140 | n/a | |
---|
141 | n/a | |
---|
142 | n/a | def bug1333982(x=[]): |
---|
143 | n/a | assert 0, ([s for s in x] + |
---|
144 | n/a | 1) |
---|
145 | n/a | pass |
---|
146 | n/a | |
---|
147 | n/a | dis_bug1333982 = """\ |
---|
148 | n/a | %3d 0 LOAD_CONST 1 (0) |
---|
149 | n/a | 2 POP_JUMP_IF_TRUE 26 |
---|
150 | n/a | 4 LOAD_GLOBAL 0 (AssertionError) |
---|
151 | n/a | 6 LOAD_CONST 2 (<code object <listcomp> at 0x..., file "%s", line %d>) |
---|
152 | n/a | 8 LOAD_CONST 3 ('bug1333982.<locals>.<listcomp>') |
---|
153 | n/a | 10 MAKE_FUNCTION 0 |
---|
154 | n/a | 12 LOAD_FAST 0 (x) |
---|
155 | n/a | 14 GET_ITER |
---|
156 | n/a | 16 CALL_FUNCTION 1 |
---|
157 | n/a | |
---|
158 | n/a | %3d 18 LOAD_CONST 4 (1) |
---|
159 | n/a | 20 BINARY_ADD |
---|
160 | n/a | 22 CALL_FUNCTION 1 |
---|
161 | n/a | 24 RAISE_VARARGS 1 |
---|
162 | n/a | |
---|
163 | n/a | %3d >> 26 LOAD_CONST 0 (None) |
---|
164 | n/a | 28 RETURN_VALUE |
---|
165 | n/a | """ % (bug1333982.__code__.co_firstlineno + 1, |
---|
166 | n/a | __file__, |
---|
167 | n/a | bug1333982.__code__.co_firstlineno + 1, |
---|
168 | n/a | bug1333982.__code__.co_firstlineno + 2, |
---|
169 | n/a | bug1333982.__code__.co_firstlineno + 3) |
---|
170 | n/a | |
---|
171 | n/a | _BIG_LINENO_FORMAT = """\ |
---|
172 | n/a | %3d 0 LOAD_GLOBAL 0 (spam) |
---|
173 | n/a | 2 POP_TOP |
---|
174 | n/a | 4 LOAD_CONST 0 (None) |
---|
175 | n/a | 6 RETURN_VALUE |
---|
176 | n/a | """ |
---|
177 | n/a | |
---|
178 | n/a | dis_module_expected_results = """\ |
---|
179 | n/a | Disassembly of f: |
---|
180 | n/a | 4 0 LOAD_CONST 0 (None) |
---|
181 | n/a | 2 RETURN_VALUE |
---|
182 | n/a | |
---|
183 | n/a | Disassembly of g: |
---|
184 | n/a | 5 0 LOAD_CONST 0 (None) |
---|
185 | n/a | 2 RETURN_VALUE |
---|
186 | n/a | |
---|
187 | n/a | """ |
---|
188 | n/a | |
---|
189 | n/a | expr_str = "x + 1" |
---|
190 | n/a | |
---|
191 | n/a | dis_expr_str = """\ |
---|
192 | n/a | 1 0 LOAD_NAME 0 (x) |
---|
193 | n/a | 2 LOAD_CONST 0 (1) |
---|
194 | n/a | 4 BINARY_ADD |
---|
195 | n/a | 6 RETURN_VALUE |
---|
196 | n/a | """ |
---|
197 | n/a | |
---|
198 | n/a | simple_stmt_str = "x = x + 1" |
---|
199 | n/a | |
---|
200 | n/a | dis_simple_stmt_str = """\ |
---|
201 | n/a | 1 0 LOAD_NAME 0 (x) |
---|
202 | n/a | 2 LOAD_CONST 0 (1) |
---|
203 | n/a | 4 BINARY_ADD |
---|
204 | n/a | 6 STORE_NAME 0 (x) |
---|
205 | n/a | 8 LOAD_CONST 1 (None) |
---|
206 | n/a | 10 RETURN_VALUE |
---|
207 | n/a | """ |
---|
208 | n/a | |
---|
209 | n/a | annot_stmt_str = """\ |
---|
210 | n/a | |
---|
211 | n/a | x: int = 1 |
---|
212 | n/a | y: fun(1) |
---|
213 | n/a | lst[fun(0)]: int = 1 |
---|
214 | n/a | """ |
---|
215 | n/a | # leading newline is for a reason (tests lineno) |
---|
216 | n/a | |
---|
217 | n/a | dis_annot_stmt_str = """\ |
---|
218 | n/a | 2 0 SETUP_ANNOTATIONS |
---|
219 | n/a | 2 LOAD_CONST 0 (1) |
---|
220 | n/a | 4 STORE_NAME 0 (x) |
---|
221 | n/a | 6 LOAD_NAME 1 (int) |
---|
222 | n/a | 8 STORE_ANNOTATION 0 (x) |
---|
223 | n/a | |
---|
224 | n/a | 3 10 LOAD_NAME 2 (fun) |
---|
225 | n/a | 12 LOAD_CONST 0 (1) |
---|
226 | n/a | 14 CALL_FUNCTION 1 |
---|
227 | n/a | 16 STORE_ANNOTATION 3 (y) |
---|
228 | n/a | |
---|
229 | n/a | 4 18 LOAD_CONST 0 (1) |
---|
230 | n/a | 20 LOAD_NAME 4 (lst) |
---|
231 | n/a | 22 LOAD_NAME 2 (fun) |
---|
232 | n/a | 24 LOAD_CONST 1 (0) |
---|
233 | n/a | 26 CALL_FUNCTION 1 |
---|
234 | n/a | 28 STORE_SUBSCR |
---|
235 | n/a | 30 LOAD_NAME 1 (int) |
---|
236 | n/a | 32 POP_TOP |
---|
237 | n/a | 34 LOAD_CONST 2 (None) |
---|
238 | n/a | 36 RETURN_VALUE |
---|
239 | n/a | """ |
---|
240 | n/a | |
---|
241 | n/a | compound_stmt_str = """\ |
---|
242 | n/a | x = 0 |
---|
243 | n/a | while 1: |
---|
244 | n/a | x += 1""" |
---|
245 | n/a | # Trailing newline has been deliberately omitted |
---|
246 | n/a | |
---|
247 | n/a | dis_compound_stmt_str = """\ |
---|
248 | n/a | 1 0 LOAD_CONST 0 (0) |
---|
249 | n/a | 2 STORE_NAME 0 (x) |
---|
250 | n/a | |
---|
251 | n/a | 2 4 SETUP_LOOP 12 (to 18) |
---|
252 | n/a | |
---|
253 | n/a | 3 >> 6 LOAD_NAME 0 (x) |
---|
254 | n/a | 8 LOAD_CONST 1 (1) |
---|
255 | n/a | 10 INPLACE_ADD |
---|
256 | n/a | 12 STORE_NAME 0 (x) |
---|
257 | n/a | 14 JUMP_ABSOLUTE 6 |
---|
258 | n/a | 16 POP_BLOCK |
---|
259 | n/a | >> 18 LOAD_CONST 2 (None) |
---|
260 | n/a | 20 RETURN_VALUE |
---|
261 | n/a | """ |
---|
262 | n/a | |
---|
263 | n/a | dis_traceback = """\ |
---|
264 | n/a | %3d 0 SETUP_EXCEPT 12 (to 14) |
---|
265 | n/a | |
---|
266 | n/a | %3d 2 LOAD_CONST 1 (1) |
---|
267 | n/a | 4 LOAD_CONST 2 (0) |
---|
268 | n/a | --> 6 BINARY_TRUE_DIVIDE |
---|
269 | n/a | 8 POP_TOP |
---|
270 | n/a | 10 POP_BLOCK |
---|
271 | n/a | 12 JUMP_FORWARD 40 (to 54) |
---|
272 | n/a | |
---|
273 | n/a | %3d >> 14 DUP_TOP |
---|
274 | n/a | 16 LOAD_GLOBAL 0 (Exception) |
---|
275 | n/a | 18 COMPARE_OP 10 (exception match) |
---|
276 | n/a | 20 POP_JUMP_IF_FALSE 52 |
---|
277 | n/a | 22 POP_TOP |
---|
278 | n/a | 24 STORE_FAST 0 (e) |
---|
279 | n/a | 26 POP_TOP |
---|
280 | n/a | 28 SETUP_FINALLY 12 (to 42) |
---|
281 | n/a | |
---|
282 | n/a | %3d 30 LOAD_FAST 0 (e) |
---|
283 | n/a | 32 LOAD_ATTR 1 (__traceback__) |
---|
284 | n/a | 34 STORE_FAST 1 (tb) |
---|
285 | n/a | 36 POP_BLOCK |
---|
286 | n/a | 38 POP_EXCEPT |
---|
287 | n/a | 40 LOAD_CONST 0 (None) |
---|
288 | n/a | >> 42 LOAD_CONST 0 (None) |
---|
289 | n/a | 44 STORE_FAST 0 (e) |
---|
290 | n/a | 46 DELETE_FAST 0 (e) |
---|
291 | n/a | 48 END_FINALLY |
---|
292 | n/a | 50 JUMP_FORWARD 2 (to 54) |
---|
293 | n/a | >> 52 END_FINALLY |
---|
294 | n/a | |
---|
295 | n/a | %3d >> 54 LOAD_FAST 1 (tb) |
---|
296 | n/a | 56 RETURN_VALUE |
---|
297 | n/a | """ % (TRACEBACK_CODE.co_firstlineno + 1, |
---|
298 | n/a | TRACEBACK_CODE.co_firstlineno + 2, |
---|
299 | n/a | TRACEBACK_CODE.co_firstlineno + 3, |
---|
300 | n/a | TRACEBACK_CODE.co_firstlineno + 4, |
---|
301 | n/a | TRACEBACK_CODE.co_firstlineno + 5) |
---|
302 | n/a | |
---|
303 | n/a | def _fstring(a, b, c, d): |
---|
304 | n/a | return f'{a} {b:4} {c!r} {d!r:4}' |
---|
305 | n/a | |
---|
306 | n/a | dis_fstring = """\ |
---|
307 | n/a | %3d 0 LOAD_FAST 0 (a) |
---|
308 | n/a | 2 FORMAT_VALUE 0 |
---|
309 | n/a | 4 LOAD_CONST 1 (' ') |
---|
310 | n/a | 6 LOAD_FAST 1 (b) |
---|
311 | n/a | 8 LOAD_CONST 2 ('4') |
---|
312 | n/a | 10 FORMAT_VALUE 4 (with format) |
---|
313 | n/a | 12 LOAD_CONST 1 (' ') |
---|
314 | n/a | 14 LOAD_FAST 2 (c) |
---|
315 | n/a | 16 FORMAT_VALUE 2 (repr) |
---|
316 | n/a | 18 LOAD_CONST 1 (' ') |
---|
317 | n/a | 20 LOAD_FAST 3 (d) |
---|
318 | n/a | 22 LOAD_CONST 2 ('4') |
---|
319 | n/a | 24 FORMAT_VALUE 6 (repr, with format) |
---|
320 | n/a | 26 BUILD_STRING 7 |
---|
321 | n/a | 28 RETURN_VALUE |
---|
322 | n/a | """ % (_fstring.__code__.co_firstlineno + 1,) |
---|
323 | n/a | |
---|
324 | n/a | def _g(x): |
---|
325 | n/a | yield x |
---|
326 | n/a | |
---|
327 | n/a | class DisTests(unittest.TestCase): |
---|
328 | n/a | |
---|
329 | n/a | def get_disassembly(self, func, lasti=-1, wrapper=True): |
---|
330 | n/a | # We want to test the default printing behaviour, not the file arg |
---|
331 | n/a | output = io.StringIO() |
---|
332 | n/a | with contextlib.redirect_stdout(output): |
---|
333 | n/a | if wrapper: |
---|
334 | n/a | dis.dis(func) |
---|
335 | n/a | else: |
---|
336 | n/a | dis.disassemble(func, lasti) |
---|
337 | n/a | return output.getvalue() |
---|
338 | n/a | |
---|
339 | n/a | def get_disassemble_as_string(self, func, lasti=-1): |
---|
340 | n/a | return self.get_disassembly(func, lasti, False) |
---|
341 | n/a | |
---|
342 | n/a | def strip_addresses(self, text): |
---|
343 | n/a | return re.sub(r'\b0x[0-9A-Fa-f]+\b', '0x...', text) |
---|
344 | n/a | |
---|
345 | n/a | def do_disassembly_test(self, func, expected): |
---|
346 | n/a | got = self.get_disassembly(func) |
---|
347 | n/a | if got != expected: |
---|
348 | n/a | got = self.strip_addresses(got) |
---|
349 | n/a | self.assertEqual(got, expected) |
---|
350 | n/a | |
---|
351 | n/a | def test_opmap(self): |
---|
352 | n/a | self.assertEqual(dis.opmap["NOP"], 9) |
---|
353 | n/a | self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst) |
---|
354 | n/a | self.assertIn(dis.opmap["STORE_NAME"], dis.hasname) |
---|
355 | n/a | |
---|
356 | n/a | def test_opname(self): |
---|
357 | n/a | self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST") |
---|
358 | n/a | |
---|
359 | n/a | def test_boundaries(self): |
---|
360 | n/a | self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG) |
---|
361 | n/a | self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT) |
---|
362 | n/a | |
---|
363 | n/a | def test_dis(self): |
---|
364 | n/a | self.do_disassembly_test(_f, dis_f) |
---|
365 | n/a | |
---|
366 | n/a | def test_bug_708901(self): |
---|
367 | n/a | self.do_disassembly_test(bug708901, dis_bug708901) |
---|
368 | n/a | |
---|
369 | n/a | def test_bug_1333982(self): |
---|
370 | n/a | # This one is checking bytecodes generated for an `assert` statement, |
---|
371 | n/a | # so fails if the tests are run with -O. Skip this test then. |
---|
372 | n/a | if not __debug__: |
---|
373 | n/a | self.skipTest('need asserts, run without -O') |
---|
374 | n/a | |
---|
375 | n/a | self.do_disassembly_test(bug1333982, dis_bug1333982) |
---|
376 | n/a | |
---|
377 | n/a | def test_big_linenos(self): |
---|
378 | n/a | def func(count): |
---|
379 | n/a | namespace = {} |
---|
380 | n/a | func = "def foo():\n " + "".join(["\n "] * count + ["spam\n"]) |
---|
381 | n/a | exec(func, namespace) |
---|
382 | n/a | return namespace['foo'] |
---|
383 | n/a | |
---|
384 | n/a | # Test all small ranges |
---|
385 | n/a | for i in range(1, 300): |
---|
386 | n/a | expected = _BIG_LINENO_FORMAT % (i + 2) |
---|
387 | n/a | self.do_disassembly_test(func(i), expected) |
---|
388 | n/a | |
---|
389 | n/a | # Test some larger ranges too |
---|
390 | n/a | for i in range(300, 5000, 10): |
---|
391 | n/a | expected = _BIG_LINENO_FORMAT % (i + 2) |
---|
392 | n/a | self.do_disassembly_test(func(i), expected) |
---|
393 | n/a | |
---|
394 | n/a | from test import dis_module |
---|
395 | n/a | self.do_disassembly_test(dis_module, dis_module_expected_results) |
---|
396 | n/a | |
---|
397 | n/a | def test_disassemble_str(self): |
---|
398 | n/a | self.do_disassembly_test(expr_str, dis_expr_str) |
---|
399 | n/a | self.do_disassembly_test(simple_stmt_str, dis_simple_stmt_str) |
---|
400 | n/a | self.do_disassembly_test(annot_stmt_str, dis_annot_stmt_str) |
---|
401 | n/a | self.do_disassembly_test(compound_stmt_str, dis_compound_stmt_str) |
---|
402 | n/a | |
---|
403 | n/a | def test_disassemble_bytes(self): |
---|
404 | n/a | self.do_disassembly_test(_f.__code__.co_code, dis_f_co_code) |
---|
405 | n/a | |
---|
406 | n/a | def test_disassemble_class(self): |
---|
407 | n/a | self.do_disassembly_test(_C, dis_c) |
---|
408 | n/a | |
---|
409 | n/a | def test_disassemble_instance_method(self): |
---|
410 | n/a | self.do_disassembly_test(_C(1).__init__, dis_c_instance_method) |
---|
411 | n/a | |
---|
412 | n/a | def test_disassemble_instance_method_bytes(self): |
---|
413 | n/a | method_bytecode = _C(1).__init__.__code__.co_code |
---|
414 | n/a | self.do_disassembly_test(method_bytecode, dis_c_instance_method_bytes) |
---|
415 | n/a | |
---|
416 | n/a | def test_disassemble_static_method(self): |
---|
417 | n/a | self.do_disassembly_test(_C.sm, dis_c_static_method) |
---|
418 | n/a | |
---|
419 | n/a | def test_disassemble_class_method(self): |
---|
420 | n/a | self.do_disassembly_test(_C.cm, dis_c_class_method) |
---|
421 | n/a | |
---|
422 | n/a | def test_disassemble_generator(self): |
---|
423 | n/a | gen_func_disas = self.get_disassembly(_g) # Disassemble generator function |
---|
424 | n/a | gen_disas = self.get_disassembly(_g(1)) # Disassemble generator itself |
---|
425 | n/a | self.assertEqual(gen_disas, gen_func_disas) |
---|
426 | n/a | |
---|
427 | n/a | def test_disassemble_fstring(self): |
---|
428 | n/a | self.do_disassembly_test(_fstring, dis_fstring) |
---|
429 | n/a | |
---|
430 | n/a | def test_dis_none(self): |
---|
431 | n/a | try: |
---|
432 | n/a | del sys.last_traceback |
---|
433 | n/a | except AttributeError: |
---|
434 | n/a | pass |
---|
435 | n/a | self.assertRaises(RuntimeError, dis.dis, None) |
---|
436 | n/a | |
---|
437 | n/a | def test_dis_traceback(self): |
---|
438 | n/a | try: |
---|
439 | n/a | del sys.last_traceback |
---|
440 | n/a | except AttributeError: |
---|
441 | n/a | pass |
---|
442 | n/a | |
---|
443 | n/a | try: |
---|
444 | n/a | 1/0 |
---|
445 | n/a | except Exception as e: |
---|
446 | n/a | tb = e.__traceback__ |
---|
447 | n/a | sys.last_traceback = tb |
---|
448 | n/a | |
---|
449 | n/a | tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti) |
---|
450 | n/a | self.do_disassembly_test(None, tb_dis) |
---|
451 | n/a | |
---|
452 | n/a | def test_dis_object(self): |
---|
453 | n/a | self.assertRaises(TypeError, dis.dis, object()) |
---|
454 | n/a | |
---|
455 | n/a | class DisWithFileTests(DisTests): |
---|
456 | n/a | |
---|
457 | n/a | # Run the tests again, using the file arg instead of print |
---|
458 | n/a | def get_disassembly(self, func, lasti=-1, wrapper=True): |
---|
459 | n/a | output = io.StringIO() |
---|
460 | n/a | if wrapper: |
---|
461 | n/a | dis.dis(func, file=output) |
---|
462 | n/a | else: |
---|
463 | n/a | dis.disassemble(func, lasti, file=output) |
---|
464 | n/a | return output.getvalue() |
---|
465 | n/a | |
---|
466 | n/a | |
---|
467 | n/a | |
---|
468 | n/a | code_info_code_info = """\ |
---|
469 | n/a | Name: code_info |
---|
470 | n/a | Filename: (.*) |
---|
471 | n/a | Argument count: 1 |
---|
472 | n/a | Kw-only arguments: 0 |
---|
473 | n/a | Number of locals: 1 |
---|
474 | n/a | Stack size: 3 |
---|
475 | n/a | Flags: OPTIMIZED, NEWLOCALS, NOFREE |
---|
476 | n/a | Constants: |
---|
477 | n/a | 0: %r |
---|
478 | n/a | Names: |
---|
479 | n/a | 0: _format_code_info |
---|
480 | n/a | 1: _get_code_object |
---|
481 | n/a | Variable names: |
---|
482 | n/a | 0: x""" % (('Formatted details of methods, functions, or code.',) |
---|
483 | n/a | if sys.flags.optimize < 2 else (None,)) |
---|
484 | n/a | |
---|
485 | n/a | @staticmethod |
---|
486 | n/a | def tricky(x, y, z=True, *args, c, d, e=[], **kwds): |
---|
487 | n/a | def f(c=c): |
---|
488 | n/a | print(x, y, z, c, d, e, f) |
---|
489 | n/a | yield x, y, z, c, d, e, f |
---|
490 | n/a | |
---|
491 | n/a | code_info_tricky = """\ |
---|
492 | n/a | Name: tricky |
---|
493 | n/a | Filename: (.*) |
---|
494 | n/a | Argument count: 3 |
---|
495 | n/a | Kw-only arguments: 3 |
---|
496 | n/a | Number of locals: 8 |
---|
497 | n/a | Stack size: 7 |
---|
498 | n/a | Flags: OPTIMIZED, NEWLOCALS, VARARGS, VARKEYWORDS, GENERATOR |
---|
499 | n/a | Constants: |
---|
500 | n/a | 0: None |
---|
501 | n/a | 1: <code object f at (.*), file "(.*)", line (.*)> |
---|
502 | n/a | 2: 'tricky.<locals>.f' |
---|
503 | n/a | Variable names: |
---|
504 | n/a | 0: x |
---|
505 | n/a | 1: y |
---|
506 | n/a | 2: z |
---|
507 | n/a | 3: c |
---|
508 | n/a | 4: d |
---|
509 | n/a | 5: e |
---|
510 | n/a | 6: args |
---|
511 | n/a | 7: kwds |
---|
512 | n/a | Cell variables: |
---|
513 | n/a | 0: [edfxyz] |
---|
514 | n/a | 1: [edfxyz] |
---|
515 | n/a | 2: [edfxyz] |
---|
516 | n/a | 3: [edfxyz] |
---|
517 | n/a | 4: [edfxyz] |
---|
518 | n/a | 5: [edfxyz]""" |
---|
519 | n/a | # NOTE: the order of the cell variables above depends on dictionary order! |
---|
520 | n/a | |
---|
521 | n/a | co_tricky_nested_f = tricky.__func__.__code__.co_consts[1] |
---|
522 | n/a | |
---|
523 | n/a | code_info_tricky_nested_f = """\ |
---|
524 | n/a | Name: f |
---|
525 | n/a | Filename: (.*) |
---|
526 | n/a | Argument count: 1 |
---|
527 | n/a | Kw-only arguments: 0 |
---|
528 | n/a | Number of locals: 1 |
---|
529 | n/a | Stack size: 8 |
---|
530 | n/a | Flags: OPTIMIZED, NEWLOCALS, NESTED |
---|
531 | n/a | Constants: |
---|
532 | n/a | 0: None |
---|
533 | n/a | Names: |
---|
534 | n/a | 0: print |
---|
535 | n/a | Variable names: |
---|
536 | n/a | 0: c |
---|
537 | n/a | Free variables: |
---|
538 | n/a | 0: [edfxyz] |
---|
539 | n/a | 1: [edfxyz] |
---|
540 | n/a | 2: [edfxyz] |
---|
541 | n/a | 3: [edfxyz] |
---|
542 | n/a | 4: [edfxyz] |
---|
543 | n/a | 5: [edfxyz]""" |
---|
544 | n/a | |
---|
545 | n/a | code_info_expr_str = """\ |
---|
546 | n/a | Name: <module> |
---|
547 | n/a | Filename: <disassembly> |
---|
548 | n/a | Argument count: 0 |
---|
549 | n/a | Kw-only arguments: 0 |
---|
550 | n/a | Number of locals: 0 |
---|
551 | n/a | Stack size: 2 |
---|
552 | n/a | Flags: NOFREE |
---|
553 | n/a | Constants: |
---|
554 | n/a | 0: 1 |
---|
555 | n/a | Names: |
---|
556 | n/a | 0: x""" |
---|
557 | n/a | |
---|
558 | n/a | code_info_simple_stmt_str = """\ |
---|
559 | n/a | Name: <module> |
---|
560 | n/a | Filename: <disassembly> |
---|
561 | n/a | Argument count: 0 |
---|
562 | n/a | Kw-only arguments: 0 |
---|
563 | n/a | Number of locals: 0 |
---|
564 | n/a | Stack size: 2 |
---|
565 | n/a | Flags: NOFREE |
---|
566 | n/a | Constants: |
---|
567 | n/a | 0: 1 |
---|
568 | n/a | 1: None |
---|
569 | n/a | Names: |
---|
570 | n/a | 0: x""" |
---|
571 | n/a | |
---|
572 | n/a | code_info_compound_stmt_str = """\ |
---|
573 | n/a | Name: <module> |
---|
574 | n/a | Filename: <disassembly> |
---|
575 | n/a | Argument count: 0 |
---|
576 | n/a | Kw-only arguments: 0 |
---|
577 | n/a | Number of locals: 0 |
---|
578 | n/a | Stack size: 2 |
---|
579 | n/a | Flags: NOFREE |
---|
580 | n/a | Constants: |
---|
581 | n/a | 0: 0 |
---|
582 | n/a | 1: 1 |
---|
583 | n/a | 2: None |
---|
584 | n/a | Names: |
---|
585 | n/a | 0: x""" |
---|
586 | n/a | |
---|
587 | n/a | |
---|
588 | n/a | async def async_def(): |
---|
589 | n/a | await 1 |
---|
590 | n/a | async for a in b: pass |
---|
591 | n/a | async with c as d: pass |
---|
592 | n/a | |
---|
593 | n/a | code_info_async_def = """\ |
---|
594 | n/a | Name: async_def |
---|
595 | n/a | Filename: (.*) |
---|
596 | n/a | Argument count: 0 |
---|
597 | n/a | Kw-only arguments: 0 |
---|
598 | n/a | Number of locals: 2 |
---|
599 | n/a | Stack size: 17 |
---|
600 | n/a | Flags: OPTIMIZED, NEWLOCALS, NOFREE, COROUTINE |
---|
601 | n/a | Constants: |
---|
602 | n/a | 0: None |
---|
603 | n/a | 1: 1""" |
---|
604 | n/a | |
---|
605 | n/a | class CodeInfoTests(unittest.TestCase): |
---|
606 | n/a | test_pairs = [ |
---|
607 | n/a | (dis.code_info, code_info_code_info), |
---|
608 | n/a | (tricky, code_info_tricky), |
---|
609 | n/a | (co_tricky_nested_f, code_info_tricky_nested_f), |
---|
610 | n/a | (expr_str, code_info_expr_str), |
---|
611 | n/a | (simple_stmt_str, code_info_simple_stmt_str), |
---|
612 | n/a | (compound_stmt_str, code_info_compound_stmt_str), |
---|
613 | n/a | (async_def, code_info_async_def) |
---|
614 | n/a | ] |
---|
615 | n/a | |
---|
616 | n/a | def test_code_info(self): |
---|
617 | n/a | self.maxDiff = 1000 |
---|
618 | n/a | for x, expected in self.test_pairs: |
---|
619 | n/a | self.assertRegex(dis.code_info(x), expected) |
---|
620 | n/a | |
---|
621 | n/a | def test_show_code(self): |
---|
622 | n/a | self.maxDiff = 1000 |
---|
623 | n/a | for x, expected in self.test_pairs: |
---|
624 | n/a | with captured_stdout() as output: |
---|
625 | n/a | dis.show_code(x) |
---|
626 | n/a | self.assertRegex(output.getvalue(), expected+"\n") |
---|
627 | n/a | output = io.StringIO() |
---|
628 | n/a | dis.show_code(x, file=output) |
---|
629 | n/a | self.assertRegex(output.getvalue(), expected) |
---|
630 | n/a | |
---|
631 | n/a | def test_code_info_object(self): |
---|
632 | n/a | self.assertRaises(TypeError, dis.code_info, object()) |
---|
633 | n/a | |
---|
634 | n/a | def test_pretty_flags_no_flags(self): |
---|
635 | n/a | self.assertEqual(dis.pretty_flags(0), '0x0') |
---|
636 | n/a | |
---|
637 | n/a | |
---|
638 | n/a | # Fodder for instruction introspection tests |
---|
639 | n/a | # Editing any of these may require recalculating the expected output |
---|
640 | n/a | def outer(a=1, b=2): |
---|
641 | n/a | def f(c=3, d=4): |
---|
642 | n/a | def inner(e=5, f=6): |
---|
643 | n/a | print(a, b, c, d, e, f) |
---|
644 | n/a | print(a, b, c, d) |
---|
645 | n/a | return inner |
---|
646 | n/a | print(a, b, '', 1, [], {}, "Hello world!") |
---|
647 | n/a | return f |
---|
648 | n/a | |
---|
649 | n/a | def jumpy(): |
---|
650 | n/a | # This won't actually run (but that's OK, we only disassemble it) |
---|
651 | n/a | for i in range(10): |
---|
652 | n/a | print(i) |
---|
653 | n/a | if i < 4: |
---|
654 | n/a | continue |
---|
655 | n/a | if i > 6: |
---|
656 | n/a | break |
---|
657 | n/a | else: |
---|
658 | n/a | print("I can haz else clause?") |
---|
659 | n/a | while i: |
---|
660 | n/a | print(i) |
---|
661 | n/a | i -= 1 |
---|
662 | n/a | if i > 6: |
---|
663 | n/a | continue |
---|
664 | n/a | if i < 4: |
---|
665 | n/a | break |
---|
666 | n/a | else: |
---|
667 | n/a | print("Who let lolcatz into this test suite?") |
---|
668 | n/a | try: |
---|
669 | n/a | 1 / 0 |
---|
670 | n/a | except ZeroDivisionError: |
---|
671 | n/a | print("Here we go, here we go, here we go...") |
---|
672 | n/a | else: |
---|
673 | n/a | with i as dodgy: |
---|
674 | n/a | print("Never reach this") |
---|
675 | n/a | finally: |
---|
676 | n/a | print("OK, now we're done") |
---|
677 | n/a | |
---|
678 | n/a | # End fodder for opinfo generation tests |
---|
679 | n/a | expected_outer_line = 1 |
---|
680 | n/a | _line_offset = outer.__code__.co_firstlineno - 1 |
---|
681 | n/a | code_object_f = outer.__code__.co_consts[3] |
---|
682 | n/a | expected_f_line = code_object_f.co_firstlineno - _line_offset |
---|
683 | n/a | code_object_inner = code_object_f.co_consts[3] |
---|
684 | n/a | expected_inner_line = code_object_inner.co_firstlineno - _line_offset |
---|
685 | n/a | expected_jumpy_line = 1 |
---|
686 | n/a | |
---|
687 | n/a | # The following lines are useful to regenerate the expected results after |
---|
688 | n/a | # either the fodder is modified or the bytecode generation changes |
---|
689 | n/a | # After regeneration, update the references to code_object_f and |
---|
690 | n/a | # code_object_inner before rerunning the tests |
---|
691 | n/a | |
---|
692 | n/a | #_instructions = dis.get_instructions(outer, first_line=expected_outer_line) |
---|
693 | n/a | #print('expected_opinfo_outer = [\n ', |
---|
694 | n/a | #',\n '.join(map(str, _instructions)), ',\n]', sep='') |
---|
695 | n/a | #_instructions = dis.get_instructions(outer(), first_line=expected_f_line) |
---|
696 | n/a | #print('expected_opinfo_f = [\n ', |
---|
697 | n/a | #',\n '.join(map(str, _instructions)), ',\n]', sep='') |
---|
698 | n/a | #_instructions = dis.get_instructions(outer()(), first_line=expected_inner_line) |
---|
699 | n/a | #print('expected_opinfo_inner = [\n ', |
---|
700 | n/a | #',\n '.join(map(str, _instructions)), ',\n]', sep='') |
---|
701 | n/a | #_instructions = dis.get_instructions(jumpy, first_line=expected_jumpy_line) |
---|
702 | n/a | #print('expected_opinfo_jumpy = [\n ', |
---|
703 | n/a | #',\n '.join(map(str, _instructions)), ',\n]', sep='') |
---|
704 | n/a | |
---|
705 | n/a | |
---|
706 | n/a | Instruction = dis.Instruction |
---|
707 | n/a | expected_opinfo_outer = [ |
---|
708 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval=(3, 4), argrepr='(3, 4)', offset=0, starts_line=2, is_jump_target=False), |
---|
709 | n/a | Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False), |
---|
710 | n/a | Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False), |
---|
711 | n/a | Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=6, starts_line=None, is_jump_target=False), |
---|
712 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_f, argrepr=repr(code_object_f), offset=8, starts_line=None, is_jump_target=False), |
---|
713 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f', argrepr="'outer.<locals>.f'", offset=10, starts_line=None, is_jump_target=False), |
---|
714 | n/a | Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='', offset=12, starts_line=None, is_jump_target=False), |
---|
715 | n/a | Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=14, starts_line=None, is_jump_target=False), |
---|
716 | n/a | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=16, starts_line=7, is_jump_target=False), |
---|
717 | n/a | Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=18, starts_line=None, is_jump_target=False), |
---|
718 | n/a | Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=20, starts_line=None, is_jump_target=False), |
---|
719 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=22, starts_line=None, is_jump_target=False), |
---|
720 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=24, starts_line=None, is_jump_target=False), |
---|
721 | n/a | Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=26, starts_line=None, is_jump_target=False), |
---|
722 | n/a | Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=28, starts_line=None, is_jump_target=False), |
---|
723 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval='Hello world!', argrepr="'Hello world!'", offset=30, starts_line=None, is_jump_target=False), |
---|
724 | n/a | Instruction(opname='CALL_FUNCTION', opcode=131, arg=7, argval=7, argrepr='', offset=32, starts_line=None, is_jump_target=False), |
---|
725 | n/a | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=34, starts_line=None, is_jump_target=False), |
---|
726 | n/a | Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=36, starts_line=8, is_jump_target=False), |
---|
727 | n/a | Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=38, starts_line=None, is_jump_target=False), |
---|
728 | n/a | ] |
---|
729 | n/a | |
---|
730 | n/a | expected_opinfo_f = [ |
---|
731 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=(5, 6), argrepr='(5, 6)', offset=0, starts_line=3, is_jump_target=False), |
---|
732 | n/a | Instruction(opname='LOAD_CLOSURE', opcode=135, arg=2, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False), |
---|
733 | n/a | Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False), |
---|
734 | n/a | Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False), |
---|
735 | n/a | Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False), |
---|
736 | n/a | Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=10, starts_line=None, is_jump_target=False), |
---|
737 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_inner, argrepr=repr(code_object_inner), offset=12, starts_line=None, is_jump_target=False), |
---|
738 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f.<locals>.inner', argrepr="'outer.<locals>.f.<locals>.inner'", offset=14, starts_line=None, is_jump_target=False), |
---|
739 | n/a | Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='', offset=16, starts_line=None, is_jump_target=False), |
---|
740 | n/a | Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=18, starts_line=None, is_jump_target=False), |
---|
741 | n/a | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=20, starts_line=5, is_jump_target=False), |
---|
742 | n/a | Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='a', argrepr='a', offset=22, starts_line=None, is_jump_target=False), |
---|
743 | n/a | Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='b', argrepr='b', offset=24, starts_line=None, is_jump_target=False), |
---|
744 | n/a | Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='c', argrepr='c', offset=26, starts_line=None, is_jump_target=False), |
---|
745 | n/a | Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='d', argrepr='d', offset=28, starts_line=None, is_jump_target=False), |
---|
746 | n/a | Instruction(opname='CALL_FUNCTION', opcode=131, arg=4, argval=4, argrepr='', offset=30, starts_line=None, is_jump_target=False), |
---|
747 | n/a | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=32, starts_line=None, is_jump_target=False), |
---|
748 | n/a | Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=34, starts_line=6, is_jump_target=False), |
---|
749 | n/a | Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=36, starts_line=None, is_jump_target=False), |
---|
750 | n/a | ] |
---|
751 | n/a | |
---|
752 | n/a | expected_opinfo_inner = [ |
---|
753 | n/a | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=0, starts_line=4, is_jump_target=False), |
---|
754 | n/a | Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False), |
---|
755 | n/a | Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False), |
---|
756 | n/a | Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False), |
---|
757 | n/a | Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False), |
---|
758 | n/a | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=10, starts_line=None, is_jump_target=False), |
---|
759 | n/a | Instruction(opname='LOAD_FAST', opcode=124, arg=1, argval='f', argrepr='f', offset=12, starts_line=None, is_jump_target=False), |
---|
760 | n/a | Instruction(opname='CALL_FUNCTION', opcode=131, arg=6, argval=6, argrepr='', offset=14, starts_line=None, is_jump_target=False), |
---|
761 | n/a | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=16, starts_line=None, is_jump_target=False), |
---|
762 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=18, starts_line=None, is_jump_target=False), |
---|
763 | n/a | Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=20, starts_line=None, is_jump_target=False), |
---|
764 | n/a | ] |
---|
765 | n/a | |
---|
766 | n/a | expected_opinfo_jumpy = [ |
---|
767 | n/a | Instruction(opname='SETUP_LOOP', opcode=120, arg=52, argval=54, argrepr='to 54', offset=0, starts_line=3, is_jump_target=False), |
---|
768 | n/a | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr='range', offset=2, starts_line=None, is_jump_target=False), |
---|
769 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=10, argrepr='10', offset=4, starts_line=None, is_jump_target=False), |
---|
770 | n/a | Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=6, starts_line=None, is_jump_target=False), |
---|
771 | n/a | Instruction(opname='GET_ITER', opcode=68, arg=None, argval=None, argrepr='', offset=8, starts_line=None, is_jump_target=False), |
---|
772 | n/a | Instruction(opname='FOR_ITER', opcode=93, arg=32, argval=44, argrepr='to 44', offset=10, starts_line=None, is_jump_target=True), |
---|
773 | n/a | Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=12, starts_line=None, is_jump_target=False), |
---|
774 | n/a | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=14, starts_line=4, is_jump_target=False), |
---|
775 | n/a | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=16, starts_line=None, is_jump_target=False), |
---|
776 | n/a | Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=18, starts_line=None, is_jump_target=False), |
---|
777 | n/a | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=20, starts_line=None, is_jump_target=False), |
---|
778 | n/a | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=22, starts_line=5, is_jump_target=False), |
---|
779 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=24, starts_line=None, is_jump_target=False), |
---|
780 | n/a | Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=26, starts_line=None, is_jump_target=False), |
---|
781 | n/a | Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=32, argval=32, argrepr='', offset=28, starts_line=None, is_jump_target=False), |
---|
782 | n/a | Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=10, argval=10, argrepr='', offset=30, starts_line=6, is_jump_target=False), |
---|
783 | n/a | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=32, starts_line=7, is_jump_target=True), |
---|
784 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=34, starts_line=None, is_jump_target=False), |
---|
785 | n/a | Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=36, starts_line=None, is_jump_target=False), |
---|
786 | n/a | Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=10, argval=10, argrepr='', offset=38, starts_line=None, is_jump_target=False), |
---|
787 | n/a | Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=40, starts_line=8, is_jump_target=False), |
---|
788 | n/a | Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=10, argval=10, argrepr='', offset=42, starts_line=None, is_jump_target=False), |
---|
789 | n/a | Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=44, starts_line=None, is_jump_target=True), |
---|
790 | n/a | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=46, starts_line=10, is_jump_target=False), |
---|
791 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='I can haz else clause?', argrepr="'I can haz else clause?'", offset=48, starts_line=None, is_jump_target=False), |
---|
792 | n/a | Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=50, starts_line=None, is_jump_target=False), |
---|
793 | n/a | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=52, starts_line=None, is_jump_target=False), |
---|
794 | n/a | Instruction(opname='SETUP_LOOP', opcode=120, arg=52, argval=108, argrepr='to 108', offset=54, starts_line=11, is_jump_target=True), |
---|
795 | n/a | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=56, starts_line=None, is_jump_target=True), |
---|
796 | n/a | Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=98, argval=98, argrepr='', offset=58, starts_line=None, is_jump_target=False), |
---|
797 | n/a | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=60, starts_line=12, is_jump_target=False), |
---|
798 | n/a | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=62, starts_line=None, is_jump_target=False), |
---|
799 | n/a | Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=64, starts_line=None, is_jump_target=False), |
---|
800 | n/a | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=66, starts_line=None, is_jump_target=False), |
---|
801 | n/a | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=68, starts_line=13, is_jump_target=False), |
---|
802 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=70, starts_line=None, is_jump_target=False), |
---|
803 | n/a | Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=72, starts_line=None, is_jump_target=False), |
---|
804 | n/a | Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=74, starts_line=None, is_jump_target=False), |
---|
805 | n/a | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=76, starts_line=14, is_jump_target=False), |
---|
806 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=78, starts_line=None, is_jump_target=False), |
---|
807 | n/a | Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=80, starts_line=None, is_jump_target=False), |
---|
808 | n/a | Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=86, argval=86, argrepr='', offset=82, starts_line=None, is_jump_target=False), |
---|
809 | n/a | Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=56, argval=56, argrepr='', offset=84, starts_line=15, is_jump_target=False), |
---|
810 | n/a | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=86, starts_line=16, is_jump_target=True), |
---|
811 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=88, starts_line=None, is_jump_target=False), |
---|
812 | n/a | Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=90, starts_line=None, is_jump_target=False), |
---|
813 | n/a | Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=56, argval=56, argrepr='', offset=92, starts_line=None, is_jump_target=False), |
---|
814 | n/a | Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=94, starts_line=17, is_jump_target=False), |
---|
815 | n/a | Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=56, argval=56, argrepr='', offset=96, starts_line=None, is_jump_target=False), |
---|
816 | n/a | Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=98, starts_line=None, is_jump_target=True), |
---|
817 | n/a | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=100, starts_line=19, is_jump_target=False), |
---|
818 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval='Who let lolcatz into this test suite?', argrepr="'Who let lolcatz into this test suite?'", offset=102, starts_line=None, is_jump_target=False), |
---|
819 | n/a | Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=104, starts_line=None, is_jump_target=False), |
---|
820 | n/a | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=106, starts_line=None, is_jump_target=False), |
---|
821 | n/a | Instruction(opname='SETUP_FINALLY', opcode=122, arg=70, argval=180, argrepr='to 180', offset=108, starts_line=20, is_jump_target=True), |
---|
822 | n/a | Instruction(opname='SETUP_EXCEPT', opcode=121, arg=12, argval=124, argrepr='to 124', offset=110, starts_line=None, is_jump_target=False), |
---|
823 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=112, starts_line=21, is_jump_target=False), |
---|
824 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=114, starts_line=None, is_jump_target=False), |
---|
825 | n/a | Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=116, starts_line=None, is_jump_target=False), |
---|
826 | n/a | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=118, starts_line=None, is_jump_target=False), |
---|
827 | n/a | Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=120, starts_line=None, is_jump_target=False), |
---|
828 | n/a | Instruction(opname='JUMP_FORWARD', opcode=110, arg=28, argval=152, argrepr='to 152', offset=122, starts_line=None, is_jump_target=False), |
---|
829 | n/a | Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=124, starts_line=22, is_jump_target=True), |
---|
830 | n/a | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=126, starts_line=None, is_jump_target=False), |
---|
831 | n/a | Instruction(opname='COMPARE_OP', opcode=107, arg=10, argval='exception match', argrepr='exception match', offset=128, starts_line=None, is_jump_target=False), |
---|
832 | n/a | Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=150, argval=150, argrepr='', offset=130, starts_line=None, is_jump_target=False), |
---|
833 | n/a | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=132, starts_line=None, is_jump_target=False), |
---|
834 | n/a | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=134, starts_line=None, is_jump_target=False), |
---|
835 | n/a | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=136, starts_line=None, is_jump_target=False), |
---|
836 | n/a | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=138, starts_line=23, is_jump_target=False), |
---|
837 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval='Here we go, here we go, here we go...', argrepr="'Here we go, here we go, here we go...'", offset=140, starts_line=None, is_jump_target=False), |
---|
838 | n/a | Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=142, starts_line=None, is_jump_target=False), |
---|
839 | n/a | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=144, starts_line=None, is_jump_target=False), |
---|
840 | n/a | Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=146, starts_line=None, is_jump_target=False), |
---|
841 | n/a | Instruction(opname='JUMP_FORWARD', opcode=110, arg=26, argval=176, argrepr='to 176', offset=148, starts_line=None, is_jump_target=False), |
---|
842 | n/a | Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=150, starts_line=None, is_jump_target=True), |
---|
843 | n/a | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=152, starts_line=25, is_jump_target=True), |
---|
844 | n/a | Instruction(opname='SETUP_WITH', opcode=143, arg=14, argval=170, argrepr='to 170', offset=154, starts_line=None, is_jump_target=False), |
---|
845 | n/a | Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=156, starts_line=None, is_jump_target=False), |
---|
846 | n/a | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=158, starts_line=26, is_jump_target=False), |
---|
847 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=9, argval='Never reach this', argrepr="'Never reach this'", offset=160, starts_line=None, is_jump_target=False), |
---|
848 | n/a | Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=162, starts_line=None, is_jump_target=False), |
---|
849 | n/a | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=164, starts_line=None, is_jump_target=False), |
---|
850 | n/a | Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=166, starts_line=None, is_jump_target=False), |
---|
851 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=168, starts_line=None, is_jump_target=False), |
---|
852 | n/a | Instruction(opname='WITH_CLEANUP_START', opcode=81, arg=None, argval=None, argrepr='', offset=170, starts_line=None, is_jump_target=True), |
---|
853 | n/a | Instruction(opname='WITH_CLEANUP_FINISH', opcode=82, arg=None, argval=None, argrepr='', offset=172, starts_line=None, is_jump_target=False), |
---|
854 | n/a | Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=174, starts_line=None, is_jump_target=False), |
---|
855 | n/a | Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=176, starts_line=None, is_jump_target=True), |
---|
856 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=178, starts_line=None, is_jump_target=False), |
---|
857 | n/a | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=180, starts_line=28, is_jump_target=True), |
---|
858 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=182, starts_line=None, is_jump_target=False), |
---|
859 | n/a | Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=184, starts_line=None, is_jump_target=False), |
---|
860 | n/a | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=186, starts_line=None, is_jump_target=False), |
---|
861 | n/a | Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=188, starts_line=None, is_jump_target=False), |
---|
862 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=190, starts_line=None, is_jump_target=False), |
---|
863 | n/a | Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=192, starts_line=None, is_jump_target=False), |
---|
864 | n/a | ] |
---|
865 | n/a | |
---|
866 | n/a | # One last piece of inspect fodder to check the default line number handling |
---|
867 | n/a | def simple(): pass |
---|
868 | n/a | expected_opinfo_simple = [ |
---|
869 | n/a | Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=0, starts_line=simple.__code__.co_firstlineno, is_jump_target=False), |
---|
870 | n/a | Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=2, starts_line=None, is_jump_target=False) |
---|
871 | n/a | ] |
---|
872 | n/a | |
---|
873 | n/a | |
---|
874 | n/a | class InstructionTests(BytecodeTestCase): |
---|
875 | n/a | |
---|
876 | n/a | def test_default_first_line(self): |
---|
877 | n/a | actual = dis.get_instructions(simple) |
---|
878 | n/a | self.assertEqual(list(actual), expected_opinfo_simple) |
---|
879 | n/a | |
---|
880 | n/a | def test_first_line_set_to_None(self): |
---|
881 | n/a | actual = dis.get_instructions(simple, first_line=None) |
---|
882 | n/a | self.assertEqual(list(actual), expected_opinfo_simple) |
---|
883 | n/a | |
---|
884 | n/a | def test_outer(self): |
---|
885 | n/a | actual = dis.get_instructions(outer, first_line=expected_outer_line) |
---|
886 | n/a | self.assertEqual(list(actual), expected_opinfo_outer) |
---|
887 | n/a | |
---|
888 | n/a | def test_nested(self): |
---|
889 | n/a | with captured_stdout(): |
---|
890 | n/a | f = outer() |
---|
891 | n/a | actual = dis.get_instructions(f, first_line=expected_f_line) |
---|
892 | n/a | self.assertEqual(list(actual), expected_opinfo_f) |
---|
893 | n/a | |
---|
894 | n/a | def test_doubly_nested(self): |
---|
895 | n/a | with captured_stdout(): |
---|
896 | n/a | inner = outer()() |
---|
897 | n/a | actual = dis.get_instructions(inner, first_line=expected_inner_line) |
---|
898 | n/a | self.assertEqual(list(actual), expected_opinfo_inner) |
---|
899 | n/a | |
---|
900 | n/a | def test_jumpy(self): |
---|
901 | n/a | actual = dis.get_instructions(jumpy, first_line=expected_jumpy_line) |
---|
902 | n/a | self.assertEqual(list(actual), expected_opinfo_jumpy) |
---|
903 | n/a | |
---|
904 | n/a | # get_instructions has its own tests above, so can rely on it to validate |
---|
905 | n/a | # the object oriented API |
---|
906 | n/a | class BytecodeTests(unittest.TestCase): |
---|
907 | n/a | def test_instantiation(self): |
---|
908 | n/a | # Test with function, method, code string and code object |
---|
909 | n/a | for obj in [_f, _C(1).__init__, "a=1", _f.__code__]: |
---|
910 | n/a | with self.subTest(obj=obj): |
---|
911 | n/a | b = dis.Bytecode(obj) |
---|
912 | n/a | self.assertIsInstance(b.codeobj, types.CodeType) |
---|
913 | n/a | |
---|
914 | n/a | self.assertRaises(TypeError, dis.Bytecode, object()) |
---|
915 | n/a | |
---|
916 | n/a | def test_iteration(self): |
---|
917 | n/a | for obj in [_f, _C(1).__init__, "a=1", _f.__code__]: |
---|
918 | n/a | with self.subTest(obj=obj): |
---|
919 | n/a | via_object = list(dis.Bytecode(obj)) |
---|
920 | n/a | via_generator = list(dis.get_instructions(obj)) |
---|
921 | n/a | self.assertEqual(via_object, via_generator) |
---|
922 | n/a | |
---|
923 | n/a | def test_explicit_first_line(self): |
---|
924 | n/a | actual = dis.Bytecode(outer, first_line=expected_outer_line) |
---|
925 | n/a | self.assertEqual(list(actual), expected_opinfo_outer) |
---|
926 | n/a | |
---|
927 | n/a | def test_source_line_in_disassembly(self): |
---|
928 | n/a | # Use the line in the source code |
---|
929 | n/a | actual = dis.Bytecode(simple).dis()[:3] |
---|
930 | n/a | expected = "{:>3}".format(simple.__code__.co_firstlineno) |
---|
931 | n/a | self.assertEqual(actual, expected) |
---|
932 | n/a | # Use an explicit first line number |
---|
933 | n/a | actual = dis.Bytecode(simple, first_line=350).dis()[:3] |
---|
934 | n/a | self.assertEqual(actual, "350") |
---|
935 | n/a | |
---|
936 | n/a | def test_info(self): |
---|
937 | n/a | self.maxDiff = 1000 |
---|
938 | n/a | for x, expected in CodeInfoTests.test_pairs: |
---|
939 | n/a | b = dis.Bytecode(x) |
---|
940 | n/a | self.assertRegex(b.info(), expected) |
---|
941 | n/a | |
---|
942 | n/a | def test_disassembled(self): |
---|
943 | n/a | actual = dis.Bytecode(_f).dis() |
---|
944 | n/a | self.assertEqual(actual, dis_f) |
---|
945 | n/a | |
---|
946 | n/a | def test_from_traceback(self): |
---|
947 | n/a | tb = get_tb() |
---|
948 | n/a | b = dis.Bytecode.from_traceback(tb) |
---|
949 | n/a | while tb.tb_next: tb = tb.tb_next |
---|
950 | n/a | |
---|
951 | n/a | self.assertEqual(b.current_offset, tb.tb_lasti) |
---|
952 | n/a | |
---|
953 | n/a | def test_from_traceback_dis(self): |
---|
954 | n/a | tb = get_tb() |
---|
955 | n/a | b = dis.Bytecode.from_traceback(tb) |
---|
956 | n/a | self.assertEqual(b.dis(), dis_traceback) |
---|
957 | n/a | |
---|
958 | n/a | if __name__ == "__main__": |
---|
959 | n/a | unittest.main() |
---|