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