Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1 | source = '''# line 1 |
| 2 | 'A module docstring.' |
| 3 | |
| 4 | import sys, inspect |
| 5 | # line 5 |
| 6 | |
| 7 | # line 7 |
| 8 | def spam(a, b, c, d=3, (e, (f,))=(4, (5,)), *g, **h): |
| 9 | eggs(b + d, c + f) |
| 10 | |
| 11 | # line 11 |
| 12 | def eggs(x, y): |
| 13 | "A docstring." |
| 14 | global fr, st |
| 15 | fr = inspect.currentframe() |
| 16 | st = inspect.stack() |
| 17 | p = x |
| 18 | q = y / 0 |
| 19 | |
| 20 | # line 20 |
| 21 | class StupidGit: |
| 22 | """A longer, |
| 23 | |
| 24 | indented |
| 25 | |
| 26 | docstring.""" |
| 27 | # line 27 |
| 28 | |
| 29 | def abuse(self, a, b, c): |
| 30 | """Another |
| 31 | |
| 32 | \tdocstring |
| 33 | |
| 34 | containing |
| 35 | |
| 36 | \ttabs |
| 37 | \t |
| 38 | """ |
| 39 | self.argue(a, b, c) |
| 40 | # line 40 |
| 41 | def argue(self, a, b, c): |
| 42 | try: |
| 43 | spam(a, b, c) |
| 44 | except: |
| 45 | self.ex = sys.exc_info() |
| 46 | self.tr = inspect.trace() |
| 47 | |
| 48 | # line 48 |
| 49 | class MalodorousPervert(StupidGit): |
| 50 | pass |
| 51 | |
| 52 | class ParrotDroppings: |
| 53 | pass |
| 54 | |
| 55 | class FesteringGob(MalodorousPervert, ParrotDroppings): |
| 56 | pass |
| 57 | ''' |
| 58 | |
| 59 | # Functions tested in this suite: |
| 60 | # ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode, |
| 61 | # isbuiltin, isroutine, getmembers, getdoc, getfile, getmodule, |
| 62 | # getsourcefile, getcomments, getsource, getclasstree, getargspec, |
| 63 | # getargvalues, formatargspec, formatargvalues, currentframe, stack, trace |
| 64 | |
| 65 | from test_support import TestFailed, TESTFN |
| 66 | import sys, imp, os, string |
| 67 | |
| 68 | def test(assertion, message, *args): |
| 69 | if not assertion: |
| 70 | raise TestFailed, message % args |
| 71 | |
| 72 | import inspect |
| 73 | |
| 74 | file = open(TESTFN, 'w') |
| 75 | file.write(source) |
| 76 | file.close() |
| 77 | |
| 78 | mod = imp.load_source('testmod', TESTFN) |
| 79 | |
| 80 | def istest(func, exp): |
| 81 | obj = eval(exp) |
| 82 | test(func(obj), '%s(%s)' % (func.__name__, exp)) |
| 83 | for other in [inspect.isbuiltin, inspect.isclass, inspect.iscode, |
| 84 | inspect.isframe, inspect.isfunction, inspect.ismethod, |
| 85 | inspect.ismodule, inspect.istraceback]: |
| 86 | if other is not func: |
| 87 | test(not other(obj), 'not %s(%s)' % (other.__name__, exp)) |
| 88 | |
| 89 | git = mod.StupidGit() |
| 90 | try: |
| 91 | 1/0 |
| 92 | except: |
| 93 | tb = sys.exc_traceback |
| 94 | |
| 95 | istest(inspect.isbuiltin, 'sys.exit') |
| 96 | istest(inspect.isbuiltin, '[].append') |
| 97 | istest(inspect.isclass, 'mod.StupidGit') |
| 98 | istest(inspect.iscode, 'mod.spam.func_code') |
| 99 | istest(inspect.isframe, 'tb.tb_frame') |
| 100 | istest(inspect.isfunction, 'mod.spam') |
| 101 | istest(inspect.ismethod, 'mod.StupidGit.abuse') |
| 102 | istest(inspect.ismethod, 'git.argue') |
| 103 | istest(inspect.ismodule, 'mod') |
| 104 | istest(inspect.istraceback, 'tb') |
| 105 | test(inspect.isroutine(mod.spam), 'isroutine(mod.spam)') |
| 106 | test(inspect.isroutine([].count), 'isroutine([].count)') |
| 107 | |
| 108 | classes = inspect.getmembers(mod, inspect.isclass) |
| 109 | test(classes == |
| 110 | [('FesteringGob', mod.FesteringGob), |
| 111 | ('MalodorousPervert', mod.MalodorousPervert), |
| 112 | ('ParrotDroppings', mod.ParrotDroppings), |
| 113 | ('StupidGit', mod.StupidGit)], 'class list') |
| 114 | tree = inspect.getclasstree(map(lambda x: x[1], classes), 1) |
| 115 | test(tree == |
| 116 | [(mod.ParrotDroppings, ()), |
| 117 | (mod.StupidGit, ()), |
| 118 | [(mod.MalodorousPervert, (mod.StupidGit,)), |
| 119 | [(mod.FesteringGob, (mod.MalodorousPervert, mod.ParrotDroppings)) |
| 120 | ] |
| 121 | ] |
| 122 | ], 'class tree') |
| 123 | |
| 124 | functions = inspect.getmembers(mod, inspect.isfunction) |
| 125 | test(functions == [('eggs', mod.eggs), ('spam', mod.spam)], 'function list') |
| 126 | |
| 127 | test(inspect.getdoc(mod) == 'A module docstring.', 'getdoc(mod)') |
| 128 | test(inspect.getcomments(mod) == '# line 1\n', 'getcomments(mod)') |
| 129 | test(inspect.getmodule(mod.StupidGit) == mod, 'getmodule(mod.StupidGit)') |
| 130 | test(inspect.getfile(mod.StupidGit) == TESTFN, 'getfile(mod.StupidGit)') |
| 131 | test(inspect.getsourcefile(mod.spam) == TESTFN, 'getsourcefile(mod.spam)') |
| 132 | test(inspect.getsourcefile(git.abuse) == TESTFN, 'getsourcefile(git.abuse)') |
| 133 | |
| 134 | def sourcerange(top, bottom): |
| 135 | lines = string.split(source, '\n') |
| 136 | return string.join(lines[top-1:bottom], '\n') + '\n' |
| 137 | |
| 138 | test(inspect.getsource(git.abuse) == sourcerange(29, 39), |
| 139 | 'getsource(git.abuse)') |
| 140 | test(inspect.getsource(mod.StupidGit) == sourcerange(21, 46), |
| 141 | 'getsource(mod.StupidGit)') |
| 142 | test(inspect.getdoc(mod.StupidGit) == |
| 143 | 'A longer,\n\nindented\n\ndocstring.', 'getdoc(mod.StupidGit)') |
| 144 | test(inspect.getdoc(git.abuse) == |
| 145 | 'Another\n\ndocstring\n\ncontaining\n\ntabs\n\n', 'getdoc(git.abuse)') |
| 146 | test(inspect.getcomments(mod.StupidGit) == '# line 20\n', |
| 147 | 'getcomments(mod.StupidGit)') |
| 148 | |
| 149 | args, varargs, varkw, defaults = inspect.getargspec(mod.eggs) |
| 150 | test(args == ['x', 'y'], 'mod.eggs args') |
| 151 | test(varargs == None, 'mod.eggs varargs') |
| 152 | test(varkw == None, 'mod.eggs varkw') |
| 153 | test(defaults == None, 'mod.eggs defaults') |
| 154 | test(inspect.formatargspec(args, varargs, varkw, defaults) == |
| 155 | '(x, y)', 'mod.eggs formatted argspec') |
| 156 | args, varargs, varkw, defaults = inspect.getargspec(mod.spam) |
| 157 | test(args == ['a', 'b', 'c', 'd', ['e', ['f']]], 'mod.spam args') |
| 158 | test(varargs == 'g', 'mod.spam varargs') |
| 159 | test(varkw == 'h', 'mod.spam varkw') |
| 160 | test(defaults == (3, (4, (5,))), 'mod.spam defaults') |
| 161 | test(inspect.formatargspec(args, varargs, varkw, defaults) == |
| 162 | '(a, b, c, d=3, (e, (f,))=(4, (5,)), *g, **h)', |
| 163 | 'mod.spam formatted argspec') |
| 164 | |
| 165 | git.abuse(7, 8, 9) |
| 166 | |
| 167 | istest(inspect.istraceback, 'git.ex[2]') |
| 168 | istest(inspect.isframe, 'mod.fr') |
| 169 | |
| 170 | test(len(git.tr) == 2, 'trace() length') |
Ka-Ping Yee | 9054344 | 2001-03-02 05:48:10 +0000 | [diff] [blame] | 171 | test(git.tr[0][1:] == (TESTFN, 9, 'spam', [' eggs(b + d, c + f)\n'], 0), |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 172 | 'trace() row 1') |
Ka-Ping Yee | 9054344 | 2001-03-02 05:48:10 +0000 | [diff] [blame] | 173 | test(git.tr[1][1:] == (TESTFN, 18, 'eggs', [' q = y / 0\n'], 0), |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 174 | 'trace() row 2') |
| 175 | |
| 176 | test(len(mod.st) >= 5, 'stack() length') |
| 177 | test(mod.st[0][1:] == |
Ka-Ping Yee | 9054344 | 2001-03-02 05:48:10 +0000 | [diff] [blame] | 178 | (TESTFN, 16, 'eggs', [' st = inspect.stack()\n'], 0), |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 179 | 'stack() row 1') |
| 180 | test(mod.st[1][1:] == |
Ka-Ping Yee | 9054344 | 2001-03-02 05:48:10 +0000 | [diff] [blame] | 181 | (TESTFN, 9, 'spam', [' eggs(b + d, c + f)\n'], 0), |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 182 | 'stack() row 2') |
| 183 | test(mod.st[2][1:] == |
Ka-Ping Yee | 9054344 | 2001-03-02 05:48:10 +0000 | [diff] [blame] | 184 | (TESTFN, 43, 'argue', [' spam(a, b, c)\n'], 0), |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 185 | 'stack() row 3') |
| 186 | test(mod.st[3][1:] == |
Ka-Ping Yee | 9054344 | 2001-03-02 05:48:10 +0000 | [diff] [blame] | 187 | (TESTFN, 39, 'abuse', [' self.argue(a, b, c)\n'], 0), |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 188 | 'stack() row 4') |
| 189 | # row 4 is in test_inspect.py |
| 190 | |
| 191 | args, varargs, varkw, locals = inspect.getargvalues(mod.fr) |
| 192 | test(args == ['x', 'y'], 'mod.fr args') |
| 193 | test(varargs == None, 'mod.fr varargs') |
| 194 | test(varkw == None, 'mod.fr varkw') |
| 195 | test(locals == {'x': 11, 'p': 11, 'y': 14}, 'mod.fr locals') |
| 196 | test(inspect.formatargvalues(args, varargs, varkw, locals) == |
| 197 | '(x=11, y=14)', 'mod.fr formatted argvalues') |
| 198 | |
| 199 | args, varargs, varkw, locals = inspect.getargvalues(mod.fr.f_back) |
| 200 | test(args == ['a', 'b', 'c', 'd', ['e', ['f']]], 'mod.fr.f_back args') |
| 201 | test(varargs == 'g', 'mod.fr.f_back varargs') |
| 202 | test(varkw == 'h', 'mod.fr.f_back varkw') |
| 203 | test(inspect.formatargvalues(args, varargs, varkw, locals) == |
| 204 | '(a=7, b=8, c=9, d=3, (e=4, (f=5,)), *g=(), **h={})', |
| 205 | 'mod.fr.f_back formatted argvalues') |
| 206 | |
| 207 | os.unlink(TESTFN) |