Left: | ||
Right: |
OLD | NEW |
---|---|
1 import dis | 1 import dis |
2 import sys | 2 import sys |
3 from cStringIO import StringIO | 3 from cStringIO import StringIO |
4 import unittest | 4 import unittest |
5 | 5 |
6 def disassemble(func): | 6 def disassemble(func): |
7 f = StringIO() | 7 f = StringIO() |
8 tmp = sys.stdout | 8 tmp = sys.stdout |
9 sys.stdout = f | 9 sys.stdout = f |
10 dis.dis(func) | 10 dis.dis(func) |
11 sys.stdout = tmp | 11 sys.stdout = tmp |
12 result = f.getvalue() | 12 result = f.getvalue() |
13 f.close() | 13 f.close() |
14 return result | 14 return result |
15 | 15 |
16 def dis_single(line): | 16 def dis_single(line): |
17 return disassemble(compile(line, '', 'single')) | 17 return disassemble(compile(line, '', 'single')) |
18 | 18 |
19 class TestTranforms(unittest.TestCase): | 19 class TestTranforms(unittest.TestCase): |
20 | 20 |
21 def test_unot(self): | 21 def test_unot(self): |
22 # UNARY_NOT JUMP_IF_FALSE POP_TOP --> JUMP_IF_TRUE POP_TOP' | 22 # UNARY_NOT POP_JUMP_IF_FALSE --> POP_JUMP_IF_TRUE' |
Collin Winter
2009/02/27 21:33:21
Can you kill the lingering ' at the end of the lin
Jeffrey Yasskin
2009/02/28 01:39:00
Sure.
| |
23 def unot(x): | 23 def unot(x): |
24 if not x == 2: | 24 if not x == 2: |
25 del x | 25 del x |
26 asm = disassemble(unot) | 26 asm = disassemble(unot) |
27 for elem in ('UNARY_NOT', 'JUMP_IF_FALSE'): | 27 for elem in ('UNARY_NOT', 'POP_JUMP_IF_FALSE'): |
28 self.assert_(elem not in asm) | 28 self.assert_(elem not in asm) |
29 for elem in ('JUMP_IF_TRUE', 'POP_TOP'): | 29 for elem in ('POP_JUMP_IF_TRUE',): |
Collin Winter
2009/02/27 21:33:21
No need for this for loop.
Jeffrey Yasskin
2009/02/28 01:39:00
True, but the other similar assertions in other te
| |
30 self.assert_(elem in asm) | 30 self.assert_(elem in asm) |
31 | 31 |
32 def test_elim_inversion_of_is_or_in(self): | 32 def test_elim_inversion_of_is_or_in(self): |
33 for line, elem in ( | 33 for line, elem in ( |
34 ('not a is b', '(is not)',), | 34 ('not a is b', '(is not)',), |
35 ('not a in b', '(not in)',), | 35 ('not a in b', '(not in)',), |
36 ('not a is not b', '(is)',), | 36 ('not a is not b', '(is)',), |
37 ('not a not in b', '(in)',), | 37 ('not a not in b', '(in)',), |
38 ): | 38 ): |
39 asm = dis_single(line) | 39 asm = dis_single(line) |
40 self.assert_(elem in asm) | 40 self.assert_(elem in asm) |
41 | 41 |
42 def test_none_as_constant(self): | 42 def test_none_as_constant(self): |
43 # LOAD_GLOBAL None --> LOAD_CONST None | 43 # LOAD_GLOBAL None --> LOAD_CONST None |
44 def f(x): | 44 def f(x): |
45 None | 45 None |
46 return x | 46 return x |
47 asm = disassemble(f) | 47 asm = disassemble(f) |
48 for elem in ('LOAD_GLOBAL',): | 48 for elem in ('LOAD_GLOBAL',): |
49 self.assert_(elem not in asm) | 49 self.assert_(elem not in asm) |
50 for elem in ('LOAD_CONST', '(None)'): | 50 for elem in ('LOAD_CONST', '(None)'): |
51 self.assert_(elem in asm) | 51 self.assert_(elem in asm) |
52 def f(): | 52 def f(): |
53 'Adding a docstring made this test fail in Py2.5.0' | 53 'Adding a docstring made this test fail in Py2.5.0' |
54 return None | 54 return None |
55 self.assert_('LOAD_CONST' in disassemble(f)) | 55 self.assert_('LOAD_CONST' in disassemble(f)) |
56 self.assert_('LOAD_GLOBAL' not in disassemble(f)) | 56 self.assert_('LOAD_GLOBAL' not in disassemble(f)) |
57 | 57 |
58 def test_while_one(self): | 58 def test_while_one(self): |
59 # Skip over: LOAD_CONST trueconst JUMP_IF_FALSE xx POP_TOP | 59 # Skip over: LOAD_CONST trueconst POP_JUMP_IF_FALSE xx |
60 def f(): | 60 def f(): |
61 while 1: | 61 while 1: |
62 pass | 62 pass |
63 return list | 63 return list |
64 asm = disassemble(f) | 64 asm = disassemble(f) |
65 for elem in ('LOAD_CONST', 'JUMP_IF_FALSE'): | 65 for elem in ('LOAD_CONST', 'POP_JUMP_IF_FALSE'): |
66 self.assert_(elem not in asm) | 66 self.assert_(elem not in asm) |
67 for elem in ('JUMP_ABSOLUTE',): | 67 for elem in ('JUMP_ABSOLUTE',): |
68 self.assert_(elem in asm) | 68 self.assert_(elem in asm) |
69 | 69 |
70 def test_pack_unpack(self): | 70 def test_pack_unpack(self): |
71 for line, elem in ( | 71 for line, elem in ( |
72 ('a, = a,', 'LOAD_CONST',), | 72 ('a, = a,', 'LOAD_CONST',), |
73 ('a, b = a, b', 'ROT_TWO',), | 73 ('a, b = a, b', 'ROT_TWO',), |
74 ('a, b, c = a, b, c', 'ROT_THREE',), | 74 ('a, b, c = a, b, c', 'ROT_THREE',), |
75 ): | 75 ): |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
214 import gc | 214 import gc |
215 counts = [None] * 5 | 215 counts = [None] * 5 |
216 for i in xrange(len(counts)): | 216 for i in xrange(len(counts)): |
217 test_support.run_unittest(*test_classes) | 217 test_support.run_unittest(*test_classes) |
218 gc.collect() | 218 gc.collect() |
219 counts[i] = sys.gettotalrefcount() | 219 counts[i] = sys.gettotalrefcount() |
220 print counts | 220 print counts |
221 | 221 |
222 if __name__ == "__main__": | 222 if __name__ == "__main__": |
223 test_main(verbose=True) | 223 test_main(verbose=True) |
OLD | NEW |