summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_inspect.py
blob: 130fa8ef7350b5c243e2a34e0d66d5ddb87b1c09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
source = '''# line 1
'A module docstring.'

import sys, inspect
# line 5

# line 7
def spam(a, b, c, d=3, (e, (f,))=(4, (5,)), *g, **h):
    eggs(b + d, c + f)

# line 11
def eggs(x, y):
    "A docstring."
    global fr, st
    fr = inspect.currentframe()
    st = inspect.stack()
    p = x
    q = y / 0

# line 20
class StupidGit:
    """A longer,

    indented

    docstring."""
# line 27

    def abuse(self, a, b, c):
        """Another

\tdocstring

        containing

\ttabs
\t
        """
        self.argue(a, b, c)
# line 40
    def argue(self, a, b, c):
        try:
            spam(a, b, c)
        except:
            self.ex = sys.exc_info()
            self.tr = inspect.trace()

# line 48
class MalodorousPervert(StupidGit):
    pass

class ParrotDroppings:
    pass

class FesteringGob(MalodorousPervert, ParrotDroppings):
    pass
'''

# Functions tested in this suite:
# ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode,
# isbuiltin, isroutine, getmembers, getdoc, getfile, getmodule,
# getsourcefile, getcomments, getsource, getclasstree, getargspec,
# getargvalues, formatargspec, formatargvalues, currentframe, stack, trace

from test_support import TestFailed, TESTFN
import sys, imp, os, string

def test(assertion, message, *args):
    if not assertion:
        raise TestFailed, message % args

import inspect

file = open(TESTFN, 'w')
file.write(source)
file.close()

# Note that load_source creates file TESTFN+'c' or TESTFN+'o'.
mod = imp.load_source('testmod', TESTFN)
files_to_clean_up = [TESTFN, TESTFN + 'c', TESTFN + 'o']

def istest(func, exp):
    obj = eval(exp)
    test(func(obj), '%s(%s)' % (func.__name__, exp))
    for other in [inspect.isbuiltin, inspect.isclass, inspect.iscode,
                  inspect.isframe, inspect.isfunction, inspect.ismethod,
                  inspect.ismodule, inspect.istraceback]:
        if other is not func:
            test(not other(obj), 'not %s(%s)' % (other.__name__, exp))

git = mod.StupidGit()
try:
    1/0
except:
    tb = sys.exc_traceback

istest(inspect.isbuiltin, 'sys.exit')
istest(inspect.isbuiltin, '[].append')
istest(inspect.isclass, 'mod.StupidGit')
istest(inspect.iscode, 'mod.spam.func_code')
istest(inspect.isframe, 'tb.tb_frame')
istest(inspect.isfunction, 'mod.spam')
istest(inspect.ismethod, 'mod.StupidGit.abuse')
istest(inspect.ismethod, 'git.argue')
istest(inspect.ismodule, 'mod')
istest(inspect.istraceback, 'tb')
test(inspect.isroutine(mod.spam), 'isroutine(mod.spam)')
test(inspect.isroutine([].count), 'isroutine([].count)')

classes = inspect.getmembers(mod, inspect.isclass)
test(classes ==
     [('FesteringGob', mod.FesteringGob),
      ('MalodorousPervert', mod.MalodorousPervert),
      ('ParrotDroppings', mod.ParrotDroppings),
      ('StupidGit', mod.StupidGit)], 'class list')
tree = inspect.getclasstree(map(lambda x: x[1], classes), 1)
test(tree ==
     [(mod.ParrotDroppings, ()),
      (mod.StupidGit, ()),
      [(mod.MalodorousPervert, (mod.StupidGit,)),
       [(mod.FesteringGob, (mod.MalodorousPervert, mod.ParrotDroppings))
       ]
      ]
     ], 'class tree')

functions = inspect.getmembers(mod, inspect.isfunction)
test(functions == [('eggs', mod.eggs), ('spam', mod.spam)], 'function list')

test(inspect.getdoc(mod) == 'A module docstring.', 'getdoc(mod)')
test(inspect.getcomments(mod) == '# line 1\n', 'getcomments(mod)')
test(inspect.getmodule(mod.StupidGit) == mod, 'getmodule(mod.StupidGit)')
test(inspect.getfile(mod.StupidGit) == TESTFN, 'getfile(mod.StupidGit)')
test(inspect.getsourcefile(mod.spam) == TESTFN, 'getsourcefile(mod.spam)')
test(inspect.getsourcefile(git.abuse) == TESTFN, 'getsourcefile(git.abuse)')

def sourcerange(top, bottom):
    lines = string.split(source, '\n')
    return string.join(lines[top-1:bottom], '\n') + '\n'

test(inspect.getsource(git.abuse) == sourcerange(29, 39),
     'getsource(git.abuse)')
test(inspect.getsource(mod.StupidGit) == sourcerange(21, 46),
     'getsource(mod.StupidGit)')
test(inspect.getdoc(mod.StupidGit) ==
     'A longer,\n\nindented\n\ndocstring.', 'getdoc(mod.StupidGit)')
test(inspect.getdoc(git.abuse) ==
     'Another\n\ndocstring\n\ncontaining\n\ntabs\n\n', 'getdoc(git.abuse)')
test(inspect.getcomments(mod.StupidGit) == '# line 20\n',
     'getcomments(mod.StupidGit)')

args, varargs, varkw, defaults = inspect.getargspec(mod.eggs)
test(args == ['x', 'y'], 'mod.eggs args')
test(varargs == None, 'mod.eggs varargs')
test(varkw == None, 'mod.eggs varkw')
test(defaults == None, 'mod.eggs defaults')
test(inspect.formatargspec(args, varargs, varkw, defaults) ==
     '(x, y)', 'mod.eggs formatted argspec')
args, varargs, varkw, defaults = inspect.getargspec(mod.spam)
test(args == ['a', 'b', 'c', 'd', ['e', ['f']]], 'mod.spam args')
test(varargs == 'g', 'mod.spam varargs')
test(varkw == 'h', 'mod.spam varkw')
test(defaults == (3, (4, (5,))), 'mod.spam defaults')
test(inspect.formatargspec(args, varargs, varkw, defaults) ==
     '(a, b, c, d=3, (e, (f,))=(4, (5,)), *g, **h)',
     'mod.spam formatted argspec')

git.abuse(7, 8, 9)

istest(inspect.istraceback, 'git.ex[2]')
istest(inspect.isframe, 'mod.fr')

test(len(git.tr) == 3, 'trace() length')
test(git.tr[0][1:] == (TESTFN, 46, 'argue',
                       ['            self.tr = inspect.trace()\n'], 0),
     'trace() row 2')
test(git.tr[1][1:] == (TESTFN, 9, 'spam', ['    eggs(b + d, c + f)\n'], 0),
     'trace() row 2')
test(git.tr[2][1:] == (TESTFN, 18, 'eggs', ['    q = y / 0\n'], 0),
     'trace() row 3')

test(len(mod.st) >= 5, 'stack() length')
test(mod.st[0][1:] ==
     (TESTFN, 16, 'eggs', ['    st = inspect.stack()\n'], 0),
     'stack() row 1')
test(mod.st[1][1:] ==
     (TESTFN, 9, 'spam', ['    eggs(b + d, c + f)\n'], 0),
     'stack() row 2')
test(mod.st[2][1:] ==
     (TESTFN, 43, 'argue', ['            spam(a, b, c)\n'], 0),
     'stack() row 3')
test(mod.st[3][1:] ==
     (TESTFN, 39, 'abuse', ['        self.argue(a, b, c)\n'], 0),
     'stack() row 4')

args, varargs, varkw, locals = inspect.getargvalues(mod.fr)
test(args == ['x', 'y'], 'mod.fr args')
test(varargs == None, 'mod.fr varargs')
test(varkw == None, 'mod.fr varkw')
test(locals == {'x': 11, 'p': 11, 'y': 14}, 'mod.fr locals')
test(inspect.formatargvalues(args, varargs, varkw, locals) ==
     '(x=11, y=14)', 'mod.fr formatted argvalues')

args, varargs, varkw, locals = inspect.getargvalues(mod.fr.f_back)
test(args == ['a', 'b', 'c', 'd', ['e', ['f']]], 'mod.fr.f_back args')
test(varargs == 'g', 'mod.fr.f_back varargs')
test(varkw == 'h', 'mod.fr.f_back varkw')
test(inspect.formatargvalues(args, varargs, varkw, locals) ==
     '(a=7, b=8, c=9, d=3, (e=4, (f=5,)), *g=(), **h={})',
     'mod.fr.f_back formatted argvalues')

for fname in files_to_clean_up:
    try:
        os.unlink(fname)
    except:
        pass