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

Python code coverage for Lib/test/test_dis.py

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