~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to lib-python/2.4.1/test/test_dis.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from test.test_support import verify, verbose, TestFailed, run_unittest
 
2
import sys
 
3
import dis
 
4
import StringIO
 
5
 
 
6
# Minimal tests for dis module
 
7
 
 
8
import unittest
 
9
 
 
10
def _f(a):
 
11
    print a
 
12
    return 1
 
13
 
 
14
dis_f = """\
 
15
 %-4d         0 LOAD_FAST                0 (a)
 
16
              3 PRINT_ITEM
 
17
              4 PRINT_NEWLINE
 
18
 
 
19
 %-4d         5 LOAD_CONST               1 (1)
 
20
              8 RETURN_VALUE
 
21
"""%(_f.func_code.co_firstlineno + 1,
 
22
     _f.func_code.co_firstlineno + 2)
 
23
 
 
24
 
 
25
def bug708901():
 
26
    for res in range(1,
 
27
                     10):
 
28
        pass
 
29
 
 
30
dis_bug708901 = """\
 
31
 %-4d         0 SETUP_LOOP              23 (to 26)
 
32
              3 LOAD_GLOBAL              0 (range)
 
33
              6 LOAD_CONST               1 (1)
 
34
 
 
35
 %-4d         9 LOAD_CONST               2 (10)
 
36
             12 CALL_FUNCTION            2
 
37
             15 GET_ITER
 
38
        >>   16 FOR_ITER                 6 (to 25)
 
39
             19 STORE_FAST               0 (res)
 
40
 
 
41
 %-4d        22 JUMP_ABSOLUTE           16
 
42
        >>   25 POP_BLOCK
 
43
        >>   26 LOAD_CONST               0 (None)
 
44
             29 RETURN_VALUE
 
45
"""%(bug708901.func_code.co_firstlineno + 1,
 
46
     bug708901.func_code.co_firstlineno + 2,
 
47
     bug708901.func_code.co_firstlineno + 3)
 
48
 
 
49
class DisTests(unittest.TestCase):
 
50
    def do_disassembly_test(self, func, expected):
 
51
        s = StringIO.StringIO()
 
52
        save_stdout = sys.stdout
 
53
        sys.stdout = s
 
54
        dis.dis(func)
 
55
        sys.stdout = save_stdout
 
56
        got = s.getvalue()
 
57
        # Trim trailing blanks (if any).
 
58
        lines = got.split('\n')
 
59
        lines = [line.rstrip() for line in lines]
 
60
        expected = expected.split("\n")
 
61
        import difflib
 
62
        if expected != lines:
 
63
            self.fail(
 
64
                "events did not match expectation:\n" +
 
65
                "\n".join(difflib.ndiff(expected,
 
66
                                        lines)))
 
67
 
 
68
    def test_opmap(self):
 
69
        self.assertEqual(dis.opmap["STOP_CODE"], 0)
 
70
        self.assertEqual(dis.opmap["LOAD_CONST"] in dis.hasconst, True)
 
71
        self.assertEqual(dis.opmap["STORE_NAME"] in dis.hasname, True)
 
72
 
 
73
    def test_opname(self):
 
74
        self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
 
75
 
 
76
    def test_boundaries(self):
 
77
        self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG)
 
78
        self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT)
 
79
 
 
80
    def test_dis(self):
 
81
        self.do_disassembly_test(_f, dis_f)
 
82
 
 
83
    def test_bug_708901(self):
 
84
        self.do_disassembly_test(bug708901, dis_bug708901)
 
85
 
 
86
def test_main():
 
87
    run_unittest(DisTests)
 
88
 
 
89
 
 
90
if __name__ == "__main__":
 
91
    test_main()