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