Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 1 | import sys |
Barry Warsaw | 00decd7 | 2006-07-27 23:43:15 +0000 | [diff] [blame] | 2 | import types |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 3 | import unittest |
| 4 | import inspect |
Barry Warsaw | 00decd7 | 2006-07-27 23:43:15 +0000 | [diff] [blame] | 5 | import datetime |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 6 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 7 | from test.test_support import TESTFN, run_unittest |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 8 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 9 | from test import inspect_fodder as mod |
| 10 | from test import inspect_fodder2 as mod2 |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 11 | |
| 12 | # Functions tested in this suite: |
| 13 | # ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode, |
| 14 | # isbuiltin, isroutine, getmembers, getdoc, getfile, getmodule, |
| 15 | # getsourcefile, getcomments, getsource, getclasstree, getargspec, |
| 16 | # getargvalues, formatargspec, formatargvalues, currentframe, stack, trace |
Martin v. Löwis | e59e2ba | 2003-05-03 09:09:02 +0000 | [diff] [blame] | 17 | # isdatadescriptor |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 18 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 19 | modfile = mod.__file__ |
Georg Brandl | b2afe85 | 2006-06-09 20:43:48 +0000 | [diff] [blame] | 20 | if modfile.endswith(('c', 'o')): |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 21 | modfile = modfile[:-1] |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 22 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 23 | import __builtin__ |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 24 | |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 25 | try: |
| 26 | 1/0 |
| 27 | except: |
| 28 | tb = sys.exc_traceback |
| 29 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 30 | git = mod.StupidGit() |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 31 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 32 | class IsTestBase(unittest.TestCase): |
| 33 | predicates = set([inspect.isbuiltin, inspect.isclass, inspect.iscode, |
| 34 | inspect.isframe, inspect.isfunction, inspect.ismethod, |
| 35 | inspect.ismodule, inspect.istraceback]) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 36 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 37 | def istest(self, predicate, exp): |
| 38 | obj = eval(exp) |
| 39 | self.failUnless(predicate(obj), '%s(%s)' % (predicate.__name__, exp)) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 40 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 41 | for other in self.predicates - set([predicate]): |
| 42 | self.failIf(other(obj), 'not %s(%s)' % (other.__name__, exp)) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 43 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 44 | class TestPredicates(IsTestBase): |
Barry Warsaw | 00decd7 | 2006-07-27 23:43:15 +0000 | [diff] [blame] | 45 | def test_thirteen(self): |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 46 | count = len(filter(lambda x:x.startswith('is'), dir(inspect))) |
Neal Norwitz | df80af7 | 2006-07-28 04:22:34 +0000 | [diff] [blame] | 47 | # Doc/lib/libinspect.tex claims there are 13 such functions |
| 48 | expected = 13 |
| 49 | err_msg = "There are %d (not %d) is* functions" % (count, expected) |
| 50 | self.assertEqual(count, expected, err_msg) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 51 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 52 | def test_excluding_predicates(self): |
| 53 | self.istest(inspect.isbuiltin, 'sys.exit') |
| 54 | self.istest(inspect.isbuiltin, '[].append') |
| 55 | self.istest(inspect.isclass, 'mod.StupidGit') |
| 56 | self.istest(inspect.iscode, 'mod.spam.func_code') |
| 57 | self.istest(inspect.isframe, 'tb.tb_frame') |
| 58 | self.istest(inspect.isfunction, 'mod.spam') |
| 59 | self.istest(inspect.ismethod, 'mod.StupidGit.abuse') |
| 60 | self.istest(inspect.ismethod, 'git.argue') |
| 61 | self.istest(inspect.ismodule, 'mod') |
| 62 | self.istest(inspect.istraceback, 'tb') |
| 63 | self.istest(inspect.isdatadescriptor, '__builtin__.file.closed') |
| 64 | self.istest(inspect.isdatadescriptor, '__builtin__.file.softspace') |
Barry Warsaw | 00decd7 | 2006-07-27 23:43:15 +0000 | [diff] [blame] | 65 | if hasattr(types, 'GetSetDescriptorType'): |
| 66 | self.istest(inspect.isgetsetdescriptor, |
| 67 | 'type(tb.tb_frame).f_locals') |
| 68 | else: |
| 69 | self.failIf(inspect.isgetsetdescriptor(type(tb.tb_frame).f_locals)) |
| 70 | if hasattr(types, 'MemberDescriptorType'): |
| 71 | self.istest(inspect.ismemberdescriptor, 'datetime.timedelta.days') |
| 72 | else: |
| 73 | self.failIf(inspect.ismemberdescriptor(datetime.timedelta.days)) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 74 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 75 | def test_isroutine(self): |
| 76 | self.assert_(inspect.isroutine(mod.spam)) |
| 77 | self.assert_(inspect.isroutine([].count)) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 78 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 79 | class TestInterpreterStack(IsTestBase): |
| 80 | def __init__(self, *args, **kwargs): |
| 81 | unittest.TestCase.__init__(self, *args, **kwargs) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 82 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 83 | git.abuse(7, 8, 9) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 84 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 85 | def test_abuse_done(self): |
| 86 | self.istest(inspect.istraceback, 'git.ex[2]') |
| 87 | self.istest(inspect.isframe, 'mod.fr') |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 88 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 89 | def test_stack(self): |
| 90 | self.assert_(len(mod.st) >= 5) |
| 91 | self.assertEqual(mod.st[0][1:], |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 92 | (modfile, 16, 'eggs', [' st = inspect.stack()\n'], 0)) |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 93 | self.assertEqual(mod.st[1][1:], |
| 94 | (modfile, 9, 'spam', [' eggs(b + d, c + f)\n'], 0)) |
| 95 | self.assertEqual(mod.st[2][1:], |
| 96 | (modfile, 43, 'argue', [' spam(a, b, c)\n'], 0)) |
| 97 | self.assertEqual(mod.st[3][1:], |
| 98 | (modfile, 39, 'abuse', [' self.argue(a, b, c)\n'], 0)) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 99 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 100 | def test_trace(self): |
| 101 | self.assertEqual(len(git.tr), 3) |
| 102 | self.assertEqual(git.tr[0][1:], (modfile, 43, 'argue', |
| 103 | [' spam(a, b, c)\n'], 0)) |
| 104 | self.assertEqual(git.tr[1][1:], (modfile, 9, 'spam', |
| 105 | [' eggs(b + d, c + f)\n'], 0)) |
| 106 | self.assertEqual(git.tr[2][1:], (modfile, 18, 'eggs', |
| 107 | [' q = y / 0\n'], 0)) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 108 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 109 | def test_frame(self): |
| 110 | args, varargs, varkw, locals = inspect.getargvalues(mod.fr) |
| 111 | self.assertEqual(args, ['x', 'y']) |
| 112 | self.assertEqual(varargs, None) |
| 113 | self.assertEqual(varkw, None) |
| 114 | self.assertEqual(locals, {'x': 11, 'p': 11, 'y': 14}) |
| 115 | self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals), |
| 116 | '(x=11, y=14)') |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 117 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 118 | def test_previous_frame(self): |
| 119 | args, varargs, varkw, locals = inspect.getargvalues(mod.fr.f_back) |
| 120 | self.assertEqual(args, ['a', 'b', 'c', 'd', ['e', ['f']]]) |
| 121 | self.assertEqual(varargs, 'g') |
| 122 | self.assertEqual(varkw, 'h') |
| 123 | self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals), |
| 124 | '(a=7, b=8, c=9, d=3, (e=4, (f=5,)), *g=(), **h={})') |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 125 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 126 | class GetSourceBase(unittest.TestCase): |
| 127 | # Subclasses must override. |
| 128 | fodderFile = None |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 129 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 130 | def __init__(self, *args, **kwargs): |
| 131 | unittest.TestCase.__init__(self, *args, **kwargs) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 132 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 133 | self.source = file(inspect.getsourcefile(self.fodderFile)).read() |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 134 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 135 | def sourcerange(self, top, bottom): |
| 136 | lines = self.source.split("\n") |
| 137 | return "\n".join(lines[top-1:bottom]) + "\n" |
Tim Peters | e0b2d7a | 2001-09-22 06:10:55 +0000 | [diff] [blame] | 138 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 139 | def assertSourceEqual(self, obj, top, bottom): |
| 140 | self.assertEqual(inspect.getsource(obj), |
| 141 | self.sourcerange(top, bottom)) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 142 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 143 | class TestRetrievingSourceCode(GetSourceBase): |
| 144 | fodderFile = mod |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 145 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 146 | def test_getclasses(self): |
| 147 | classes = inspect.getmembers(mod, inspect.isclass) |
| 148 | self.assertEqual(classes, |
| 149 | [('FesteringGob', mod.FesteringGob), |
| 150 | ('MalodorousPervert', mod.MalodorousPervert), |
| 151 | ('ParrotDroppings', mod.ParrotDroppings), |
| 152 | ('StupidGit', mod.StupidGit)]) |
| 153 | tree = inspect.getclasstree([cls[1] for cls in classes], 1) |
| 154 | self.assertEqual(tree, |
| 155 | [(mod.ParrotDroppings, ()), |
| 156 | (mod.StupidGit, ()), |
| 157 | [(mod.MalodorousPervert, (mod.StupidGit,)), |
| 158 | [(mod.FesteringGob, (mod.MalodorousPervert, |
| 159 | mod.ParrotDroppings)) |
| 160 | ] |
| 161 | ] |
| 162 | ]) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 163 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 164 | def test_getfunctions(self): |
| 165 | functions = inspect.getmembers(mod, inspect.isfunction) |
| 166 | self.assertEqual(functions, [('eggs', mod.eggs), |
| 167 | ('spam', mod.spam)]) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 168 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 169 | def test_getdoc(self): |
| 170 | self.assertEqual(inspect.getdoc(mod), 'A module docstring.') |
| 171 | self.assertEqual(inspect.getdoc(mod.StupidGit), |
| 172 | 'A longer,\n\nindented\n\ndocstring.') |
| 173 | self.assertEqual(inspect.getdoc(git.abuse), |
| 174 | 'Another\n\ndocstring\n\ncontaining\n\ntabs') |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 175 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 176 | def test_getcomments(self): |
| 177 | self.assertEqual(inspect.getcomments(mod), '# line 1\n') |
| 178 | self.assertEqual(inspect.getcomments(mod.StupidGit), '# line 20\n') |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 179 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 180 | def test_getmodule(self): |
| 181 | self.assertEqual(inspect.getmodule(mod.StupidGit), mod) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 182 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 183 | def test_getsource(self): |
| 184 | self.assertSourceEqual(git.abuse, 29, 39) |
| 185 | self.assertSourceEqual(mod.StupidGit, 21, 46) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 186 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 187 | def test_getsourcefile(self): |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 188 | self.assertEqual(inspect.getsourcefile(mod.spam), modfile) |
| 189 | self.assertEqual(inspect.getsourcefile(git.abuse), modfile) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 190 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 191 | def test_getfile(self): |
| 192 | self.assertEqual(inspect.getfile(mod.StupidGit), mod.__file__) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 193 | |
Phillip J. Eby | 5d86bdb | 2006-07-10 19:03:29 +0000 | [diff] [blame] | 194 | def test_getmodule_recursion(self): |
| 195 | from new import module |
| 196 | name = '__inspect_dummy' |
| 197 | m = sys.modules[name] = module(name) |
Tim Peters | 722b883 | 2006-07-10 21:11:49 +0000 | [diff] [blame] | 198 | m.__file__ = "<string>" # hopefully not a real filename... |
Phillip J. Eby | 5d86bdb | 2006-07-10 19:03:29 +0000 | [diff] [blame] | 199 | m.__loader__ = "dummy" # pretend the filename is understood by a loader |
| 200 | exec "def x(): pass" in m.__dict__ |
| 201 | self.assertEqual(inspect.getsourcefile(m.x.func_code), '<string>') |
| 202 | del sys.modules[name] |
Phillip J. Eby | 1a2959c | 2006-07-20 15:54:16 +0000 | [diff] [blame] | 203 | inspect.getmodule(compile('a=10','','single')) |
Phillip J. Eby | 5d86bdb | 2006-07-10 19:03:29 +0000 | [diff] [blame] | 204 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 205 | class TestDecorators(GetSourceBase): |
| 206 | fodderFile = mod2 |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 207 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 208 | def test_wrapped_decorator(self): |
| 209 | self.assertSourceEqual(mod2.wrapped, 14, 17) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 210 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 211 | def test_replacing_decorator(self): |
| 212 | self.assertSourceEqual(mod2.gone, 9, 10) |
Tim Peters | e0b2d7a | 2001-09-22 06:10:55 +0000 | [diff] [blame] | 213 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 214 | class TestOneliners(GetSourceBase): |
| 215 | fodderFile = mod2 |
| 216 | def test_oneline_lambda(self): |
| 217 | # Test inspect.getsource with a one-line lambda function. |
| 218 | self.assertSourceEqual(mod2.oll, 25, 25) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 219 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 220 | def test_threeline_lambda(self): |
| 221 | # Test inspect.getsource with a three-line lambda function, |
| 222 | # where the second and third lines are _not_ indented. |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 223 | self.assertSourceEqual(mod2.tll, 28, 30) |
| 224 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 225 | def test_twoline_indented_lambda(self): |
| 226 | # Test inspect.getsource with a two-line lambda function, |
| 227 | # where the second line _is_ indented. |
| 228 | self.assertSourceEqual(mod2.tlli, 33, 34) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 229 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 230 | def test_onelinefunc(self): |
| 231 | # Test inspect.getsource with a regular one-line function. |
| 232 | self.assertSourceEqual(mod2.onelinefunc, 37, 37) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 233 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 234 | def test_manyargs(self): |
| 235 | # Test inspect.getsource with a regular function where |
| 236 | # the arguments are on two lines and _not_ indented and |
| 237 | # the body on the second line with the last arguments. |
| 238 | self.assertSourceEqual(mod2.manyargs, 40, 41) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 239 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 240 | def test_twolinefunc(self): |
| 241 | # Test inspect.getsource with a regular function where |
| 242 | # the body is on two lines, following the argument list and |
| 243 | # continued on the next line by a \\. |
| 244 | self.assertSourceEqual(mod2.twolinefunc, 44, 45) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 245 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 246 | def test_lambda_in_list(self): |
| 247 | # Test inspect.getsource with a one-line lambda function |
| 248 | # defined in a list, indented. |
| 249 | self.assertSourceEqual(mod2.a[1], 49, 49) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 250 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 251 | def test_anonymous(self): |
| 252 | # Test inspect.getsource with a lambda function defined |
| 253 | # as argument to another function. |
| 254 | self.assertSourceEqual(mod2.anonymous, 55, 55) |
| 255 | |
Johannes Gijsbers | a5855d5 | 2005-03-12 16:37:11 +0000 | [diff] [blame] | 256 | class TestBuggyCases(GetSourceBase): |
| 257 | fodderFile = mod2 |
| 258 | |
| 259 | def test_with_comment(self): |
| 260 | self.assertSourceEqual(mod2.with_comment, 58, 59) |
| 261 | |
| 262 | def test_multiline_sig(self): |
| 263 | self.assertSourceEqual(mod2.multiline_sig[0], 63, 64) |
| 264 | |
Armin Rigo | dd5c023 | 2005-09-25 11:45:45 +0000 | [diff] [blame] | 265 | def test_nested_class(self): |
| 266 | self.assertSourceEqual(mod2.func69().func71, 71, 72) |
| 267 | |
| 268 | def test_one_liner_followed_by_non_name(self): |
| 269 | self.assertSourceEqual(mod2.func77, 77, 77) |
| 270 | |
| 271 | def test_one_liner_dedent_non_name(self): |
| 272 | self.assertSourceEqual(mod2.cls82.func83, 83, 83) |
| 273 | |
| 274 | def test_with_comment_instead_of_docstring(self): |
| 275 | self.assertSourceEqual(mod2.func88, 88, 90) |
| 276 | |
Georg Brandl | 2463f8f | 2006-08-14 21:34:08 +0000 | [diff] [blame^] | 277 | def test_method_in_dynamic_class(self): |
| 278 | self.assertSourceEqual(mod2.method_in_dynamic_class, 95, 97) |
| 279 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 280 | # Helper for testing classify_class_attrs. |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 281 | def attrs_wo_objs(cls): |
| 282 | return [t[:3] for t in inspect.classify_class_attrs(cls)] |
| 283 | |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 284 | class TestClassesAndFunctions(unittest.TestCase): |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 285 | def test_classic_mro(self): |
| 286 | # Test classic-class method resolution order. |
| 287 | class A: pass |
| 288 | class B(A): pass |
| 289 | class C(A): pass |
| 290 | class D(B, C): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 291 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 292 | expected = (D, B, A, C) |
| 293 | got = inspect.getmro(D) |
| 294 | self.assertEqual(expected, got) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 295 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 296 | def test_newstyle_mro(self): |
| 297 | # The same w/ new-class MRO. |
| 298 | class A(object): pass |
| 299 | class B(A): pass |
| 300 | class C(A): pass |
| 301 | class D(B, C): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 302 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 303 | expected = (D, B, C, A, object) |
| 304 | got = inspect.getmro(D) |
| 305 | self.assertEqual(expected, got) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 306 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 307 | def assertArgSpecEquals(self, routine, args_e, varargs_e = None, |
| 308 | varkw_e = None, defaults_e = None, |
| 309 | formatted = None): |
| 310 | args, varargs, varkw, defaults = inspect.getargspec(routine) |
| 311 | self.assertEqual(args, args_e) |
| 312 | self.assertEqual(varargs, varargs_e) |
| 313 | self.assertEqual(varkw, varkw_e) |
| 314 | self.assertEqual(defaults, defaults_e) |
| 315 | if formatted is not None: |
| 316 | self.assertEqual(inspect.formatargspec(args, varargs, varkw, defaults), |
| 317 | formatted) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 318 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 319 | def test_getargspec(self): |
| 320 | self.assertArgSpecEquals(mod.eggs, ['x', 'y'], formatted = '(x, y)') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 321 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 322 | self.assertArgSpecEquals(mod.spam, |
| 323 | ['a', 'b', 'c', 'd', ['e', ['f']]], |
| 324 | 'g', 'h', (3, (4, (5,))), |
| 325 | '(a, b, c, d=3, (e, (f,))=(4, (5,)), *g, **h)') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 326 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 327 | def test_getargspec_method(self): |
| 328 | class A(object): |
| 329 | def m(self): |
| 330 | pass |
| 331 | self.assertArgSpecEquals(A.m, ['self']) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 332 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 333 | def test_getargspec_sublistofone(self): |
Neal Norwitz | 33b730e | 2006-03-27 08:58:23 +0000 | [diff] [blame] | 334 | def sublistOfOne((foo,)): return 1 |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 335 | self.assertArgSpecEquals(sublistOfOne, [['foo']]) |
| 336 | |
Neal Norwitz | 33b730e | 2006-03-27 08:58:23 +0000 | [diff] [blame] | 337 | def fakeSublistOfOne((foo)): return 1 |
| 338 | self.assertArgSpecEquals(fakeSublistOfOne, ['foo']) |
| 339 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 340 | def test_classify_oldstyle(self): |
| 341 | class A: |
| 342 | def s(): pass |
| 343 | s = staticmethod(s) |
| 344 | |
| 345 | def c(cls): pass |
| 346 | c = classmethod(c) |
| 347 | |
| 348 | def getp(self): pass |
| 349 | p = property(getp) |
| 350 | |
| 351 | def m(self): pass |
| 352 | |
| 353 | def m1(self): pass |
| 354 | |
| 355 | datablob = '1' |
| 356 | |
| 357 | attrs = attrs_wo_objs(A) |
| 358 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 359 | self.assert_(('c', 'class method', A) in attrs, 'missing class method') |
| 360 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 361 | self.assert_(('m', 'method', A) in attrs, 'missing plain method') |
| 362 | self.assert_(('m1', 'method', A) in attrs, 'missing plain method') |
| 363 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
| 364 | |
| 365 | class B(A): |
| 366 | def m(self): pass |
| 367 | |
| 368 | attrs = attrs_wo_objs(B) |
| 369 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 370 | self.assert_(('c', 'class method', A) in attrs, 'missing class method') |
| 371 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 372 | self.assert_(('m', 'method', B) in attrs, 'missing plain method') |
| 373 | self.assert_(('m1', 'method', A) in attrs, 'missing plain method') |
| 374 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 375 | |
| 376 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 377 | class C(A): |
| 378 | def m(self): pass |
| 379 | def c(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 380 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 381 | attrs = attrs_wo_objs(C) |
| 382 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 383 | self.assert_(('c', 'method', C) in attrs, 'missing plain method') |
| 384 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 385 | self.assert_(('m', 'method', C) in attrs, 'missing plain method') |
| 386 | self.assert_(('m1', 'method', A) in attrs, 'missing plain method') |
| 387 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 388 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 389 | class D(B, C): |
| 390 | def m1(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 391 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 392 | attrs = attrs_wo_objs(D) |
| 393 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 394 | self.assert_(('c', 'class method', A) in attrs, 'missing class method') |
| 395 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 396 | self.assert_(('m', 'method', B) in attrs, 'missing plain method') |
| 397 | self.assert_(('m1', 'method', D) in attrs, 'missing plain method') |
| 398 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 399 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 400 | # Repeat all that, but w/ new-style classes. |
| 401 | def test_classify_newstyle(self): |
| 402 | class A(object): |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 403 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 404 | def s(): pass |
| 405 | s = staticmethod(s) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 406 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 407 | def c(cls): pass |
| 408 | c = classmethod(c) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 409 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 410 | def getp(self): pass |
| 411 | p = property(getp) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 412 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 413 | def m(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 414 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 415 | def m1(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 416 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 417 | datablob = '1' |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 418 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 419 | attrs = attrs_wo_objs(A) |
| 420 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 421 | self.assert_(('c', 'class method', A) in attrs, 'missing class method') |
| 422 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 423 | self.assert_(('m', 'method', A) in attrs, 'missing plain method') |
| 424 | self.assert_(('m1', 'method', A) in attrs, 'missing plain method') |
| 425 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 426 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 427 | class B(A): |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 428 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 429 | def m(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 430 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 431 | attrs = attrs_wo_objs(B) |
| 432 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 433 | self.assert_(('c', 'class method', A) in attrs, 'missing class method') |
| 434 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 435 | self.assert_(('m', 'method', B) in attrs, 'missing plain method') |
| 436 | self.assert_(('m1', 'method', A) in attrs, 'missing plain method') |
| 437 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 438 | |
| 439 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 440 | class C(A): |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 441 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 442 | def m(self): pass |
| 443 | def c(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 444 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 445 | attrs = attrs_wo_objs(C) |
| 446 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 447 | self.assert_(('c', 'method', C) in attrs, 'missing plain method') |
| 448 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 449 | self.assert_(('m', 'method', C) in attrs, 'missing plain method') |
| 450 | self.assert_(('m1', 'method', A) in attrs, 'missing plain method') |
| 451 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 452 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 453 | class D(B, C): |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 454 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 455 | def m1(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 456 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 457 | attrs = attrs_wo_objs(D) |
| 458 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 459 | self.assert_(('c', 'method', C) in attrs, 'missing plain method') |
| 460 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 461 | self.assert_(('m', 'method', B) in attrs, 'missing plain method') |
| 462 | self.assert_(('m1', 'method', D) in attrs, 'missing plain method') |
| 463 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Jeremy Hylton | c4bf5ed | 2003-06-27 18:43:12 +0000 | [diff] [blame] | 464 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 465 | def test_main(): |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 466 | run_unittest(TestDecorators, TestRetrievingSourceCode, TestOneliners, |
Johannes Gijsbers | a5855d5 | 2005-03-12 16:37:11 +0000 | [diff] [blame] | 467 | TestBuggyCases, |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 468 | TestInterpreterStack, TestClassesAndFunctions, TestPredicates) |
Martin v. Löwis | 893ffa4 | 2003-10-31 15:35:53 +0000 | [diff] [blame] | 469 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 470 | if __name__ == "__main__": |
| 471 | test_main() |