blob: c62fa552d8caf7a2365bfd90e595a632426a233d [file] [log] [blame]
Johannes Gijsberscb9015d2004-12-12 16:20:22 +00001import sys
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002import types
Johannes Gijsberscb9015d2004-12-12 16:20:22 +00003import unittest
4import inspect
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005import datetime
Guido van Rossum813b0e52007-05-21 18:11:34 +00006import collections
Christian Heimesa3538eb2007-11-06 11:44:48 +00007from os.path import normcase
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00008
Johannes Gijsberscb9015d2004-12-12 16:20:22 +00009from test.test_support import TESTFN, run_unittest
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000010
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000011from test import inspect_fodder as mod
12from test import inspect_fodder2 as mod2
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000013
14# Functions tested in this suite:
15# ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode,
Christian Heimes7131fd92008-02-19 14:21:46 +000016# 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 Yee6397c7c2001-02-27 14:43:21 +000020
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000021modfile = mod.__file__
Thomas Wouters0e3f5912006-08-11 14:57:12 +000022if modfile.endswith(('c', 'o')):
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000023 modfile = modfile[:-1]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000024
Christian Heimesa3538eb2007-11-06 11:44:48 +000025# Normalize file names: on Windows, the case of file names of compiled
26# modules depends on the path used to start the python executable.
27modfile = normcase(modfile)
28
29def revise(filename, *args):
30 return (normcase(filename),) + args
31
Georg Brandl1a3284e2007-12-02 09:40:06 +000032import builtins
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000033
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000034try:
35 1/0
36except:
Guido van Rossume7ba4952007-06-06 23:52:48 +000037 tb = sys.exc_info()[2]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000038
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000039git = mod.StupidGit()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000040
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000041class IsTestBase(unittest.TestCase):
42 predicates = set([inspect.isbuiltin, inspect.isclass, inspect.iscode,
43 inspect.isframe, inspect.isfunction, inspect.ismethod,
Christian Heimes7131fd92008-02-19 14:21:46 +000044 inspect.ismodule, inspect.istraceback,
45 inspect.isgenerator, inspect.isgeneratorfunction])
Tim Peters5a9fb3c2005-01-07 16:01:32 +000046
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000047 def istest(self, predicate, exp):
48 obj = eval(exp)
49 self.failUnless(predicate(obj), '%s(%s)' % (predicate.__name__, exp))
Tim Peters5a9fb3c2005-01-07 16:01:32 +000050
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000051 for other in self.predicates - set([predicate]):
Christian Heimes7131fd92008-02-19 14:21:46 +000052 if predicate == inspect.isgeneratorfunction and\
53 other == inspect.isfunction:
54 continue
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000055 self.failIf(other(obj), 'not %s(%s)' % (other.__name__, exp))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000056
Christian Heimes7131fd92008-02-19 14:21:46 +000057def generator_function_example(self):
58 for i in range(2):
59 yield i
60
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000061class TestPredicates(IsTestBase):
Christian Heimes227c8002008-03-03 20:34:40 +000062 def test_sixteen(self):
Guido van Rossumc1f779c2007-07-03 08:25:58 +000063 count = len([x for x in dir(inspect) if x.startswith('is')])
Christian Heimes7131fd92008-02-19 14:21:46 +000064 # This test is here for remember you to update Doc/library/inspect.rst
Christian Heimes78644762008-03-04 23:39:23 +000065 # which claims there are 16 such functions
Christian Heimes227c8002008-03-03 20:34:40 +000066 expected = 16
Thomas Wouters0e3f5912006-08-11 14:57:12 +000067 err_msg = "There are %d (not %d) is* functions" % (count, expected)
68 self.assertEqual(count, expected, err_msg)
Tim Peters5a9fb3c2005-01-07 16:01:32 +000069
Christian Heimes7131fd92008-02-19 14:21:46 +000070
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000071 def test_excluding_predicates(self):
72 self.istest(inspect.isbuiltin, 'sys.exit')
73 self.istest(inspect.isbuiltin, '[].append')
74 self.istest(inspect.isclass, 'mod.StupidGit')
Neal Norwitz221085d2007-02-25 20:55:47 +000075 self.istest(inspect.iscode, 'mod.spam.__code__')
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000076 self.istest(inspect.isframe, 'tb.tb_frame')
77 self.istest(inspect.isfunction, 'mod.spam')
Christian Heimes4a22b5d2007-11-25 09:39:14 +000078 self.istest(inspect.isfunction, 'mod.StupidGit.abuse')
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000079 self.istest(inspect.ismethod, 'git.argue')
80 self.istest(inspect.ismodule, 'mod')
81 self.istest(inspect.istraceback, 'tb')
Guido van Rossum813b0e52007-05-21 18:11:34 +000082 self.istest(inspect.isdatadescriptor, 'collections.defaultdict.default_factory')
Christian Heimes7131fd92008-02-19 14:21:46 +000083 self.istest(inspect.isgenerator, '(x for x in range(2))')
84 self.istest(inspect.isgeneratorfunction, 'generator_function_example')
Thomas Wouters0e3f5912006-08-11 14:57:12 +000085 if hasattr(types, 'GetSetDescriptorType'):
86 self.istest(inspect.isgetsetdescriptor,
87 'type(tb.tb_frame).f_locals')
88 else:
89 self.failIf(inspect.isgetsetdescriptor(type(tb.tb_frame).f_locals))
90 if hasattr(types, 'MemberDescriptorType'):
91 self.istest(inspect.ismemberdescriptor, 'datetime.timedelta.days')
92 else:
93 self.failIf(inspect.ismemberdescriptor(datetime.timedelta.days))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000094
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000095 def test_isroutine(self):
96 self.assert_(inspect.isroutine(mod.spam))
97 self.assert_(inspect.isroutine([].count))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000098
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000099class TestInterpreterStack(IsTestBase):
100 def __init__(self, *args, **kwargs):
101 unittest.TestCase.__init__(self, *args, **kwargs)
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000102
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000103 git.abuse(7, 8, 9)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000104
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000105 def test_abuse_done(self):
106 self.istest(inspect.istraceback, 'git.ex[2]')
107 self.istest(inspect.isframe, 'mod.fr')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000108
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000109 def test_stack(self):
110 self.assert_(len(mod.st) >= 5)
Christian Heimesa3538eb2007-11-06 11:44:48 +0000111 self.assertEqual(revise(*mod.st[0][1:]),
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000112 (modfile, 16, 'eggs', [' st = inspect.stack()\n'], 0))
Christian Heimesa3538eb2007-11-06 11:44:48 +0000113 self.assertEqual(revise(*mod.st[1][1:]),
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000114 (modfile, 9, 'spam', [' eggs(b + d, c + f)\n'], 0))
Christian Heimesa3538eb2007-11-06 11:44:48 +0000115 self.assertEqual(revise(*mod.st[2][1:]),
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000116 (modfile, 43, 'argue', [' spam(a, b, c)\n'], 0))
Christian Heimesa3538eb2007-11-06 11:44:48 +0000117 self.assertEqual(revise(*mod.st[3][1:]),
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000118 (modfile, 39, 'abuse', [' self.argue(a, b, c)\n'], 0))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000119
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000120 def test_trace(self):
121 self.assertEqual(len(git.tr), 3)
Christian Heimesa3538eb2007-11-06 11:44:48 +0000122 self.assertEqual(revise(*git.tr[0][1:]),
123 (modfile, 43, 'argue', [' spam(a, b, c)\n'], 0))
124 self.assertEqual(revise(*git.tr[1][1:]),
125 (modfile, 9, 'spam', [' eggs(b + d, c + f)\n'], 0))
126 self.assertEqual(revise(*git.tr[2][1:]),
127 (modfile, 18, 'eggs', [' q = y / 0\n'], 0))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000128
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000129 def test_frame(self):
130 args, varargs, varkw, locals = inspect.getargvalues(mod.fr)
131 self.assertEqual(args, ['x', 'y'])
132 self.assertEqual(varargs, None)
133 self.assertEqual(varkw, None)
134 self.assertEqual(locals, {'x': 11, 'p': 11, 'y': 14})
135 self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals),
136 '(x=11, y=14)')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000137
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000138 def test_previous_frame(self):
139 args, varargs, varkw, locals = inspect.getargvalues(mod.fr.f_back)
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000140 self.assertEqual(args, ['a', 'b', 'c', 'd', 'e', 'f'])
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000141 self.assertEqual(varargs, 'g')
142 self.assertEqual(varkw, 'h')
143 self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals),
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000144 '(a=7, b=8, c=9, d=3, e=4, f=5, *g=(), **h={})')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000145
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000146class GetSourceBase(unittest.TestCase):
147 # Subclasses must override.
148 fodderFile = None
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000149
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000150 def __init__(self, *args, **kwargs):
151 unittest.TestCase.__init__(self, *args, **kwargs)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000152
Alex Martelli01c77c62006-08-24 02:58:11 +0000153 self.source = open(inspect.getsourcefile(self.fodderFile)).read()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000154
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000155 def sourcerange(self, top, bottom):
156 lines = self.source.split("\n")
157 return "\n".join(lines[top-1:bottom]) + "\n"
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000158
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000159 def assertSourceEqual(self, obj, top, bottom):
160 self.assertEqual(inspect.getsource(obj),
161 self.sourcerange(top, bottom))
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000162
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000163class TestRetrievingSourceCode(GetSourceBase):
164 fodderFile = mod
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000165
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000166 def test_getclasses(self):
167 classes = inspect.getmembers(mod, inspect.isclass)
168 self.assertEqual(classes,
169 [('FesteringGob', mod.FesteringGob),
170 ('MalodorousPervert', mod.MalodorousPervert),
171 ('ParrotDroppings', mod.ParrotDroppings),
172 ('StupidGit', mod.StupidGit)])
173 tree = inspect.getclasstree([cls[1] for cls in classes], 1)
174 self.assertEqual(tree,
Thomas Wouters725af872006-04-15 09:13:19 +0000175 [(object, ()),
176 [(mod.ParrotDroppings, (object,)),
177 (mod.StupidGit, (object,)),
178 [(mod.MalodorousPervert, (mod.StupidGit,)),
179 [(mod.FesteringGob, (mod.MalodorousPervert,
180 mod.ParrotDroppings))
181 ]
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000182 ]
183 ]
184 ])
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000185
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000186 def test_getfunctions(self):
187 functions = inspect.getmembers(mod, inspect.isfunction)
188 self.assertEqual(functions, [('eggs', mod.eggs),
189 ('spam', mod.spam)])
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000190
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000191 def test_getdoc(self):
192 self.assertEqual(inspect.getdoc(mod), 'A module docstring.')
193 self.assertEqual(inspect.getdoc(mod.StupidGit),
194 'A longer,\n\nindented\n\ndocstring.')
195 self.assertEqual(inspect.getdoc(git.abuse),
196 'Another\n\ndocstring\n\ncontaining\n\ntabs')
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000197
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000198 def test_getcomments(self):
199 self.assertEqual(inspect.getcomments(mod), '# line 1\n')
200 self.assertEqual(inspect.getcomments(mod.StupidGit), '# line 20\n')
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000201
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000202 def test_getmodule(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000203 # Check actual module
204 self.assertEqual(inspect.getmodule(mod), mod)
205 # Check class (uses __module__ attribute)
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000206 self.assertEqual(inspect.getmodule(mod.StupidGit), mod)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000207 # Check a method (no __module__ attribute, falls back to filename)
208 self.assertEqual(inspect.getmodule(mod.StupidGit.abuse), mod)
209 # Do it again (check the caching isn't broken)
210 self.assertEqual(inspect.getmodule(mod.StupidGit.abuse), mod)
211 # Check a builtin
Georg Brandl1a3284e2007-12-02 09:40:06 +0000212 self.assertEqual(inspect.getmodule(str), sys.modules["builtins"])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000213 # Check filename override
214 self.assertEqual(inspect.getmodule(None, modfile), mod)
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000215
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000216 def test_getsource(self):
217 self.assertSourceEqual(git.abuse, 29, 39)
218 self.assertSourceEqual(mod.StupidGit, 21, 46)
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000219
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000220 def test_getsourcefile(self):
Christian Heimesa3538eb2007-11-06 11:44:48 +0000221 self.assertEqual(normcase(inspect.getsourcefile(mod.spam)), modfile)
222 self.assertEqual(normcase(inspect.getsourcefile(git.abuse)), modfile)
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000223
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000224 def test_getfile(self):
225 self.assertEqual(inspect.getfile(mod.StupidGit), mod.__file__)
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000226
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000227 def test_getmodule_recursion(self):
Christian Heimes45f9af32007-11-27 21:50:00 +0000228 from types import ModuleType
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000229 name = '__inspect_dummy'
Christian Heimes45f9af32007-11-27 21:50:00 +0000230 m = sys.modules[name] = ModuleType(name)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000231 m.__file__ = "<string>" # hopefully not a real filename...
232 m.__loader__ = "dummy" # pretend the filename is understood by a loader
Georg Brandl7cae87c2006-09-06 06:51:57 +0000233 exec("def x(): pass", m.__dict__)
Neal Norwitz221085d2007-02-25 20:55:47 +0000234 self.assertEqual(inspect.getsourcefile(m.x.__code__), '<string>')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000235 del sys.modules[name]
236 inspect.getmodule(compile('a=10','','single'))
237
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000238class TestDecorators(GetSourceBase):
239 fodderFile = mod2
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000240
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000241 def test_wrapped_decorator(self):
Christian Heimes09aaa882008-02-23 15:01:06 +0000242 self.assertSourceEqual(mod2.wrapped, 14, 17)
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000243
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000244 def test_replacing_decorator(self):
245 self.assertSourceEqual(mod2.gone, 9, 10)
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000246
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000247class TestOneliners(GetSourceBase):
248 fodderFile = mod2
249 def test_oneline_lambda(self):
250 # Test inspect.getsource with a one-line lambda function.
251 self.assertSourceEqual(mod2.oll, 25, 25)
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000252
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000253 def test_threeline_lambda(self):
254 # Test inspect.getsource with a three-line lambda function,
255 # where the second and third lines are _not_ indented.
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000256 self.assertSourceEqual(mod2.tll, 28, 30)
257
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000258 def test_twoline_indented_lambda(self):
259 # Test inspect.getsource with a two-line lambda function,
260 # where the second line _is_ indented.
261 self.assertSourceEqual(mod2.tlli, 33, 34)
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000262
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000263 def test_onelinefunc(self):
264 # Test inspect.getsource with a regular one-line function.
265 self.assertSourceEqual(mod2.onelinefunc, 37, 37)
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000266
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000267 def test_manyargs(self):
268 # Test inspect.getsource with a regular function where
269 # the arguments are on two lines and _not_ indented and
270 # the body on the second line with the last arguments.
271 self.assertSourceEqual(mod2.manyargs, 40, 41)
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000272
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000273 def test_twolinefunc(self):
274 # Test inspect.getsource with a regular function where
275 # the body is on two lines, following the argument list and
276 # continued on the next line by a \\.
277 self.assertSourceEqual(mod2.twolinefunc, 44, 45)
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000278
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000279 def test_lambda_in_list(self):
280 # Test inspect.getsource with a one-line lambda function
281 # defined in a list, indented.
282 self.assertSourceEqual(mod2.a[1], 49, 49)
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000283
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000284 def test_anonymous(self):
285 # Test inspect.getsource with a lambda function defined
286 # as argument to another function.
287 self.assertSourceEqual(mod2.anonymous, 55, 55)
288
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000289class TestBuggyCases(GetSourceBase):
290 fodderFile = mod2
291
292 def test_with_comment(self):
293 self.assertSourceEqual(mod2.with_comment, 58, 59)
294
295 def test_multiline_sig(self):
296 self.assertSourceEqual(mod2.multiline_sig[0], 63, 64)
297
Armin Rigodd5c0232005-09-25 11:45:45 +0000298 def test_nested_class(self):
299 self.assertSourceEqual(mod2.func69().func71, 71, 72)
300
301 def test_one_liner_followed_by_non_name(self):
302 self.assertSourceEqual(mod2.func77, 77, 77)
303
304 def test_one_liner_dedent_non_name(self):
305 self.assertSourceEqual(mod2.cls82.func83, 83, 83)
306
307 def test_with_comment_instead_of_docstring(self):
308 self.assertSourceEqual(mod2.func88, 88, 90)
309
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000310 def test_method_in_dynamic_class(self):
311 self.assertSourceEqual(mod2.method_in_dynamic_class, 95, 97)
312
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000313# Helper for testing classify_class_attrs.
Tim Peters13b49d32001-09-23 02:00:29 +0000314def attrs_wo_objs(cls):
315 return [t[:3] for t in inspect.classify_class_attrs(cls)]
316
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000317class TestClassesAndFunctions(unittest.TestCase):
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000318 def test_newstyle_mro(self):
319 # The same w/ new-class MRO.
320 class A(object): pass
321 class B(A): pass
322 class C(A): pass
323 class D(B, C): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000324
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000325 expected = (D, B, C, A, object)
326 got = inspect.getmro(D)
327 self.assertEqual(expected, got)
Tim Peters13b49d32001-09-23 02:00:29 +0000328
Christian Heimes3795b532007-11-08 13:48:53 +0000329 def assertArgSpecEquals(self, routine, args_e, varargs_e=None,
330 varkw_e=None, defaults_e=None, formatted=None):
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000331 args, varargs, varkw, defaults = inspect.getargspec(routine)
332 self.assertEqual(args, args_e)
333 self.assertEqual(varargs, varargs_e)
334 self.assertEqual(varkw, varkw_e)
335 self.assertEqual(defaults, defaults_e)
336 if formatted is not None:
337 self.assertEqual(inspect.formatargspec(args, varargs, varkw, defaults),
338 formatted)
Tim Peters13b49d32001-09-23 02:00:29 +0000339
Christian Heimes3795b532007-11-08 13:48:53 +0000340 def assertFullArgSpecEquals(self, routine, args_e, varargs_e=None,
341 varkw_e=None, defaults_e=None,
342 kwonlyargs_e=[], kwonlydefaults_e=None,
343 ann_e={}, formatted=None):
344 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = \
345 inspect.getfullargspec(routine)
346 self.assertEqual(args, args_e)
347 self.assertEqual(varargs, varargs_e)
348 self.assertEqual(varkw, varkw_e)
349 self.assertEqual(defaults, defaults_e)
350 self.assertEqual(kwonlyargs, kwonlyargs_e)
351 self.assertEqual(kwonlydefaults, kwonlydefaults_e)
352 self.assertEqual(ann, ann_e)
353 if formatted is not None:
354 self.assertEqual(inspect.formatargspec(args, varargs, varkw, defaults,
355 kwonlyargs, kwonlydefaults, ann),
356 formatted)
357
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000358 def test_getargspec(self):
Christian Heimes3795b532007-11-08 13:48:53 +0000359 self.assertArgSpecEquals(mod.eggs, ['x', 'y'], formatted='(x, y)')
Tim Peters13b49d32001-09-23 02:00:29 +0000360
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000361 self.assertArgSpecEquals(mod.spam,
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000362 ['a', 'b', 'c', 'd', 'e', 'f'],
363 'g', 'h', (3, 4, 5),
364 '(a, b, c, d=3, e=4, f=5, *g, **h)')
Tim Peters13b49d32001-09-23 02:00:29 +0000365
Christian Heimes3795b532007-11-08 13:48:53 +0000366 self.assertRaises(ValueError, self.assertArgSpecEquals,
367 mod2.keyworded, [])
368
369 self.assertRaises(ValueError, self.assertArgSpecEquals,
370 mod2.annotated, [])
371
372 def test_getfullargspec(self):
373 self.assertFullArgSpecEquals(mod2.keyworded, [], varargs_e='arg1',
374 kwonlyargs_e=['arg2'],
375 kwonlydefaults_e={'arg2':1},
376 formatted='(*arg1, arg2=1)')
377
378 self.assertFullArgSpecEquals(mod2.annotated, ['arg1'],
Christian Heimesc9543e42007-11-28 08:28:28 +0000379 ann_e={'arg1' : list},
Christian Heimes3795b532007-11-08 13:48:53 +0000380 formatted='(arg1: list)')
381
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000382 def test_getargspec_method(self):
383 class A(object):
384 def m(self):
385 pass
386 self.assertArgSpecEquals(A.m, ['self'])
Tim Peters13b49d32001-09-23 02:00:29 +0000387
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000388 def test_classify_newstyle(self):
389 class A(object):
Tim Peters13b49d32001-09-23 02:00:29 +0000390
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000391 def s(): pass
392 s = staticmethod(s)
Tim Peters13b49d32001-09-23 02:00:29 +0000393
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000394 def c(cls): pass
395 c = classmethod(c)
Tim Peters13b49d32001-09-23 02:00:29 +0000396
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000397 def getp(self): pass
398 p = property(getp)
Tim Peters13b49d32001-09-23 02:00:29 +0000399
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000400 def m(self): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000401
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000402 def m1(self): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000403
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000404 datablob = '1'
Tim Peters13b49d32001-09-23 02:00:29 +0000405
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000406 attrs = attrs_wo_objs(A)
407 self.assert_(('s', 'static method', A) in attrs, 'missing static method')
408 self.assert_(('c', 'class method', A) in attrs, 'missing class method')
409 self.assert_(('p', 'property', A) in attrs, 'missing property')
Christian Heimes4a22b5d2007-11-25 09:39:14 +0000410 self.assert_(('m', 'method', A) in attrs,
411 'missing plain method: %r' % attrs)
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000412 self.assert_(('m1', 'method', A) in attrs, 'missing plain method')
413 self.assert_(('datablob', 'data', A) in attrs, 'missing data')
Tim Peters13b49d32001-09-23 02:00:29 +0000414
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000415 class B(A):
Tim Peters13b49d32001-09-23 02:00:29 +0000416
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000417 def m(self): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000418
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000419 attrs = attrs_wo_objs(B)
420 self.assert_(('s', 'static method', A) in attrs, 'missing static method')
421 self.assert_(('c', 'class method', A) in attrs, 'missing class method')
422 self.assert_(('p', 'property', A) in attrs, 'missing property')
423 self.assert_(('m', 'method', B) in attrs, 'missing plain method')
424 self.assert_(('m1', 'method', A) in attrs, 'missing plain method')
425 self.assert_(('datablob', 'data', A) in attrs, 'missing data')
Tim Peters13b49d32001-09-23 02:00:29 +0000426
427
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000428 class C(A):
Tim Peters13b49d32001-09-23 02:00:29 +0000429
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000430 def m(self): pass
431 def c(self): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000432
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000433 attrs = attrs_wo_objs(C)
434 self.assert_(('s', 'static method', A) in attrs, 'missing static method')
435 self.assert_(('c', 'method', C) in attrs, 'missing plain method')
436 self.assert_(('p', 'property', A) in attrs, 'missing property')
437 self.assert_(('m', 'method', C) in attrs, 'missing plain method')
438 self.assert_(('m1', 'method', A) in attrs, 'missing plain method')
439 self.assert_(('datablob', 'data', A) in attrs, 'missing data')
Tim Peters13b49d32001-09-23 02:00:29 +0000440
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000441 class D(B, C):
Tim Peters13b49d32001-09-23 02:00:29 +0000442
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000443 def m1(self): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000444
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000445 attrs = attrs_wo_objs(D)
446 self.assert_(('s', 'static method', A) in attrs, 'missing static method')
447 self.assert_(('c', 'method', C) in attrs, 'missing plain method')
448 self.assert_(('p', 'property', A) in attrs, 'missing property')
449 self.assert_(('m', 'method', B) in attrs, 'missing plain method')
450 self.assert_(('m1', 'method', D) in attrs, 'missing plain method')
451 self.assert_(('datablob', 'data', A) in attrs, 'missing data')
Jeremy Hyltonc4bf5ed2003-06-27 18:43:12 +0000452
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000453def test_main():
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000454 run_unittest(TestDecorators, TestRetrievingSourceCode, TestOneliners,
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000455 TestBuggyCases,
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000456 TestInterpreterStack, TestClassesAndFunctions, TestPredicates)
Martin v. Löwis893ffa42003-10-31 15:35:53 +0000457
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000458if __name__ == "__main__":
459 test_main()