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