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