blob: b653f4020042a8777aa9a49499225d616c4d14f0 [file] [log] [blame]
Johannes Gijsberscb9015d2004-12-12 16:20:22 +00001import sys
Barry Warsaw00decd72006-07-27 23:43:15 +00002import types
Johannes Gijsberscb9015d2004-12-12 16:20:22 +00003import unittest
4import inspect
Barry Warsaw00decd72006-07-27 23:43:15 +00005import datetime
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00006
Johannes Gijsberscb9015d2004-12-12 16:20:22 +00007from test.test_support import TESTFN, run_unittest
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00008
Johannes Gijsberscb9015d2004-12-12 16:20:22 +00009from test import inspect_fodder as mod
10from test import inspect_fodder2 as mod2
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000011
12# Functions tested in this suite:
13# ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode,
Facundo Batista759bfc62008-02-18 03:43:43 +000014# 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 Yee6397c7c2001-02-27 14:43:21 +000018
Nick Coghlana2053472008-12-14 10:54:50 +000019# NOTE: There are some additional tests relating to interaction with
20# zipimport in the test_zipimport_support test module.
21
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000022modfile = mod.__file__
Georg Brandlb2afe852006-06-09 20:43:48 +000023if modfile.endswith(('c', 'o')):
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000024 modfile = modfile[:-1]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000025
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000026import __builtin__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000027
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000028try:
29 1/0
30except:
31 tb = sys.exc_traceback
32
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000033git = mod.StupidGit()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000034
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000035class IsTestBase(unittest.TestCase):
36 predicates = set([inspect.isbuiltin, inspect.isclass, inspect.iscode,
37 inspect.isframe, inspect.isfunction, inspect.ismethod,
Facundo Batista759bfc62008-02-18 03:43:43 +000038 inspect.ismodule, inspect.istraceback,
39 inspect.isgenerator, inspect.isgeneratorfunction])
Tim Peters5a9fb3c2005-01-07 16:01:32 +000040
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000041 def istest(self, predicate, exp):
42 obj = eval(exp)
43 self.failUnless(predicate(obj), '%s(%s)' % (predicate.__name__, exp))
Tim Peters5a9fb3c2005-01-07 16:01:32 +000044
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000045 for other in self.predicates - set([predicate]):
Facundo Batista759bfc62008-02-18 03:43:43 +000046 if predicate == inspect.isgeneratorfunction and\
47 other == inspect.isfunction:
48 continue
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000049 self.failIf(other(obj), 'not %s(%s)' % (other.__name__, exp))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000050
Facundo Batista759bfc62008-02-18 03:43:43 +000051def generator_function_example(self):
52 for i in xrange(2):
53 yield i
54
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000055class TestPredicates(IsTestBase):
Christian Heimes728bee82008-03-03 20:30:29 +000056 def test_sixteen(self):
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000057 count = len(filter(lambda x:x.startswith('is'), dir(inspect)))
Facundo Batista759bfc62008-02-18 03:43:43 +000058 # This test is here for remember you to update Doc/library/inspect.rst
Georg Brandl26bc1772008-03-03 20:39:00 +000059 # which claims there are 16 such functions
Christian Heimes728bee82008-03-03 20:30:29 +000060 expected = 16
Neal Norwitzdf80af72006-07-28 04:22:34 +000061 err_msg = "There are %d (not %d) is* functions" % (count, expected)
62 self.assertEqual(count, expected, err_msg)
Tim Peters5a9fb3c2005-01-07 16:01:32 +000063
Facundo Batista759bfc62008-02-18 03:43:43 +000064
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000065 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 Batista759bfc62008-02-18 03:43:43 +000078 self.istest(inspect.isgenerator, '(x for x in xrange(2))')
79 self.istest(inspect.isgeneratorfunction, 'generator_function_example')
Barry Warsaw00decd72006-07-27 23:43:15 +000080 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 Yee6397c7c2001-02-27 14:43:21 +000089
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000090 def test_isroutine(self):
91 self.assert_(inspect.isroutine(mod.spam))
92 self.assert_(inspect.isroutine([].count))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000093
Amaury Forgeot d'Arcb54447f2009-01-13 23:39:22 +000094 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 Gijsberscb9015d2004-12-12 16:20:22 +0000105class TestInterpreterStack(IsTestBase):
106 def __init__(self, *args, **kwargs):
107 unittest.TestCase.__init__(self, *args, **kwargs)
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000108
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000109 git.abuse(7, 8, 9)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000110
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000111 def test_abuse_done(self):
112 self.istest(inspect.istraceback, 'git.ex[2]')
113 self.istest(inspect.isframe, 'mod.fr')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000114
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000115 def test_stack(self):
116 self.assert_(len(mod.st) >= 5)
117 self.assertEqual(mod.st[0][1:],
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000118 (modfile, 16, 'eggs', [' st = inspect.stack()\n'], 0))
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000119 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 Yee6397c7c2001-02-27 14:43:21 +0000125
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000126 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 Yee6397c7c2001-02-27 14:43:21 +0000134
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000135 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 Yee6397c7c2001-02-27 14:43:21 +0000143
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000144 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 Yee6397c7c2001-02-27 14:43:21 +0000151
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000152class GetSourceBase(unittest.TestCase):
153 # Subclasses must override.
154 fodderFile = None
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000155
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000156 def __init__(self, *args, **kwargs):
157 unittest.TestCase.__init__(self, *args, **kwargs)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000158
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000159 self.source = file(inspect.getsourcefile(self.fodderFile)).read()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000160
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000161 def sourcerange(self, top, bottom):
162 lines = self.source.split("\n")
163 return "\n".join(lines[top-1:bottom]) + "\n"
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000164
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000165 def assertSourceEqual(self, obj, top, bottom):
166 self.assertEqual(inspect.getsource(obj),
167 self.sourcerange(top, bottom))
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000168
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000169class TestRetrievingSourceCode(GetSourceBase):
170 fodderFile = mod
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000171
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000172 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 Peters5a9fb3c2005-01-07 16:01:32 +0000189
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000190 def test_getfunctions(self):
191 functions = inspect.getmembers(mod, inspect.isfunction)
192 self.assertEqual(functions, [('eggs', mod.eggs),
193 ('spam', mod.spam)])
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000194
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000195 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 Gijsbersc473c992004-08-18 12:40:31 +0000201
Georg Brandl7be19aa2008-06-07 15:59:10 +0000202 def test_cleandoc(self):
203 self.assertEqual(inspect.cleandoc('An\n indented\n docstring.'),
204 'An\nindented\ndocstring.')
205
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000206 def test_getcomments(self):
207 self.assertEqual(inspect.getcomments(mod), '# line 1\n')
208 self.assertEqual(inspect.getcomments(mod.StupidGit), '# line 20\n')
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000209
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000210 def test_getmodule(self):
Nick Coghlanc495c662006-09-07 10:50:34 +0000211 # Check actual module
212 self.assertEqual(inspect.getmodule(mod), mod)
213 # Check class (uses __module__ attribute)
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000214 self.assertEqual(inspect.getmodule(mod.StupidGit), mod)
Nick Coghlanc495c662006-09-07 10:50:34 +0000215 # 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 Gijsbersc473c992004-08-18 12:40:31 +0000223
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000224 def test_getsource(self):
225 self.assertSourceEqual(git.abuse, 29, 39)
226 self.assertSourceEqual(mod.StupidGit, 21, 46)
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000227
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000228 def test_getsourcefile(self):
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000229 self.assertEqual(inspect.getsourcefile(mod.spam), modfile)
230 self.assertEqual(inspect.getsourcefile(git.abuse), modfile)
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000231
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000232 def test_getfile(self):
233 self.assertEqual(inspect.getfile(mod.StupidGit), mod.__file__)
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000234
Phillip J. Eby5d86bdb2006-07-10 19:03:29 +0000235 def test_getmodule_recursion(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000236 from types import ModuleType
Phillip J. Eby5d86bdb2006-07-10 19:03:29 +0000237 name = '__inspect_dummy'
Christian Heimesc756d002007-11-27 21:34:01 +0000238 m = sys.modules[name] = ModuleType(name)
Tim Peters722b8832006-07-10 21:11:49 +0000239 m.__file__ = "<string>" # hopefully not a real filename...
Phillip J. Eby5d86bdb2006-07-10 19:03:29 +0000240 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. Eby1a2959c2006-07-20 15:54:16 +0000244 inspect.getmodule(compile('a=10','','single'))
Phillip J. Eby5d86bdb2006-07-10 19:03:29 +0000245
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000246class TestDecorators(GetSourceBase):
247 fodderFile = mod2
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000248
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000249 def test_wrapped_decorator(self):
250 self.assertSourceEqual(mod2.wrapped, 14, 17)
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000251
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000252 def test_replacing_decorator(self):
253 self.assertSourceEqual(mod2.gone, 9, 10)
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000254
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000255class 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 Peters5a9fb3c2005-01-07 16:01:32 +0000260
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000261 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 Peters5a9fb3c2005-01-07 16:01:32 +0000264 self.assertSourceEqual(mod2.tll, 28, 30)
265
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000266 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 Peters5a9fb3c2005-01-07 16:01:32 +0000270
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000271 def test_onelinefunc(self):
272 # Test inspect.getsource with a regular one-line function.
273 self.assertSourceEqual(mod2.onelinefunc, 37, 37)
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000274
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000275 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 Peters5a9fb3c2005-01-07 16:01:32 +0000280
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000281 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 Peters5a9fb3c2005-01-07 16:01:32 +0000286
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000287 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 Peters5a9fb3c2005-01-07 16:01:32 +0000291
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000292 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 Gijsbersa5855d52005-03-12 16:37:11 +0000297class 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 Rigodd5c0232005-09-25 11:45:45 +0000306 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 Brandl2463f8f2006-08-14 21:34:08 +0000318 def test_method_in_dynamic_class(self):
319 self.assertSourceEqual(mod2.method_in_dynamic_class, 95, 97)
320
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000321# Helper for testing classify_class_attrs.
Tim Peters13b49d32001-09-23 02:00:29 +0000322def attrs_wo_objs(cls):
323 return [t[:3] for t in inspect.classify_class_attrs(cls)]
324
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000325class TestClassesAndFunctions(unittest.TestCase):
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000326 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 Peters13b49d32001-09-23 02:00:29 +0000332
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000333 expected = (D, B, A, C)
334 got = inspect.getmro(D)
335 self.assertEqual(expected, got)
Tim Peters13b49d32001-09-23 02:00:29 +0000336
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000337 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 Peters13b49d32001-09-23 02:00:29 +0000343
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000344 expected = (D, B, C, A, object)
345 got = inspect.getmro(D)
346 self.assertEqual(expected, got)
Tim Peters13b49d32001-09-23 02:00:29 +0000347
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000348 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 Peters13b49d32001-09-23 02:00:29 +0000359
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000360 def test_getargspec(self):
361 self.assertArgSpecEquals(mod.eggs, ['x', 'y'], formatted = '(x, y)')
Tim Peters13b49d32001-09-23 02:00:29 +0000362
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000363 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 Peters13b49d32001-09-23 02:00:29 +0000367
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000368 def test_getargspec_method(self):
369 class A(object):
370 def m(self):
371 pass
372 self.assertArgSpecEquals(A.m, ['self'])
Tim Peters13b49d32001-09-23 02:00:29 +0000373
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000374 def test_getargspec_sublistofone(self):
Neal Norwitz33b730e2006-03-27 08:58:23 +0000375 def sublistOfOne((foo,)): return 1
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000376 self.assertArgSpecEquals(sublistOfOne, [['foo']])
377
Neal Norwitz33b730e2006-03-27 08:58:23 +0000378 def fakeSublistOfOne((foo)): return 1
379 self.assertArgSpecEquals(fakeSublistOfOne, ['foo'])
380
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000381 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 Peters13b49d32001-09-23 02:00:29 +0000416
417
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000418 class C(A):
419 def m(self): pass
420 def c(self): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000421
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000422 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 Peters13b49d32001-09-23 02:00:29 +0000429
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000430 class D(B, C):
431 def m1(self): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000432
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000433 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 Peters13b49d32001-09-23 02:00:29 +0000440
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000441 # Repeat all that, but w/ new-style classes.
442 def test_classify_newstyle(self):
443 class A(object):
Tim Peters13b49d32001-09-23 02:00:29 +0000444
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000445 def s(): pass
446 s = staticmethod(s)
Tim Peters13b49d32001-09-23 02:00:29 +0000447
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000448 def c(cls): pass
449 c = classmethod(c)
Tim Peters13b49d32001-09-23 02:00:29 +0000450
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000451 def getp(self): pass
452 p = property(getp)
Tim Peters13b49d32001-09-23 02:00:29 +0000453
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000454 def m(self): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000455
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000456 def m1(self): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000457
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000458 datablob = '1'
Tim Peters13b49d32001-09-23 02:00:29 +0000459
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000460 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 Peters13b49d32001-09-23 02:00:29 +0000467
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000468 class B(A):
Tim Peters13b49d32001-09-23 02:00:29 +0000469
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000470 def m(self): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000471
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000472 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 Peters13b49d32001-09-23 02:00:29 +0000479
480
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000481 class C(A):
Tim Peters13b49d32001-09-23 02:00:29 +0000482
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000483 def m(self): pass
484 def c(self): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000485
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000486 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 Peters13b49d32001-09-23 02:00:29 +0000493
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000494 class D(B, C):
Tim Peters13b49d32001-09-23 02:00:29 +0000495
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000496 def m1(self): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000497
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000498 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 Hyltonc4bf5ed2003-06-27 18:43:12 +0000505
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000506def test_main():
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000507 run_unittest(TestDecorators, TestRetrievingSourceCode, TestOneliners,
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000508 TestBuggyCases,
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000509 TestInterpreterStack, TestClassesAndFunctions, TestPredicates)
Martin v. Löwis893ffa42003-10-31 15:35:53 +0000510
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000511if __name__ == "__main__":
512 test_main()