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 | |
Amaury Forgeot d'Arc | b54447f | 2009-01-13 23:39:22 +0000 | [diff] [blame] | 94 | def test_get_slot_members(self): |
| 95 | class C(object): |
| 96 | __slots__ = ("a", "b") |
| 97 | |
| 98 | x = C() |
| 99 | x.a = 42 |
| 100 | members = dict(inspect.getmembers(x)) |
| 101 | self.assert_('a' in members) |
| 102 | self.assert_('b' not in members) |
| 103 | |
| 104 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 105 | class TestInterpreterStack(IsTestBase): |
| 106 | def __init__(self, *args, **kwargs): |
| 107 | unittest.TestCase.__init__(self, *args, **kwargs) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 108 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 109 | git.abuse(7, 8, 9) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 110 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 111 | def test_abuse_done(self): |
| 112 | self.istest(inspect.istraceback, 'git.ex[2]') |
| 113 | self.istest(inspect.isframe, 'mod.fr') |
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_stack(self): |
| 116 | self.assert_(len(mod.st) >= 5) |
| 117 | self.assertEqual(mod.st[0][1:], |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 118 | (modfile, 16, 'eggs', [' st = inspect.stack()\n'], 0)) |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 119 | self.assertEqual(mod.st[1][1:], |
| 120 | (modfile, 9, 'spam', [' eggs(b + d, c + f)\n'], 0)) |
| 121 | self.assertEqual(mod.st[2][1:], |
| 122 | (modfile, 43, 'argue', [' spam(a, b, c)\n'], 0)) |
| 123 | self.assertEqual(mod.st[3][1:], |
| 124 | (modfile, 39, 'abuse', [' self.argue(a, b, c)\n'], 0)) |
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_trace(self): |
| 127 | self.assertEqual(len(git.tr), 3) |
| 128 | self.assertEqual(git.tr[0][1:], (modfile, 43, 'argue', |
| 129 | [' spam(a, b, c)\n'], 0)) |
| 130 | self.assertEqual(git.tr[1][1:], (modfile, 9, 'spam', |
| 131 | [' eggs(b + d, c + f)\n'], 0)) |
| 132 | self.assertEqual(git.tr[2][1:], (modfile, 18, 'eggs', |
| 133 | [' q = y / 0\n'], 0)) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 134 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 135 | def test_frame(self): |
| 136 | args, varargs, varkw, locals = inspect.getargvalues(mod.fr) |
| 137 | self.assertEqual(args, ['x', 'y']) |
| 138 | self.assertEqual(varargs, None) |
| 139 | self.assertEqual(varkw, None) |
| 140 | self.assertEqual(locals, {'x': 11, 'p': 11, 'y': 14}) |
| 141 | self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals), |
| 142 | '(x=11, y=14)') |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 143 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 144 | def test_previous_frame(self): |
| 145 | args, varargs, varkw, locals = inspect.getargvalues(mod.fr.f_back) |
| 146 | self.assertEqual(args, ['a', 'b', 'c', 'd', ['e', ['f']]]) |
| 147 | self.assertEqual(varargs, 'g') |
| 148 | self.assertEqual(varkw, 'h') |
| 149 | self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals), |
| 150 | '(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] | 151 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 152 | class GetSourceBase(unittest.TestCase): |
| 153 | # Subclasses must override. |
| 154 | fodderFile = None |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 155 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 156 | def __init__(self, *args, **kwargs): |
| 157 | unittest.TestCase.__init__(self, *args, **kwargs) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 158 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 159 | self.source = file(inspect.getsourcefile(self.fodderFile)).read() |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 160 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 161 | def sourcerange(self, top, bottom): |
| 162 | lines = self.source.split("\n") |
| 163 | return "\n".join(lines[top-1:bottom]) + "\n" |
Tim Peters | e0b2d7a | 2001-09-22 06:10:55 +0000 | [diff] [blame] | 164 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 165 | def assertSourceEqual(self, obj, top, bottom): |
| 166 | self.assertEqual(inspect.getsource(obj), |
| 167 | self.sourcerange(top, bottom)) |
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 | class TestRetrievingSourceCode(GetSourceBase): |
| 170 | fodderFile = mod |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 171 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 172 | def test_getclasses(self): |
| 173 | classes = inspect.getmembers(mod, inspect.isclass) |
| 174 | self.assertEqual(classes, |
| 175 | [('FesteringGob', mod.FesteringGob), |
| 176 | ('MalodorousPervert', mod.MalodorousPervert), |
| 177 | ('ParrotDroppings', mod.ParrotDroppings), |
| 178 | ('StupidGit', mod.StupidGit)]) |
| 179 | tree = inspect.getclasstree([cls[1] for cls in classes], 1) |
| 180 | self.assertEqual(tree, |
| 181 | [(mod.ParrotDroppings, ()), |
| 182 | (mod.StupidGit, ()), |
| 183 | [(mod.MalodorousPervert, (mod.StupidGit,)), |
| 184 | [(mod.FesteringGob, (mod.MalodorousPervert, |
| 185 | mod.ParrotDroppings)) |
| 186 | ] |
| 187 | ] |
| 188 | ]) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 189 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 190 | def test_getfunctions(self): |
| 191 | functions = inspect.getmembers(mod, inspect.isfunction) |
| 192 | self.assertEqual(functions, [('eggs', mod.eggs), |
| 193 | ('spam', mod.spam)]) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 194 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 195 | def test_getdoc(self): |
| 196 | self.assertEqual(inspect.getdoc(mod), 'A module docstring.') |
| 197 | self.assertEqual(inspect.getdoc(mod.StupidGit), |
| 198 | 'A longer,\n\nindented\n\ndocstring.') |
| 199 | self.assertEqual(inspect.getdoc(git.abuse), |
| 200 | 'Another\n\ndocstring\n\ncontaining\n\ntabs') |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 201 | |
Georg Brandl | 7be19aa | 2008-06-07 15:59:10 +0000 | [diff] [blame] | 202 | def test_cleandoc(self): |
| 203 | self.assertEqual(inspect.cleandoc('An\n indented\n docstring.'), |
| 204 | 'An\nindented\ndocstring.') |
| 205 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 206 | def test_getcomments(self): |
| 207 | self.assertEqual(inspect.getcomments(mod), '# line 1\n') |
| 208 | self.assertEqual(inspect.getcomments(mod.StupidGit), '# line 20\n') |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 209 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 210 | def test_getmodule(self): |
Nick Coghlan | c495c66 | 2006-09-07 10:50:34 +0000 | [diff] [blame] | 211 | # Check actual module |
| 212 | self.assertEqual(inspect.getmodule(mod), mod) |
| 213 | # Check class (uses __module__ attribute) |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 214 | self.assertEqual(inspect.getmodule(mod.StupidGit), mod) |
Nick Coghlan | c495c66 | 2006-09-07 10:50:34 +0000 | [diff] [blame] | 215 | # Check a method (no __module__ attribute, falls back to filename) |
| 216 | self.assertEqual(inspect.getmodule(mod.StupidGit.abuse), mod) |
| 217 | # Do it again (check the caching isn't broken) |
| 218 | self.assertEqual(inspect.getmodule(mod.StupidGit.abuse), mod) |
| 219 | # Check a builtin |
| 220 | self.assertEqual(inspect.getmodule(str), sys.modules["__builtin__"]) |
| 221 | # Check filename override |
| 222 | self.assertEqual(inspect.getmodule(None, modfile), mod) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 223 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 224 | def test_getsource(self): |
| 225 | self.assertSourceEqual(git.abuse, 29, 39) |
| 226 | self.assertSourceEqual(mod.StupidGit, 21, 46) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 227 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 228 | def test_getsourcefile(self): |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 229 | self.assertEqual(inspect.getsourcefile(mod.spam), modfile) |
| 230 | self.assertEqual(inspect.getsourcefile(git.abuse), modfile) |
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_getfile(self): |
| 233 | self.assertEqual(inspect.getfile(mod.StupidGit), mod.__file__) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 234 | |
Phillip J. Eby | 5d86bdb | 2006-07-10 19:03:29 +0000 | [diff] [blame] | 235 | def test_getmodule_recursion(self): |
Christian Heimes | c756d00 | 2007-11-27 21:34:01 +0000 | [diff] [blame] | 236 | from types import ModuleType |
Phillip J. Eby | 5d86bdb | 2006-07-10 19:03:29 +0000 | [diff] [blame] | 237 | name = '__inspect_dummy' |
Christian Heimes | c756d00 | 2007-11-27 21:34:01 +0000 | [diff] [blame] | 238 | m = sys.modules[name] = ModuleType(name) |
Tim Peters | 722b883 | 2006-07-10 21:11:49 +0000 | [diff] [blame] | 239 | m.__file__ = "<string>" # hopefully not a real filename... |
Phillip J. Eby | 5d86bdb | 2006-07-10 19:03:29 +0000 | [diff] [blame] | 240 | m.__loader__ = "dummy" # pretend the filename is understood by a loader |
| 241 | exec "def x(): pass" in m.__dict__ |
| 242 | self.assertEqual(inspect.getsourcefile(m.x.func_code), '<string>') |
| 243 | del sys.modules[name] |
Phillip J. Eby | 1a2959c | 2006-07-20 15:54:16 +0000 | [diff] [blame] | 244 | inspect.getmodule(compile('a=10','','single')) |
Phillip J. Eby | 5d86bdb | 2006-07-10 19:03:29 +0000 | [diff] [blame] | 245 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 246 | class TestDecorators(GetSourceBase): |
| 247 | fodderFile = mod2 |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 248 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 249 | def test_wrapped_decorator(self): |
| 250 | self.assertSourceEqual(mod2.wrapped, 14, 17) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 251 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 252 | def test_replacing_decorator(self): |
| 253 | self.assertSourceEqual(mod2.gone, 9, 10) |
Tim Peters | e0b2d7a | 2001-09-22 06:10:55 +0000 | [diff] [blame] | 254 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 255 | class TestOneliners(GetSourceBase): |
| 256 | fodderFile = mod2 |
| 257 | def test_oneline_lambda(self): |
| 258 | # Test inspect.getsource with a one-line lambda function. |
| 259 | self.assertSourceEqual(mod2.oll, 25, 25) |
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_threeline_lambda(self): |
| 262 | # Test inspect.getsource with a three-line lambda function, |
| 263 | # where the second and third lines are _not_ indented. |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 264 | self.assertSourceEqual(mod2.tll, 28, 30) |
| 265 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 266 | def test_twoline_indented_lambda(self): |
| 267 | # Test inspect.getsource with a two-line lambda function, |
| 268 | # where the second line _is_ indented. |
| 269 | self.assertSourceEqual(mod2.tlli, 33, 34) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 270 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 271 | def test_onelinefunc(self): |
| 272 | # Test inspect.getsource with a regular one-line function. |
| 273 | self.assertSourceEqual(mod2.onelinefunc, 37, 37) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 274 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 275 | def test_manyargs(self): |
| 276 | # Test inspect.getsource with a regular function where |
| 277 | # the arguments are on two lines and _not_ indented and |
| 278 | # the body on the second line with the last arguments. |
| 279 | self.assertSourceEqual(mod2.manyargs, 40, 41) |
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_twolinefunc(self): |
| 282 | # Test inspect.getsource with a regular function where |
| 283 | # the body is on two lines, following the argument list and |
| 284 | # continued on the next line by a \\. |
| 285 | self.assertSourceEqual(mod2.twolinefunc, 44, 45) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 286 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 287 | def test_lambda_in_list(self): |
| 288 | # Test inspect.getsource with a one-line lambda function |
| 289 | # defined in a list, indented. |
| 290 | self.assertSourceEqual(mod2.a[1], 49, 49) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 291 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 292 | def test_anonymous(self): |
| 293 | # Test inspect.getsource with a lambda function defined |
| 294 | # as argument to another function. |
| 295 | self.assertSourceEqual(mod2.anonymous, 55, 55) |
| 296 | |
Johannes Gijsbers | a5855d5 | 2005-03-12 16:37:11 +0000 | [diff] [blame] | 297 | class TestBuggyCases(GetSourceBase): |
| 298 | fodderFile = mod2 |
| 299 | |
| 300 | def test_with_comment(self): |
| 301 | self.assertSourceEqual(mod2.with_comment, 58, 59) |
| 302 | |
| 303 | def test_multiline_sig(self): |
| 304 | self.assertSourceEqual(mod2.multiline_sig[0], 63, 64) |
| 305 | |
Armin Rigo | dd5c023 | 2005-09-25 11:45:45 +0000 | [diff] [blame] | 306 | def test_nested_class(self): |
| 307 | self.assertSourceEqual(mod2.func69().func71, 71, 72) |
| 308 | |
| 309 | def test_one_liner_followed_by_non_name(self): |
| 310 | self.assertSourceEqual(mod2.func77, 77, 77) |
| 311 | |
| 312 | def test_one_liner_dedent_non_name(self): |
| 313 | self.assertSourceEqual(mod2.cls82.func83, 83, 83) |
| 314 | |
| 315 | def test_with_comment_instead_of_docstring(self): |
| 316 | self.assertSourceEqual(mod2.func88, 88, 90) |
| 317 | |
Georg Brandl | 2463f8f | 2006-08-14 21:34:08 +0000 | [diff] [blame] | 318 | def test_method_in_dynamic_class(self): |
| 319 | self.assertSourceEqual(mod2.method_in_dynamic_class, 95, 97) |
| 320 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 321 | # Helper for testing classify_class_attrs. |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 322 | def attrs_wo_objs(cls): |
| 323 | return [t[:3] for t in inspect.classify_class_attrs(cls)] |
| 324 | |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 325 | class TestClassesAndFunctions(unittest.TestCase): |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 326 | def test_classic_mro(self): |
| 327 | # Test classic-class method resolution order. |
| 328 | class A: 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, A, C) |
| 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 test_newstyle_mro(self): |
| 338 | # The same w/ new-class MRO. |
| 339 | class A(object): pass |
| 340 | class B(A): pass |
| 341 | class C(A): pass |
| 342 | class D(B, C): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 343 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 344 | expected = (D, B, C, A, object) |
| 345 | got = inspect.getmro(D) |
| 346 | self.assertEqual(expected, got) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 347 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 348 | def assertArgSpecEquals(self, routine, args_e, varargs_e = None, |
| 349 | varkw_e = None, defaults_e = None, |
| 350 | formatted = None): |
| 351 | args, varargs, varkw, defaults = inspect.getargspec(routine) |
| 352 | self.assertEqual(args, args_e) |
| 353 | self.assertEqual(varargs, varargs_e) |
| 354 | self.assertEqual(varkw, varkw_e) |
| 355 | self.assertEqual(defaults, defaults_e) |
| 356 | if formatted is not None: |
| 357 | self.assertEqual(inspect.formatargspec(args, varargs, varkw, defaults), |
| 358 | formatted) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 359 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 360 | def test_getargspec(self): |
| 361 | self.assertArgSpecEquals(mod.eggs, ['x', 'y'], formatted = '(x, y)') |
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 | self.assertArgSpecEquals(mod.spam, |
| 364 | ['a', 'b', 'c', 'd', ['e', ['f']]], |
| 365 | 'g', 'h', (3, (4, (5,))), |
| 366 | '(a, b, c, d=3, (e, (f,))=(4, (5,)), *g, **h)') |
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 | def test_getargspec_method(self): |
| 369 | class A(object): |
| 370 | def m(self): |
| 371 | pass |
| 372 | self.assertArgSpecEquals(A.m, ['self']) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 373 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 374 | def test_getargspec_sublistofone(self): |
Neal Norwitz | 33b730e | 2006-03-27 08:58:23 +0000 | [diff] [blame] | 375 | def sublistOfOne((foo,)): return 1 |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 376 | self.assertArgSpecEquals(sublistOfOne, [['foo']]) |
| 377 | |
Neal Norwitz | 33b730e | 2006-03-27 08:58:23 +0000 | [diff] [blame] | 378 | def fakeSublistOfOne((foo)): return 1 |
| 379 | self.assertArgSpecEquals(fakeSublistOfOne, ['foo']) |
| 380 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 381 | def test_classify_oldstyle(self): |
| 382 | class A: |
| 383 | def s(): pass |
| 384 | s = staticmethod(s) |
| 385 | |
| 386 | def c(cls): pass |
| 387 | c = classmethod(c) |
| 388 | |
| 389 | def getp(self): pass |
| 390 | p = property(getp) |
| 391 | |
| 392 | def m(self): pass |
| 393 | |
| 394 | def m1(self): pass |
| 395 | |
| 396 | datablob = '1' |
| 397 | |
| 398 | attrs = attrs_wo_objs(A) |
| 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', A) 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') |
| 405 | |
| 406 | class B(A): |
| 407 | def m(self): pass |
| 408 | |
| 409 | attrs = attrs_wo_objs(B) |
| 410 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 411 | self.assert_(('c', 'class method', A) in attrs, 'missing class method') |
| 412 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 413 | self.assert_(('m', 'method', B) in attrs, 'missing plain method') |
| 414 | self.assert_(('m1', 'method', A) in attrs, 'missing plain method') |
| 415 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 416 | |
| 417 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 418 | class C(A): |
| 419 | def m(self): pass |
| 420 | def c(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(C) |
| 423 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 424 | self.assert_(('c', 'method', C) in attrs, 'missing plain method') |
| 425 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 426 | self.assert_(('m', 'method', C) in attrs, 'missing plain method') |
| 427 | self.assert_(('m1', 'method', A) 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 | class D(B, C): |
| 431 | def m1(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 432 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 433 | attrs = attrs_wo_objs(D) |
| 434 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 435 | self.assert_(('c', 'class method', A) in attrs, 'missing class method') |
| 436 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 437 | self.assert_(('m', 'method', B) in attrs, 'missing plain method') |
| 438 | self.assert_(('m1', 'method', D) in attrs, 'missing plain method') |
| 439 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 440 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 441 | # Repeat all that, but w/ new-style classes. |
| 442 | def test_classify_newstyle(self): |
| 443 | class A(object): |
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 s(): pass |
| 446 | s = staticmethod(s) |
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 | def c(cls): pass |
| 449 | c = classmethod(c) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 450 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 451 | def getp(self): pass |
| 452 | p = property(getp) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 453 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 454 | def m(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 455 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 456 | def m1(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 457 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 458 | datablob = '1' |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 459 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 460 | attrs = attrs_wo_objs(A) |
| 461 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 462 | self.assert_(('c', 'class method', A) in attrs, 'missing class method') |
| 463 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 464 | self.assert_(('m', 'method', A) in attrs, 'missing plain method') |
| 465 | self.assert_(('m1', 'method', A) in attrs, 'missing plain method') |
| 466 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 467 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 468 | class B(A): |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 469 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 470 | def m(self): pass |
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 | attrs = attrs_wo_objs(B) |
| 473 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 474 | self.assert_(('c', 'class method', A) in attrs, 'missing class method') |
| 475 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 476 | self.assert_(('m', 'method', B) in attrs, 'missing plain method') |
| 477 | self.assert_(('m1', 'method', A) in attrs, 'missing plain method') |
| 478 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 479 | |
| 480 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 481 | class C(A): |
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 | def m(self): pass |
| 484 | def c(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 485 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 486 | attrs = attrs_wo_objs(C) |
| 487 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 488 | self.assert_(('c', 'method', C) in attrs, 'missing plain method') |
| 489 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 490 | self.assert_(('m', 'method', C) in attrs, 'missing plain method') |
| 491 | self.assert_(('m1', 'method', A) in attrs, 'missing plain method') |
| 492 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 493 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 494 | class D(B, C): |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 495 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 496 | def m1(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 497 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 498 | attrs = attrs_wo_objs(D) |
| 499 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 500 | self.assert_(('c', 'method', C) in attrs, 'missing plain method') |
| 501 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 502 | self.assert_(('m', 'method', B) in attrs, 'missing plain method') |
| 503 | self.assert_(('m1', 'method', D) in attrs, 'missing plain method') |
| 504 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Jeremy Hylton | c4bf5ed | 2003-06-27 18:43:12 +0000 | [diff] [blame] | 505 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 506 | def test_main(): |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 507 | run_unittest(TestDecorators, TestRetrievingSourceCode, TestOneliners, |
Johannes Gijsbers | a5855d5 | 2005-03-12 16:37:11 +0000 | [diff] [blame] | 508 | TestBuggyCases, |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 509 | TestInterpreterStack, TestClassesAndFunctions, TestPredicates) |
Martin v. Löwis | 893ffa4 | 2003-10-31 15:35:53 +0000 | [diff] [blame] | 510 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 511 | if __name__ == "__main__": |
| 512 | test_main() |