Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(2644)

Side by Side Diff: Lib/test/test_dis.py

Issue 11816: Add functions to return disassembly as string
Patch Set: Created 2 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/dis.py ('k') | Lib/test/test_peepholer.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Minimal tests for dis module 1 # Minimal tests for dis module
2 2
3 from test.support import run_unittest, captured_stdout 3 from test.support import run_unittest, captured_stdout
4 import difflib 4 import difflib
5 import unittest 5 import unittest
6 import sys 6 import sys
7 import dis 7 import dis
8 import io
9 8
10 class _C: 9 class _C:
11 def __init__(self, x): 10 def __init__(self, x):
12 self.x = x == 1 11 self.x = x == 1
13 12
14 dis_c_instance_method = """\ 13 dis_c_instance_method = """\
15 %-4d 0 LOAD_FAST 1 (x) 14 %-4d 0 LOAD_FAST 1 (x)
16 3 LOAD_CONST 1 (1) 15 3 LOAD_CONST 1 (1)
17 6 COMPARE_OP 2 (==) 16 6 COMPARE_OP 2 (==)
18 9 LOAD_FAST 0 (self) 17 9 LOAD_FAST 0 (self)
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 15 INPLACE_ADD 166 15 INPLACE_ADD
168 16 STORE_NAME 0 (x) 167 16 STORE_NAME 0 (x)
169 19 JUMP_ABSOLUTE 9 168 19 JUMP_ABSOLUTE 9
170 >> 22 LOAD_CONST 2 (None) 169 >> 22 LOAD_CONST 2 (None)
171 25 RETURN_VALUE 170 25 RETURN_VALUE
172 """ 171 """
173 172
174 class DisTests(unittest.TestCase): 173 class DisTests(unittest.TestCase):
175 174
176 def get_disassembly(self, func, lasti=-1, wrapper=True): 175 def get_disassembly(self, func, lasti=-1, wrapper=True):
177 s = io.StringIO() 176 if wrapper:
178 save_stdout = sys.stdout 177 s = dis.dis_to_str(func)
179 sys.stdout = s 178 else:
180 try: 179 s = dis.disassemble_to_str(func, lasti)
181 if wrapper:
182 dis.dis(func)
183 else:
184 dis.disassemble(func, lasti)
185 finally:
186 sys.stdout = save_stdout
187 # Trim trailing blanks (if any). 180 # Trim trailing blanks (if any).
188 return [line.rstrip() for line in s.getvalue().splitlines()] 181 return [line.rstrip() for line in s.splitlines()]
189 182
190 def get_disassemble_as_string(self, func, lasti=-1): 183 def get_disassemble_as_string(self, func, lasti=-1):
191 return '\n'.join(self.get_disassembly(func, lasti, False)) 184 return '\n'.join(self.get_disassembly(func, lasti, False))
192 185
193 def do_disassembly_test(self, func, expected): 186 def do_disassembly_test(self, func, expected):
194 lines = self.get_disassembly(func) 187 lines = self.get_disassembly(func)
195 expected = expected.splitlines() 188 expected = expected.splitlines()
196 if expected != lines: 189 if expected != lines:
197 self.fail( 190 self.fail(
198 "events did not match expectation:\n" + 191 "events did not match expectation:\n" +
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 442
450 def test_pretty_flags_no_flags(self): 443 def test_pretty_flags_no_flags(self):
451 self.assertEqual(dis.pretty_flags(0), '0x0') 444 self.assertEqual(dis.pretty_flags(0), '0x0')
452 445
453 446
454 def test_main(): 447 def test_main():
455 run_unittest(DisTests, CodeInfoTests) 448 run_unittest(DisTests, CodeInfoTests)
456 449
457 if __name__ == "__main__": 450 if __name__ == "__main__":
458 test_main() 451 test_main()
OLDNEW
« no previous file with comments | « Lib/dis.py ('k') | Lib/test/test_peepholer.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld cbc36f91f3f7