blob: 06f6b94c851c08f41b14be6833ba610e1092cb0e [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
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000019modfile = mod.__file__
Georg Brandlb2afe852006-06-09 20:43:48 +000020if modfile.endswith(('c', 'o')):
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000021 modfile = modfile[:-1]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000022
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000023import __builtin__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000024
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000025try:
26 1/0
27except:
28 tb = sys.exc_traceback
29
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000030git = mod.StupidGit()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000031
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000032class IsTestBase(unittest.TestCase):
33 predicates = set([inspect.isbuiltin, inspect.isclass, inspect.iscode,
34 inspect.isframe, inspect.isfunction, inspect.ismethod,
Facundo Batista759bfc62008-02-18 03:43:43 +000035 inspect.ismodule, inspect.istraceback,
36 inspect.isgenerator, inspect.isgeneratorfunction])
Tim Peters5a9fb3c2005-01-07 16:01:32 +000037
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000038 def istest(self, predicate, exp):
39 obj = eval(exp)
40 self.failUnless(predicate(obj), '%s(%s)' % (predicate.__name__, exp))
Tim Peters5a9fb3c2005-01-07 16:01:32 +000041
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000042 for other in self.predicates - set([predicate]):
Facundo Batista759bfc62008-02-18 03:43:43 +000043 if predicate == inspect.isgeneratorfunction and\
44 other == inspect.isfunction:
45 continue
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000046 self.failIf(other(obj), 'not %s(%s)' % (other.__name__, exp))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000047
Facundo Batista759bfc62008-02-18 03:43:43 +000048def generator_function_example(self):
49 for i in xrange(2):
50 yield i
51
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000052class TestPredicates(IsTestBase):
Christian Heimes728bee82008-03-03 20:30:29 +000053 def test_sixteen(self):
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000054 count = len(filter(lambda x:x.startswith('is'), dir(inspect)))
Facundo Batista759bfc62008-02-18 03:43:43 +000055 # This test is here for remember you to update Doc/library/inspect.rst
56 # which claims there are 15 such functions
Christian Heimes728bee82008-03-03 20:30:29 +000057 expected = 16
Neal Norwitzdf80af72006-07-28 04:22:34 +000058 err_msg = "There are %d (not %d) is* functions" % (count, expected)
59 self.assertEqual(count, expected, err_msg)
Tim Peters5a9fb3c2005-01-07 16:01:32 +000060
Facundo Batista759bfc62008-02-18 03:43:43 +000061
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000062 def test_excluding_predicates(self):
63 self.istest(inspect.isbuiltin, 'sys.exit')
64 self.istest(inspect.isbuiltin, '[].append')
65 self.istest(inspect.isclass, 'mod.StupidGit')
66 self.istest(inspect.iscode, 'mod.spam.func_code')
67 self.istest(inspect.isframe, 'tb.tb_frame')
68 self.istest(inspect.isfunction, 'mod.spam')
69 self.istest(inspect.ismethod, 'mod.StupidGit.abuse')
70 self.istest(inspect.ismethod, 'git.argue')
71 self.istest(inspect.ismodule, 'mod')
72 self.istest(inspect.istraceback, 'tb')
73 self.istest(inspect.isdatadescriptor, '__builtin__.file.closed')
74 self.istest(inspect.isdatadescriptor, '__builtin__.file.softspace')
Facundo Batista759bfc62008-02-18 03:43:43 +000075 self.istest(inspect.isgenerator, '(x for x in xrange(2))')
76 self.istest(inspect.isgeneratorfunction, 'generator_function_example')
Barry Warsaw00decd72006-07-27 23:43:15 +000077 if hasattr(types, 'GetSetDescriptorType'):
78 self.istest(inspect.isgetsetdescriptor,
79 'type(tb.tb_frame).f_locals')
80 else:
81 self.failIf(inspect.isgetsetdescriptor(type(tb.tb_frame).f_locals))
82 if hasattr(types, 'MemberDescriptorType'):
83 self.istest(inspect.ismemberdescriptor, 'datetime.timedelta.days')
84 else:
85 self.failIf(inspect.ismemberdescriptor(datetime.timedelta.days))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000086
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000087 def test_isroutine(self):
88 self.assert_(inspect.isroutine(mod.spam))
89 self.assert_(inspect.isroutine([].count))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000090
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000091class TestInterpreterStack(IsTestBase):
92 def __init__(self, *args, **kwargs):
93 unittest.TestCase.__init__(self, *args, **kwargs)
Tim Peters5a9fb3c2005-01-07 16:01:32 +000094
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000095 git.abuse(7, 8, 9)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000096
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000097 def test_abuse_done(self):
98 self.istest(inspect.istraceback, 'git.ex[2]')
99 self.istest(inspect.isframe, 'mod.fr')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000100
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000101 def test_stack(self):
102 self.assert_(len(mod.st) >= 5)
103 self.assertEqual(mod.st[0][1:],
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000104 (modfile, 16, 'eggs', [' st = inspect.stack()\n'], 0))
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000105 self.assertEqual(mod.st[1][1:],
106 (modfile, 9, 'spam', [' eggs(b + d, c + f)\n'], 0))
107 self.assertEqual(mod.st[2][1:],
108 (modfile, 43, 'argue', [' spam(a, b, c)\n'], 0))
109 self.assertEqual(mod.st[3][1:],
110 (modfile, 39, 'abuse', [' self.argue(a, b, c)\n'], 0))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000111
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000112 def test_trace(self):
113 self.assertEqual(len(git.tr), 3)
114 self.assertEqual(git.tr[0][1:], (modfile, 43, 'argue',
115 [' spam(a, b, c)\n'], 0))
116 self.assertEqual(git.tr[1][1:], (modfile, 9, 'spam',
117 [' eggs(b + d, c + f)\n'], 0))
118 self.assertEqual(git.tr[2][1:], (modfile, 18, 'eggs',
119 [' q = y / 0\n'], 0))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000120
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000121 def test_frame(self):
122 args, varargs, varkw, locals = inspect.getargvalues(mod.fr)
123 self.assertEqual(args, ['x', 'y'])
124 self.assertEqual(varargs, None)
125 self.assertEqual(varkw, None)
126 self.assertEqual(locals, {'x': 11, 'p': 11, 'y': 14})
127 self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals),
128 '(x=11, y=14)')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000129
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000130 def test_previous_frame(self):
131 args, varargs, varkw, locals = inspect.getargvalues(mod.fr.f_back)
132 self.assertEqual(args, ['a', 'b', 'c', 'd', ['e', ['f']]])
133 self.assertEqual(varargs, 'g')
134 self.assertEqual(varkw, 'h')
135 self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals),
136 '(a=7, b=8, c=9, d=3, (e=4, (f=5,)), *g=(), **h={})')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000137
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000138class GetSourceBase(unittest.TestCase):
139 # Subclasses must override.
140 fodderFile = None
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000141
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000142 def __init__(self, *args, **kwargs):
143 unittest.TestCase.__init__(self, *args, **kwargs)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000144
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000145 self.source = file(inspect.getsourcefile(self.fodderFile)).read()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000146
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000147 def sourcerange(self, top, bottom):
148 lines = self.source.split("\n")
149 return "\n".join(lines[top-1:bottom]) + "\n"
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000150
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000151 def assertSourceEqual(self, obj, top, bottom):
152 self.assertEqual(inspect.getsource(obj),
153 self.sourcerange(top, bottom))
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000154
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000155class TestRetrievingSourceCode(GetSourceBase):
156 fodderFile = mod
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000157
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000158 def test_getclasses(self):
159 classes = inspect.getmembers(mod, inspect.isclass)
160 self.assertEqual(classes,
161 [('FesteringGob', mod.FesteringGob),
162 ('MalodorousPervert', mod.MalodorousPervert),
163 ('ParrotDroppings', mod.ParrotDroppings),
164 ('StupidGit', mod.StupidGit)])
165 tree = inspect.getclasstree([cls[1] for cls in classes], 1)
166 self.assertEqual(tree,
167 [(mod.ParrotDroppings, ()),
168 (mod.StupidGit, ()),
169 [(mod.MalodorousPervert, (mod.StupidGit,)),
170 [(mod.FesteringGob, (mod.MalodorousPervert,
171 mod.ParrotDroppings))
172 ]
173 ]
174 ])
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000175
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000176 def test_getfunctions(self):
177 functions = inspect.getmembers(mod, inspect.isfunction)
178 self.assertEqual(functions, [('eggs', mod.eggs),
179 ('spam', mod.spam)])
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000180
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000181 def test_getdoc(self):
182 self.assertEqual(inspect.getdoc(mod), 'A module docstring.')
183 self.assertEqual(inspect.getdoc(mod.StupidGit),
184 'A longer,\n\nindented\n\ndocstring.')
185 self.assertEqual(inspect.getdoc(git.abuse),
186 'Another\n\ndocstring\n\ncontaining\n\ntabs')
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000187
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000188 def test_getcomments(self):
189 self.assertEqual(inspect.getcomments(mod), '# line 1\n')
190 self.assertEqual(inspect.getcomments(mod.StupidGit), '# line 20\n')
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000191
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000192 def test_getmodule(self):
Nick Coghlanc495c662006-09-07 10:50:34 +0000193 # Check actual module
194 self.assertEqual(inspect.getmodule(mod), mod)
195 # Check class (uses __module__ attribute)
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000196 self.assertEqual(inspect.getmodule(mod.StupidGit), mod)
Nick Coghlanc495c662006-09-07 10:50:34 +0000197 # Check a method (no __module__ attribute, falls back to filename)
198 self.assertEqual(inspect.getmodule(mod.StupidGit.abuse), mod)
199 # Do it again (check the caching isn't broken)
200 self.assertEqual(inspect.getmodule(mod.StupidGit.abuse), mod)
201 # Check a builtin
202 self.assertEqual(inspect.getmodule(str), sys.modules["__builtin__"])
203 # Check filename override
204 self.assertEqual(inspect.getmodule(None, modfile), mod)
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000205
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000206 def test_getsource(self):
207 self.assertSourceEqual(git.abuse, 29, 39)
208 self.assertSourceEqual(mod.StupidGit, 21, 46)
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000209
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000210 def test_getsourcefile(self):
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000211 self.assertEqual(inspect.getsourcefile(mod.spam), modfile)
212 self.assertEqual(inspect.getsourcefile(git.abuse), modfile)
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000213
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000214 def test_getfile(self):
215 self.assertEqual(inspect.getfile(mod.StupidGit), mod.__file__)
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000216
Phillip J. Eby5d86bdb2006-07-10 19:03:29 +0000217 def test_getmodule_recursion(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000218 from types import ModuleType
Phillip J. Eby5d86bdb2006-07-10 19:03:29 +0000219 name = '__inspect_dummy'
Christian Heimesc756d002007-11-27 21:34:01 +0000220 m = sys.modules[name] = ModuleType(name)
Tim Peters722b8832006-07-10 21:11:49 +0000221 m.__file__ = "<string>" # hopefully not a real filename...
Phillip J. Eby5d86bdb2006-07-10 19:03:29 +0000222 m.__loader__ = "dummy" # pretend the filename is understood by a loader
223 exec "def x(): pass" in m.__dict__
224 self.assertEqual(inspect.getsourcefile(m.x.func_code), '<string>')
225 del sys.modules[name]
Phillip J. Eby1a2959c2006-07-20 15:54:16 +0000226 inspect.getmodule(compile('a=10','','single'))
Phillip J. Eby5d86bdb2006-07-10 19:03:29 +0000227
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000228class TestDecorators(GetSourceBase):
229 fodderFile = mod2
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000230
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000231 def test_wrapped_decorator(self):
232 self.assertSourceEqual(mod2.wrapped, 14, 17)
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000233
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000234 def test_replacing_decorator(self):
235 self.assertSourceEqual(mod2.gone, 9, 10)
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000236
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000237class TestOneliners(GetSourceBase):
238 fodderFile = mod2
239 def test_oneline_lambda(self):
240 # Test inspect.getsource with a one-line lambda function.
241 self.assertSourceEqual(mod2.oll, 25, 25)
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000242
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000243 def test_threeline_lambda(self):
244 # Test inspect.getsource with a three-line lambda function,
245 # where the second and third lines are _not_ indented.
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000246 self.assertSourceEqual(mod2.tll, 28, 30)
247
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000248 def test_twoline_indented_lambda(self):
249 # Test inspect.getsource with a two-line lambda function,
250 # where the second line _is_ indented.
251 self.assertSourceEqual(mod2.tlli, 33, 34)
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000252
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000253 def test_onelinefunc(self):
254 # Test inspect.getsource with a regular one-line function.
255 self.assertSourceEqual(mod2.onelinefunc, 37, 37)
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000256
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000257 def test_manyargs(self):
258 # Test inspect.getsource with a regular function where
259 # the arguments are on two lines and _not_ indented and
260 # the body on the second line with the last arguments.
261 self.assertSourceEqual(mod2.manyargs, 40, 41)
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000262
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000263 def test_twolinefunc(self):
264 # Test inspect.getsource with a regular function where
265 # the body is on two lines, following the argument list and
266 # continued on the next line by a \\.
267 self.assertSourceEqual(mod2.twolinefunc, 44, 45)
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000268
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000269 def test_lambda_in_list(self):
270 # Test inspect.getsource with a one-line lambda function
271 # defined in a list, indented.
272 self.assertSourceEqual(mod2.a[1], 49, 49)
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000273
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000274 def test_anonymous(self):
275 # Test inspect.getsource with a lambda function defined
276 # as argument to another function.
277 self.assertSourceEqual(mod2.anonymous, 55, 55)
278
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000279class TestBuggyCases(GetSourceBase):
280 fodderFile = mod2
281
282 def test_with_comment(self):
283 self.assertSourceEqual(mod2.with_comment, 58, 59)
284
285 def test_multiline_sig(self):
286 self.assertSourceEqual(mod2.multiline_sig[0], 63, 64)
287
Armin Rigodd5c0232005-09-25 11:45:45 +0000288 def test_nested_class(self):
289 self.assertSourceEqual(mod2.func69().func71, 71, 72)
290
291 def test_one_liner_followed_by_non_name(self):
292 self.assertSourceEqual(mod2.func77, 77, 77)
293
294 def test_one_liner_dedent_non_name(self):
295 self.assertSourceEqual(mod2.cls82.func83, 83, 83)
296
297 def test_with_comment_instead_of_docstring(self):
298 self.assertSourceEqual(mod2.func88, 88, 90)
299
Georg Brandl2463f8f2006-08-14 21:34:08 +0000300 def test_method_in_dynamic_class(self):
301 self.assertSourceEqual(mod2.method_in_dynamic_class, 95, 97)
302
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000303# Helper for testing classify_class_attrs.
Tim Peters13b49d32001-09-23 02:00:29 +0000304def attrs_wo_objs(cls):
305 return [t[:3] for t in inspect.classify_class_attrs(cls)]
306
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000307class TestClassesAndFunctions(unittest.TestCase):
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000308 def test_classic_mro(self):
309 # Test classic-class method resolution order.
310 class A: pass
311 class B(A): pass
312 class C(A): pass
313 class D(B, C): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000314
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000315 expected = (D, B, A, C)
316 got = inspect.getmro(D)
317 self.assertEqual(expected, got)
Tim Peters13b49d32001-09-23 02:00:29 +0000318
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000319 def test_newstyle_mro(self):
320 # The same w/ new-class MRO.
321 class A(object): pass
322 class B(A): pass
323 class C(A): pass
324 class D(B, C): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000325
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000326 expected = (D, B, C, A, object)
327 got = inspect.getmro(D)
328 self.assertEqual(expected, got)
Tim Peters13b49d32001-09-23 02:00:29 +0000329
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000330 def assertArgSpecEquals(self, routine, args_e, varargs_e = None,
331 varkw_e = None, defaults_e = None,
332 formatted = None):
333 args, varargs, varkw, defaults = inspect.getargspec(routine)
334 self.assertEqual(args, args_e)
335 self.assertEqual(varargs, varargs_e)
336 self.assertEqual(varkw, varkw_e)
337 self.assertEqual(defaults, defaults_e)
338 if formatted is not None:
339 self.assertEqual(inspect.formatargspec(args, varargs, varkw, defaults),
340 formatted)
Tim Peters13b49d32001-09-23 02:00:29 +0000341
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000342 def test_getargspec(self):
343 self.assertArgSpecEquals(mod.eggs, ['x', 'y'], formatted = '(x, y)')
Tim Peters13b49d32001-09-23 02:00:29 +0000344
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000345 self.assertArgSpecEquals(mod.spam,
346 ['a', 'b', 'c', 'd', ['e', ['f']]],
347 'g', 'h', (3, (4, (5,))),
348 '(a, b, c, d=3, (e, (f,))=(4, (5,)), *g, **h)')
Tim Peters13b49d32001-09-23 02:00:29 +0000349
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000350 def test_getargspec_method(self):
351 class A(object):
352 def m(self):
353 pass
354 self.assertArgSpecEquals(A.m, ['self'])
Tim Peters13b49d32001-09-23 02:00:29 +0000355
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000356 def test_getargspec_sublistofone(self):
Neal Norwitz33b730e2006-03-27 08:58:23 +0000357 def sublistOfOne((foo,)): return 1
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000358 self.assertArgSpecEquals(sublistOfOne, [['foo']])
359
Neal Norwitz33b730e2006-03-27 08:58:23 +0000360 def fakeSublistOfOne((foo)): return 1
361 self.assertArgSpecEquals(fakeSublistOfOne, ['foo'])
362
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000363 def test_classify_oldstyle(self):
364 class A:
365 def s(): pass
366 s = staticmethod(s)
367
368 def c(cls): pass
369 c = classmethod(c)
370
371 def getp(self): pass
372 p = property(getp)
373
374 def m(self): pass
375
376 def m1(self): pass
377
378 datablob = '1'
379
380 attrs = attrs_wo_objs(A)
381 self.assert_(('s', 'static method', A) in attrs, 'missing static method')
382 self.assert_(('c', 'class method', A) in attrs, 'missing class method')
383 self.assert_(('p', 'property', A) in attrs, 'missing property')
384 self.assert_(('m', 'method', A) in attrs, 'missing plain method')
385 self.assert_(('m1', 'method', A) in attrs, 'missing plain method')
386 self.assert_(('datablob', 'data', A) in attrs, 'missing data')
387
388 class B(A):
389 def m(self): pass
390
391 attrs = attrs_wo_objs(B)
392 self.assert_(('s', 'static method', A) in attrs, 'missing static method')
393 self.assert_(('c', 'class method', A) in attrs, 'missing class method')
394 self.assert_(('p', 'property', A) in attrs, 'missing property')
395 self.assert_(('m', 'method', B) in attrs, 'missing plain method')
396 self.assert_(('m1', 'method', A) in attrs, 'missing plain method')
397 self.assert_(('datablob', 'data', A) in attrs, 'missing data')
Tim Peters13b49d32001-09-23 02:00:29 +0000398
399
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000400 class C(A):
401 def m(self): pass
402 def c(self): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000403
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000404 attrs = attrs_wo_objs(C)
405 self.assert_(('s', 'static method', A) in attrs, 'missing static method')
406 self.assert_(('c', 'method', C) in attrs, 'missing plain method')
407 self.assert_(('p', 'property', A) in attrs, 'missing property')
408 self.assert_(('m', 'method', C) in attrs, 'missing plain method')
409 self.assert_(('m1', 'method', A) in attrs, 'missing plain method')
410 self.assert_(('datablob', 'data', A) in attrs, 'missing data')
Tim Peters13b49d32001-09-23 02:00:29 +0000411
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000412 class D(B, C):
413 def m1(self): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000414
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000415 attrs = attrs_wo_objs(D)
416 self.assert_(('s', 'static method', A) in attrs, 'missing static method')
417 self.assert_(('c', 'class method', A) in attrs, 'missing class method')
418 self.assert_(('p', 'property', A) in attrs, 'missing property')
419 self.assert_(('m', 'method', B) in attrs, 'missing plain method')
420 self.assert_(('m1', 'method', D) in attrs, 'missing plain method')
421 self.assert_(('datablob', 'data', A) in attrs, 'missing data')
Tim Peters13b49d32001-09-23 02:00:29 +0000422
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000423 # Repeat all that, but w/ new-style classes.
424 def test_classify_newstyle(self):
425 class A(object):
Tim Peters13b49d32001-09-23 02:00:29 +0000426
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000427 def s(): pass
428 s = staticmethod(s)
Tim Peters13b49d32001-09-23 02:00:29 +0000429
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000430 def c(cls): pass
431 c = classmethod(c)
Tim Peters13b49d32001-09-23 02:00:29 +0000432
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000433 def getp(self): pass
434 p = property(getp)
Tim Peters13b49d32001-09-23 02:00:29 +0000435
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000436 def m(self): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000437
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000438 def m1(self): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000439
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000440 datablob = '1'
Tim Peters13b49d32001-09-23 02:00:29 +0000441
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000442 attrs = attrs_wo_objs(A)
443 self.assert_(('s', 'static method', A) in attrs, 'missing static method')
444 self.assert_(('c', 'class method', A) in attrs, 'missing class method')
445 self.assert_(('p', 'property', A) in attrs, 'missing property')
446 self.assert_(('m', 'method', A) in attrs, 'missing plain method')
447 self.assert_(('m1', 'method', A) in attrs, 'missing plain method')
448 self.assert_(('datablob', 'data', A) in attrs, 'missing data')
Tim Peters13b49d32001-09-23 02:00:29 +0000449
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000450 class B(A):
Tim Peters13b49d32001-09-23 02:00:29 +0000451
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000452 def m(self): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000453
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000454 attrs = attrs_wo_objs(B)
455 self.assert_(('s', 'static method', A) in attrs, 'missing static method')
456 self.assert_(('c', 'class method', A) in attrs, 'missing class method')
457 self.assert_(('p', 'property', A) in attrs, 'missing property')
458 self.assert_(('m', 'method', B) in attrs, 'missing plain method')
459 self.assert_(('m1', 'method', A) in attrs, 'missing plain method')
460 self.assert_(('datablob', 'data', A) in attrs, 'missing data')
Tim Peters13b49d32001-09-23 02:00:29 +0000461
462
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000463 class C(A):
Tim Peters13b49d32001-09-23 02:00:29 +0000464
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000465 def m(self): pass
466 def c(self): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000467
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000468 attrs = attrs_wo_objs(C)
469 self.assert_(('s', 'static method', A) in attrs, 'missing static method')
470 self.assert_(('c', 'method', C) in attrs, 'missing plain method')
471 self.assert_(('p', 'property', A) in attrs, 'missing property')
472 self.assert_(('m', 'method', C) in attrs, 'missing plain method')
473 self.assert_(('m1', 'method', A) in attrs, 'missing plain method')
474 self.assert_(('datablob', 'data', A) in attrs, 'missing data')
Tim Peters13b49d32001-09-23 02:00:29 +0000475
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000476 class D(B, C):
Tim Peters13b49d32001-09-23 02:00:29 +0000477
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000478 def m1(self): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000479
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000480 attrs = attrs_wo_objs(D)
481 self.assert_(('s', 'static method', A) in attrs, 'missing static method')
482 self.assert_(('c', 'method', C) in attrs, 'missing plain method')
483 self.assert_(('p', 'property', A) in attrs, 'missing property')
484 self.assert_(('m', 'method', B) in attrs, 'missing plain method')
485 self.assert_(('m1', 'method', D) in attrs, 'missing plain method')
486 self.assert_(('datablob', 'data', A) in attrs, 'missing data')
Jeremy Hyltonc4bf5ed2003-06-27 18:43:12 +0000487
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000488def test_main():
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000489 run_unittest(TestDecorators, TestRetrievingSourceCode, TestOneliners,
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000490 TestBuggyCases,
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000491 TestInterpreterStack, TestClassesAndFunctions, TestPredicates)
Martin v. Löwis893ffa42003-10-31 15:35:53 +0000492
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000493if __name__ == "__main__":
494 test_main()