1 # Minimal tests for dis module
3 from test
.test_support
import run_unittest
15 %-4d 0 LOAD_FAST 0 (a)
19 %-4d 5 LOAD_CONST 1 (1)
21 """%(_f
.func_code
.co_firstlineno
+ 1,
22 _f
.func_code
.co_firstlineno
+ 2)
31 %-4d 0 SETUP_LOOP 23 (to 26)
32 3 LOAD_GLOBAL 0 (range)
35 %-4d 9 LOAD_CONST 2 (10)
38 >> 16 FOR_ITER 6 (to 25)
41 %-4d 22 JUMP_ABSOLUTE 16
43 >> 26 LOAD_CONST 0 (None)
45 """%(bug708901
.func_code
.co_firstlineno
+ 1,
46 bug708901
.func_code
.co_firstlineno
+ 2,
47 bug708901
.func_code
.co_firstlineno
+ 3)
51 assert 0, ([s
for s
in x
] +
56 %-4d 0 LOAD_CONST 1 (0)
58 6 LOAD_GLOBAL 0 (AssertionError)
62 >> 16 FOR_ITER 12 (to 31)
68 %-4d >> 31 LOAD_CONST 2 (1)
72 %-4d >> 38 LOAD_CONST 0 (None)
74 """%(bug1333982
.func_code
.co_firstlineno
+ 1,
75 bug1333982
.func_code
.co_firstlineno
+ 2,
76 bug1333982
.func_code
.co_firstlineno
+ 3)
78 _BIG_LINENO_FORMAT
= """\
79 %3d 0 LOAD_GLOBAL 0 (spam)
85 class DisTests(unittest
.TestCase
):
86 def do_disassembly_test(self
, func
, expected
):
87 s
= StringIO
.StringIO()
88 save_stdout
= sys
.stdout
91 sys
.stdout
= save_stdout
93 # Trim trailing blanks (if any).
94 lines
= got
.split('\n')
95 lines
= [line
.rstrip() for line
in lines
]
96 expected
= expected
.split("\n")
100 "events did not match expectation:\n" +
101 "\n".join(difflib
.ndiff(expected
,
104 def test_opmap(self
):
105 self
.assertEqual(dis
.opmap
["STOP_CODE"], 0)
106 self
.assertEqual(dis
.opmap
["LOAD_CONST"] in dis
.hasconst
, True)
107 self
.assertEqual(dis
.opmap
["STORE_NAME"] in dis
.hasname
, True)
109 def test_opname(self
):
110 self
.assertEqual(dis
.opname
[dis
.opmap
["LOAD_FAST"]], "LOAD_FAST")
112 def test_boundaries(self
):
113 self
.assertEqual(dis
.opmap
["EXTENDED_ARG"], dis
.EXTENDED_ARG
)
114 self
.assertEqual(dis
.opmap
["STORE_NAME"], dis
.HAVE_ARGUMENT
)
117 self
.do_disassembly_test(_f
, dis_f
)
119 def test_bug_708901(self
):
120 self
.do_disassembly_test(bug708901
, dis_bug708901
)
122 def test_bug_1333982(self
):
123 # This one is checking bytecodes generated for an `assert` statement,
124 # so fails if the tests are run with -O. Skip this test then.
126 self
.do_disassembly_test(bug1333982
, dis_bug1333982
)
128 def test_big_linenos(self
):
131 func
= "def foo():\n " + "".join(["\n "] * count
+ ["spam\n"])
132 exec func
in namespace
133 return namespace
['foo']
135 # Test all small ranges
136 for i
in xrange(1, 300):
137 expected
= _BIG_LINENO_FORMAT
% (i
+ 2)
138 self
.do_disassembly_test(func(i
), expected
)
140 # Test some larger ranges too
141 for i
in xrange(300, 5000, 10):
142 expected
= _BIG_LINENO_FORMAT
% (i
+ 2)
143 self
.do_disassembly_test(func(i
), expected
)
146 run_unittest(DisTests
)
149 if __name__
== "__main__":