Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 1 | import sys |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2 | import types |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 3 | import unittest |
| 4 | import inspect |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5 | import datetime |
Guido van Rossum | 813b0e5 | 2007-05-21 18:11:34 +0000 | [diff] [blame] | 6 | import collections |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 7 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 8 | from test.test_support import TESTFN, run_unittest |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 9 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 10 | from test import inspect_fodder as mod |
| 11 | from test import inspect_fodder2 as mod2 |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 12 | |
| 13 | # Functions tested in this suite: |
| 14 | # ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode, |
| 15 | # isbuiltin, isroutine, getmembers, getdoc, getfile, getmodule, |
| 16 | # getsourcefile, getcomments, getsource, getclasstree, getargspec, |
| 17 | # getargvalues, formatargspec, formatargvalues, currentframe, stack, trace |
Martin v. Löwis | e59e2ba | 2003-05-03 09:09:02 +0000 | [diff] [blame] | 18 | # isdatadescriptor |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 19 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 20 | modfile = mod.__file__ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 21 | if modfile.endswith(('c', 'o')): |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 22 | modfile = modfile[:-1] |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 23 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 24 | import __builtin__ |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 25 | |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 26 | try: |
| 27 | 1/0 |
| 28 | except: |
| 29 | tb = sys.exc_traceback |
| 30 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 31 | git = mod.StupidGit() |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 32 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 33 | class IsTestBase(unittest.TestCase): |
| 34 | predicates = set([inspect.isbuiltin, inspect.isclass, inspect.iscode, |
| 35 | inspect.isframe, inspect.isfunction, inspect.ismethod, |
| 36 | inspect.ismodule, inspect.istraceback]) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 37 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 38 | def istest(self, predicate, exp): |
| 39 | obj = eval(exp) |
| 40 | self.failUnless(predicate(obj), '%s(%s)' % (predicate.__name__, exp)) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 41 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 42 | for other in self.predicates - set([predicate]): |
| 43 | self.failIf(other(obj), 'not %s(%s)' % (other.__name__, exp)) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 44 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 45 | class TestPredicates(IsTestBase): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 46 | def test_thirteen(self): |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 47 | count = len(filter(lambda x:x.startswith('is'), dir(inspect))) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 48 | # Doc/lib/libinspect.tex claims there are 13 such functions |
| 49 | expected = 13 |
| 50 | err_msg = "There are %d (not %d) is* functions" % (count, expected) |
| 51 | self.assertEqual(count, expected, err_msg) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 52 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 53 | def test_excluding_predicates(self): |
| 54 | self.istest(inspect.isbuiltin, 'sys.exit') |
| 55 | self.istest(inspect.isbuiltin, '[].append') |
| 56 | self.istest(inspect.isclass, 'mod.StupidGit') |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 57 | self.istest(inspect.iscode, 'mod.spam.__code__') |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 58 | self.istest(inspect.isframe, 'tb.tb_frame') |
| 59 | self.istest(inspect.isfunction, 'mod.spam') |
| 60 | self.istest(inspect.ismethod, 'mod.StupidGit.abuse') |
| 61 | self.istest(inspect.ismethod, 'git.argue') |
| 62 | self.istest(inspect.ismodule, 'mod') |
| 63 | self.istest(inspect.istraceback, 'tb') |
Guido van Rossum | 813b0e5 | 2007-05-21 18:11:34 +0000 | [diff] [blame] | 64 | self.istest(inspect.isdatadescriptor, 'collections.defaultdict.default_factory') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +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) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 120 | self.assertEqual(args, ['a', 'b', 'c', 'd', 'e', 'f']) |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 121 | self.assertEqual(varargs, 'g') |
| 122 | self.assertEqual(varkw, 'h') |
| 123 | self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals), |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 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 | |
Alex Martelli | 01c77c6 | 2006-08-24 02:58:11 +0000 | [diff] [blame] | 133 | self.source = open(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, |
Thomas Wouters | 725af87 | 2006-04-15 09:13:19 +0000 | [diff] [blame] | 155 | [(object, ()), |
| 156 | [(mod.ParrotDroppings, (object,)), |
| 157 | (mod.StupidGit, (object,)), |
| 158 | [(mod.MalodorousPervert, (mod.StupidGit,)), |
| 159 | [(mod.FesteringGob, (mod.MalodorousPervert, |
| 160 | mod.ParrotDroppings)) |
| 161 | ] |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 162 | ] |
| 163 | ] |
| 164 | ]) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 165 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 166 | def test_getfunctions(self): |
| 167 | functions = inspect.getmembers(mod, inspect.isfunction) |
| 168 | self.assertEqual(functions, [('eggs', mod.eggs), |
| 169 | ('spam', mod.spam)]) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 170 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 171 | def test_getdoc(self): |
| 172 | self.assertEqual(inspect.getdoc(mod), 'A module docstring.') |
| 173 | self.assertEqual(inspect.getdoc(mod.StupidGit), |
| 174 | 'A longer,\n\nindented\n\ndocstring.') |
| 175 | self.assertEqual(inspect.getdoc(git.abuse), |
| 176 | 'Another\n\ndocstring\n\ncontaining\n\ntabs') |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 177 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 178 | def test_getcomments(self): |
| 179 | self.assertEqual(inspect.getcomments(mod), '# line 1\n') |
| 180 | self.assertEqual(inspect.getcomments(mod.StupidGit), '# line 20\n') |
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_getmodule(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 183 | # Check actual module |
| 184 | self.assertEqual(inspect.getmodule(mod), mod) |
| 185 | # Check class (uses __module__ attribute) |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 186 | self.assertEqual(inspect.getmodule(mod.StupidGit), mod) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 187 | # Check a method (no __module__ attribute, falls back to filename) |
| 188 | self.assertEqual(inspect.getmodule(mod.StupidGit.abuse), mod) |
| 189 | # Do it again (check the caching isn't broken) |
| 190 | self.assertEqual(inspect.getmodule(mod.StupidGit.abuse), mod) |
| 191 | # Check a builtin |
| 192 | self.assertEqual(inspect.getmodule(str), sys.modules["__builtin__"]) |
| 193 | # Check filename override |
| 194 | self.assertEqual(inspect.getmodule(None, modfile), mod) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 195 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 196 | def test_getsource(self): |
| 197 | self.assertSourceEqual(git.abuse, 29, 39) |
| 198 | self.assertSourceEqual(mod.StupidGit, 21, 46) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 199 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 200 | def test_getsourcefile(self): |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 201 | self.assertEqual(inspect.getsourcefile(mod.spam), modfile) |
| 202 | self.assertEqual(inspect.getsourcefile(git.abuse), modfile) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 203 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 204 | def test_getfile(self): |
| 205 | self.assertEqual(inspect.getfile(mod.StupidGit), mod.__file__) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 206 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 207 | def test_getmodule_recursion(self): |
| 208 | from new import module |
| 209 | name = '__inspect_dummy' |
| 210 | m = sys.modules[name] = module(name) |
| 211 | m.__file__ = "<string>" # hopefully not a real filename... |
| 212 | m.__loader__ = "dummy" # pretend the filename is understood by a loader |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 213 | exec("def x(): pass", m.__dict__) |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 214 | self.assertEqual(inspect.getsourcefile(m.x.__code__), '<string>') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 215 | del sys.modules[name] |
| 216 | inspect.getmodule(compile('a=10','','single')) |
| 217 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 218 | class TestDecorators(GetSourceBase): |
| 219 | fodderFile = mod2 |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 220 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 221 | def test_wrapped_decorator(self): |
| 222 | self.assertSourceEqual(mod2.wrapped, 14, 17) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 223 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 224 | def test_replacing_decorator(self): |
| 225 | self.assertSourceEqual(mod2.gone, 9, 10) |
Tim Peters | e0b2d7a | 2001-09-22 06:10:55 +0000 | [diff] [blame] | 226 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 227 | class TestOneliners(GetSourceBase): |
| 228 | fodderFile = mod2 |
| 229 | def test_oneline_lambda(self): |
| 230 | # Test inspect.getsource with a one-line lambda function. |
| 231 | self.assertSourceEqual(mod2.oll, 25, 25) |
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_threeline_lambda(self): |
| 234 | # Test inspect.getsource with a three-line lambda function, |
| 235 | # where the second and third lines are _not_ indented. |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 236 | self.assertSourceEqual(mod2.tll, 28, 30) |
| 237 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 238 | def test_twoline_indented_lambda(self): |
| 239 | # Test inspect.getsource with a two-line lambda function, |
| 240 | # where the second line _is_ indented. |
| 241 | self.assertSourceEqual(mod2.tlli, 33, 34) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 242 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 243 | def test_onelinefunc(self): |
| 244 | # Test inspect.getsource with a regular one-line function. |
| 245 | self.assertSourceEqual(mod2.onelinefunc, 37, 37) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 246 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 247 | def test_manyargs(self): |
| 248 | # Test inspect.getsource with a regular function where |
| 249 | # the arguments are on two lines and _not_ indented and |
| 250 | # the body on the second line with the last arguments. |
| 251 | self.assertSourceEqual(mod2.manyargs, 40, 41) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 252 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 253 | def test_twolinefunc(self): |
| 254 | # Test inspect.getsource with a regular function where |
| 255 | # the body is on two lines, following the argument list and |
| 256 | # continued on the next line by a \\. |
| 257 | self.assertSourceEqual(mod2.twolinefunc, 44, 45) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 258 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 259 | def test_lambda_in_list(self): |
| 260 | # Test inspect.getsource with a one-line lambda function |
| 261 | # defined in a list, indented. |
| 262 | self.assertSourceEqual(mod2.a[1], 49, 49) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 263 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 264 | def test_anonymous(self): |
| 265 | # Test inspect.getsource with a lambda function defined |
| 266 | # as argument to another function. |
| 267 | self.assertSourceEqual(mod2.anonymous, 55, 55) |
| 268 | |
Johannes Gijsbers | a5855d5 | 2005-03-12 16:37:11 +0000 | [diff] [blame] | 269 | class TestBuggyCases(GetSourceBase): |
| 270 | fodderFile = mod2 |
| 271 | |
| 272 | def test_with_comment(self): |
| 273 | self.assertSourceEqual(mod2.with_comment, 58, 59) |
| 274 | |
| 275 | def test_multiline_sig(self): |
| 276 | self.assertSourceEqual(mod2.multiline_sig[0], 63, 64) |
| 277 | |
Armin Rigo | dd5c023 | 2005-09-25 11:45:45 +0000 | [diff] [blame] | 278 | def test_nested_class(self): |
| 279 | self.assertSourceEqual(mod2.func69().func71, 71, 72) |
| 280 | |
| 281 | def test_one_liner_followed_by_non_name(self): |
| 282 | self.assertSourceEqual(mod2.func77, 77, 77) |
| 283 | |
| 284 | def test_one_liner_dedent_non_name(self): |
| 285 | self.assertSourceEqual(mod2.cls82.func83, 83, 83) |
| 286 | |
| 287 | def test_with_comment_instead_of_docstring(self): |
| 288 | self.assertSourceEqual(mod2.func88, 88, 90) |
| 289 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 290 | def test_method_in_dynamic_class(self): |
| 291 | self.assertSourceEqual(mod2.method_in_dynamic_class, 95, 97) |
| 292 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 293 | # Helper for testing classify_class_attrs. |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 294 | def attrs_wo_objs(cls): |
| 295 | return [t[:3] for t in inspect.classify_class_attrs(cls)] |
| 296 | |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 297 | class TestClassesAndFunctions(unittest.TestCase): |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 298 | def test_newstyle_mro(self): |
| 299 | # The same w/ new-class MRO. |
| 300 | class A(object): pass |
| 301 | class B(A): pass |
| 302 | class C(A): pass |
| 303 | class D(B, C): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 304 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 305 | expected = (D, B, C, A, object) |
| 306 | got = inspect.getmro(D) |
| 307 | self.assertEqual(expected, got) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 308 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 309 | def assertArgSpecEquals(self, routine, args_e, varargs_e = None, |
| 310 | varkw_e = None, defaults_e = None, |
| 311 | formatted = None): |
| 312 | args, varargs, varkw, defaults = inspect.getargspec(routine) |
| 313 | self.assertEqual(args, args_e) |
| 314 | self.assertEqual(varargs, varargs_e) |
| 315 | self.assertEqual(varkw, varkw_e) |
| 316 | self.assertEqual(defaults, defaults_e) |
| 317 | if formatted is not None: |
| 318 | self.assertEqual(inspect.formatargspec(args, varargs, varkw, defaults), |
| 319 | formatted) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 320 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 321 | def test_getargspec(self): |
| 322 | self.assertArgSpecEquals(mod.eggs, ['x', 'y'], formatted = '(x, y)') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 323 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 324 | self.assertArgSpecEquals(mod.spam, |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 325 | ['a', 'b', 'c', 'd', 'e', 'f'], |
| 326 | 'g', 'h', (3, 4, 5), |
| 327 | '(a, b, c, d=3, e=4, f=5, *g, **h)') |
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_method(self): |
| 330 | class A(object): |
| 331 | def m(self): |
| 332 | pass |
| 333 | self.assertArgSpecEquals(A.m, ['self']) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 334 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 335 | def test_classify_newstyle(self): |
| 336 | class A(object): |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 337 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 338 | def s(): pass |
| 339 | s = staticmethod(s) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 340 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 341 | def c(cls): pass |
| 342 | c = classmethod(c) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 343 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 344 | def getp(self): pass |
| 345 | p = property(getp) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 346 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 347 | def m(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 348 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 349 | def m1(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 350 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 351 | datablob = '1' |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 352 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 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') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 360 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 361 | class B(A): |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 362 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 363 | def m(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 364 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 365 | attrs = attrs_wo_objs(B) |
| 366 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 367 | self.assert_(('c', 'class method', A) in attrs, 'missing class method') |
| 368 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 369 | self.assert_(('m', 'method', B) in attrs, 'missing plain method') |
| 370 | self.assert_(('m1', 'method', A) in attrs, 'missing plain method') |
| 371 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 372 | |
| 373 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 374 | class C(A): |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 375 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 376 | def m(self): pass |
| 377 | def c(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 378 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 379 | attrs = attrs_wo_objs(C) |
| 380 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 381 | self.assert_(('c', 'method', C) in attrs, 'missing plain method') |
| 382 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 383 | self.assert_(('m', 'method', C) in attrs, 'missing plain method') |
| 384 | self.assert_(('m1', 'method', A) in attrs, 'missing plain method') |
| 385 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 386 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 387 | class D(B, C): |
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 | def m1(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 390 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 391 | attrs = attrs_wo_objs(D) |
| 392 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 393 | self.assert_(('c', 'method', C) in attrs, 'missing plain method') |
| 394 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 395 | self.assert_(('m', 'method', B) in attrs, 'missing plain method') |
| 396 | self.assert_(('m1', 'method', D) in attrs, 'missing plain method') |
| 397 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Jeremy Hylton | c4bf5ed | 2003-06-27 18:43:12 +0000 | [diff] [blame] | 398 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 399 | def test_main(): |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 400 | run_unittest(TestDecorators, TestRetrievingSourceCode, TestOneliners, |
Johannes Gijsbers | a5855d5 | 2005-03-12 16:37:11 +0000 | [diff] [blame] | 401 | TestBuggyCases, |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 402 | TestInterpreterStack, TestClassesAndFunctions, TestPredicates) |
Martin v. Löwis | 893ffa4 | 2003-10-31 15:35:53 +0000 | [diff] [blame] | 403 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 404 | if __name__ == "__main__": |
| 405 | test_main() |