blob: 144fcf371300b283c24a43bc3f64dff52ff56067 [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
Senthil Kumaran3ddc4352010-01-08 18:41:40 +00007from test.test_support import TESTFN, run_unittest, check_warnings
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00008
Senthil Kumaran3ddc4352010-01-08 18:41:40 +00009with check_warnings():
10 from test import inspect_fodder as mod
11 from test import inspect_fodder2 as mod2
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000012
R. David Murray996ba022009-05-13 17:14:11 +000013# C module for test_findsource_binary
R. David Murray87855542009-05-14 16:12:57 +000014import unicodedata
R. David Murray996ba022009-05-13 17:14:11 +000015
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000016# Functions tested in this suite:
17# ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode,
Facundo Batista759bfc62008-02-18 03:43:43 +000018# isbuiltin, isroutine, isgenerator, isgeneratorfunction, getmembers,
19# getdoc, getfile, getmodule, getsourcefile, getcomments, getsource,
20# getclasstree, getargspec, getargvalues, formatargspec, formatargvalues,
21# currentframe, stack, trace, isdatadescriptor
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000022
Nick Coghlana2053472008-12-14 10:54:50 +000023# NOTE: There are some additional tests relating to interaction with
24# zipimport in the test_zipimport_support test module.
25
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000026modfile = mod.__file__
Georg Brandlb2afe852006-06-09 20:43:48 +000027if modfile.endswith(('c', 'o')):
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000028 modfile = modfile[:-1]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000029
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000030import __builtin__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000031
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000032try:
Senthil Kumaran3ddc4352010-01-08 18:41:40 +000033 1 // 0
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000034except:
35 tb = sys.exc_traceback
36
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000037git = mod.StupidGit()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000038
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000039class IsTestBase(unittest.TestCase):
40 predicates = set([inspect.isbuiltin, inspect.isclass, inspect.iscode,
41 inspect.isframe, inspect.isfunction, inspect.ismethod,
Facundo Batista759bfc62008-02-18 03:43:43 +000042 inspect.ismodule, inspect.istraceback,
43 inspect.isgenerator, inspect.isgeneratorfunction])
Tim Peters5a9fb3c2005-01-07 16:01:32 +000044
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000045 def istest(self, predicate, exp):
46 obj = eval(exp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +000047 self.assertTrue(predicate(obj), '%s(%s)' % (predicate.__name__, exp))
Tim Peters5a9fb3c2005-01-07 16:01:32 +000048
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000049 for other in self.predicates - set([predicate]):
Facundo Batista759bfc62008-02-18 03:43:43 +000050 if predicate == inspect.isgeneratorfunction and\
51 other == inspect.isfunction:
52 continue
Benjamin Peterson5c8da862009-06-30 22:57:08 +000053 self.assertFalse(other(obj), 'not %s(%s)' % (other.__name__, exp))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000054
Facundo Batista759bfc62008-02-18 03:43:43 +000055def generator_function_example(self):
56 for i in xrange(2):
57 yield i
58
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000059class TestPredicates(IsTestBase):
Christian Heimes728bee82008-03-03 20:30:29 +000060 def test_sixteen(self):
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000061 count = len(filter(lambda x:x.startswith('is'), dir(inspect)))
Facundo Batista759bfc62008-02-18 03:43:43 +000062 # This test is here for remember you to update Doc/library/inspect.rst
Georg Brandl26bc1772008-03-03 20:39:00 +000063 # which claims there are 16 such functions
Christian Heimes728bee82008-03-03 20:30:29 +000064 expected = 16
Neal Norwitzdf80af72006-07-28 04:22:34 +000065 err_msg = "There are %d (not %d) is* functions" % (count, expected)
66 self.assertEqual(count, expected, err_msg)
Tim Peters5a9fb3c2005-01-07 16:01:32 +000067
Facundo Batista759bfc62008-02-18 03:43:43 +000068
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000069 def test_excluding_predicates(self):
70 self.istest(inspect.isbuiltin, 'sys.exit')
71 self.istest(inspect.isbuiltin, '[].append')
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000072 self.istest(inspect.iscode, 'mod.spam.func_code')
73 self.istest(inspect.isframe, 'tb.tb_frame')
74 self.istest(inspect.isfunction, 'mod.spam')
75 self.istest(inspect.ismethod, 'mod.StupidGit.abuse')
76 self.istest(inspect.ismethod, 'git.argue')
77 self.istest(inspect.ismodule, 'mod')
78 self.istest(inspect.istraceback, 'tb')
79 self.istest(inspect.isdatadescriptor, '__builtin__.file.closed')
80 self.istest(inspect.isdatadescriptor, '__builtin__.file.softspace')
Facundo Batista759bfc62008-02-18 03:43:43 +000081 self.istest(inspect.isgenerator, '(x for x in xrange(2))')
82 self.istest(inspect.isgeneratorfunction, 'generator_function_example')
Barry Warsaw00decd72006-07-27 23:43:15 +000083 if hasattr(types, 'GetSetDescriptorType'):
84 self.istest(inspect.isgetsetdescriptor,
85 'type(tb.tb_frame).f_locals')
86 else:
Benjamin Peterson5c8da862009-06-30 22:57:08 +000087 self.assertFalse(inspect.isgetsetdescriptor(type(tb.tb_frame).f_locals))
Barry Warsaw00decd72006-07-27 23:43:15 +000088 if hasattr(types, 'MemberDescriptorType'):
89 self.istest(inspect.ismemberdescriptor, 'datetime.timedelta.days')
90 else:
Benjamin Peterson5c8da862009-06-30 22:57:08 +000091 self.assertFalse(inspect.ismemberdescriptor(datetime.timedelta.days))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000092
Johannes Gijsberscb9015d2004-12-12 16:20:22 +000093 def test_isroutine(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000094 self.assertTrue(inspect.isroutine(mod.spam))
95 self.assertTrue(inspect.isroutine([].count))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000096
Benjamin Peterson5e5fbb62009-01-17 22:27:54 +000097 def test_isclass(self):
98 self.istest(inspect.isclass, 'mod.StupidGit')
99 self.assertTrue(inspect.isclass(list))
100
101 class newstyle(object): pass
102 self.assertTrue(inspect.isclass(newstyle))
103
104 class CustomGetattr(object):
105 def __getattr__(self, attr):
106 return None
107 self.assertFalse(inspect.isclass(CustomGetattr()))
108
Amaury Forgeot d'Arcb54447f2009-01-13 23:39:22 +0000109 def test_get_slot_members(self):
110 class C(object):
111 __slots__ = ("a", "b")
112
113 x = C()
114 x.a = 42
115 members = dict(inspect.getmembers(x))
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000116 self.assertTrue('a' in members)
117 self.assertTrue('b' not in members)
Amaury Forgeot d'Arcb54447f2009-01-13 23:39:22 +0000118
Benjamin Petersonc63457b2009-10-15 03:06:55 +0000119 def test_isabstract(self):
120 from abc import ABCMeta, abstractmethod
121
122 class AbstractClassExample(object):
123 __metaclass__ = ABCMeta
124
125 @abstractmethod
126 def foo(self):
127 pass
128
129 class ClassExample(AbstractClassExample):
130 def foo(self):
131 pass
132
133 a = ClassExample()
134
135 # Test general behaviour.
136 self.assertTrue(inspect.isabstract(AbstractClassExample))
137 self.assertFalse(inspect.isabstract(ClassExample))
138 self.assertFalse(inspect.isabstract(a))
139 self.assertFalse(inspect.isabstract(int))
140 self.assertFalse(inspect.isabstract(5))
141
Amaury Forgeot d'Arcb54447f2009-01-13 23:39:22 +0000142
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000143class TestInterpreterStack(IsTestBase):
144 def __init__(self, *args, **kwargs):
145 unittest.TestCase.__init__(self, *args, **kwargs)
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000146
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000147 git.abuse(7, 8, 9)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000148
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000149 def test_abuse_done(self):
150 self.istest(inspect.istraceback, 'git.ex[2]')
151 self.istest(inspect.isframe, 'mod.fr')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000152
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000153 def test_stack(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000154 self.assertTrue(len(mod.st) >= 5)
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000155 self.assertEqual(mod.st[0][1:],
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000156 (modfile, 16, 'eggs', [' st = inspect.stack()\n'], 0))
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000157 self.assertEqual(mod.st[1][1:],
158 (modfile, 9, 'spam', [' eggs(b + d, c + f)\n'], 0))
159 self.assertEqual(mod.st[2][1:],
160 (modfile, 43, 'argue', [' spam(a, b, c)\n'], 0))
161 self.assertEqual(mod.st[3][1:],
162 (modfile, 39, 'abuse', [' self.argue(a, b, c)\n'], 0))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000163
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000164 def test_trace(self):
165 self.assertEqual(len(git.tr), 3)
166 self.assertEqual(git.tr[0][1:], (modfile, 43, 'argue',
167 [' spam(a, b, c)\n'], 0))
168 self.assertEqual(git.tr[1][1:], (modfile, 9, 'spam',
169 [' eggs(b + d, c + f)\n'], 0))
170 self.assertEqual(git.tr[2][1:], (modfile, 18, 'eggs',
Senthil Kumaran3ddc4352010-01-08 18:41:40 +0000171 [' q = y // 0\n'], 0))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000172
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000173 def test_frame(self):
174 args, varargs, varkw, locals = inspect.getargvalues(mod.fr)
175 self.assertEqual(args, ['x', 'y'])
176 self.assertEqual(varargs, None)
177 self.assertEqual(varkw, None)
178 self.assertEqual(locals, {'x': 11, 'p': 11, 'y': 14})
179 self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals),
180 '(x=11, y=14)')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000181
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000182 def test_previous_frame(self):
183 args, varargs, varkw, locals = inspect.getargvalues(mod.fr.f_back)
184 self.assertEqual(args, ['a', 'b', 'c', 'd', ['e', ['f']]])
185 self.assertEqual(varargs, 'g')
186 self.assertEqual(varkw, 'h')
187 self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals),
188 '(a=7, b=8, c=9, d=3, (e=4, (f=5,)), *g=(), **h={})')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000189
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000190class GetSourceBase(unittest.TestCase):
191 # Subclasses must override.
192 fodderFile = None
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000193
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000194 def __init__(self, *args, **kwargs):
195 unittest.TestCase.__init__(self, *args, **kwargs)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000196
Philip Jenvey6a111022009-05-28 05:58:44 +0000197 with open(inspect.getsourcefile(self.fodderFile)) as fp:
198 self.source = fp.read()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000199
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000200 def sourcerange(self, top, bottom):
201 lines = self.source.split("\n")
202 return "\n".join(lines[top-1:bottom]) + "\n"
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000203
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000204 def assertSourceEqual(self, obj, top, bottom):
205 self.assertEqual(inspect.getsource(obj),
206 self.sourcerange(top, bottom))
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000207
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000208class TestRetrievingSourceCode(GetSourceBase):
209 fodderFile = mod
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000210
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000211 def test_getclasses(self):
212 classes = inspect.getmembers(mod, inspect.isclass)
213 self.assertEqual(classes,
214 [('FesteringGob', mod.FesteringGob),
215 ('MalodorousPervert', mod.MalodorousPervert),
216 ('ParrotDroppings', mod.ParrotDroppings),
217 ('StupidGit', mod.StupidGit)])
218 tree = inspect.getclasstree([cls[1] for cls in classes], 1)
219 self.assertEqual(tree,
220 [(mod.ParrotDroppings, ()),
221 (mod.StupidGit, ()),
222 [(mod.MalodorousPervert, (mod.StupidGit,)),
223 [(mod.FesteringGob, (mod.MalodorousPervert,
224 mod.ParrotDroppings))
225 ]
226 ]
227 ])
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000228
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000229 def test_getfunctions(self):
230 functions = inspect.getmembers(mod, inspect.isfunction)
231 self.assertEqual(functions, [('eggs', mod.eggs),
232 ('spam', mod.spam)])
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000233
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000234 def test_getdoc(self):
235 self.assertEqual(inspect.getdoc(mod), 'A module docstring.')
236 self.assertEqual(inspect.getdoc(mod.StupidGit),
237 'A longer,\n\nindented\n\ndocstring.')
238 self.assertEqual(inspect.getdoc(git.abuse),
239 'Another\n\ndocstring\n\ncontaining\n\ntabs')
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000240
Georg Brandl7be19aa2008-06-07 15:59:10 +0000241 def test_cleandoc(self):
242 self.assertEqual(inspect.cleandoc('An\n indented\n docstring.'),
243 'An\nindented\ndocstring.')
244
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000245 def test_getcomments(self):
246 self.assertEqual(inspect.getcomments(mod), '# line 1\n')
247 self.assertEqual(inspect.getcomments(mod.StupidGit), '# line 20\n')
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000248
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000249 def test_getmodule(self):
Nick Coghlanc495c662006-09-07 10:50:34 +0000250 # Check actual module
251 self.assertEqual(inspect.getmodule(mod), mod)
252 # Check class (uses __module__ attribute)
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000253 self.assertEqual(inspect.getmodule(mod.StupidGit), mod)
Nick Coghlanc495c662006-09-07 10:50:34 +0000254 # Check a method (no __module__ attribute, falls back to filename)
255 self.assertEqual(inspect.getmodule(mod.StupidGit.abuse), mod)
256 # Do it again (check the caching isn't broken)
257 self.assertEqual(inspect.getmodule(mod.StupidGit.abuse), mod)
258 # Check a builtin
259 self.assertEqual(inspect.getmodule(str), sys.modules["__builtin__"])
260 # Check filename override
261 self.assertEqual(inspect.getmodule(None, modfile), mod)
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000262
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000263 def test_getsource(self):
264 self.assertSourceEqual(git.abuse, 29, 39)
265 self.assertSourceEqual(mod.StupidGit, 21, 46)
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000266
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000267 def test_getsourcefile(self):
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000268 self.assertEqual(inspect.getsourcefile(mod.spam), modfile)
269 self.assertEqual(inspect.getsourcefile(git.abuse), modfile)
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000270
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000271 def test_getfile(self):
272 self.assertEqual(inspect.getfile(mod.StupidGit), mod.__file__)
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000273
Phillip J. Eby5d86bdb2006-07-10 19:03:29 +0000274 def test_getmodule_recursion(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000275 from types import ModuleType
Phillip J. Eby5d86bdb2006-07-10 19:03:29 +0000276 name = '__inspect_dummy'
Christian Heimesc756d002007-11-27 21:34:01 +0000277 m = sys.modules[name] = ModuleType(name)
Tim Peters722b8832006-07-10 21:11:49 +0000278 m.__file__ = "<string>" # hopefully not a real filename...
Phillip J. Eby5d86bdb2006-07-10 19:03:29 +0000279 m.__loader__ = "dummy" # pretend the filename is understood by a loader
280 exec "def x(): pass" in m.__dict__
281 self.assertEqual(inspect.getsourcefile(m.x.func_code), '<string>')
282 del sys.modules[name]
Phillip J. Eby1a2959c2006-07-20 15:54:16 +0000283 inspect.getmodule(compile('a=10','','single'))
Phillip J. Eby5d86bdb2006-07-10 19:03:29 +0000284
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000285class TestDecorators(GetSourceBase):
286 fodderFile = mod2
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000287
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000288 def test_wrapped_decorator(self):
289 self.assertSourceEqual(mod2.wrapped, 14, 17)
Johannes Gijsbersc473c992004-08-18 12:40:31 +0000290
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000291 def test_replacing_decorator(self):
292 self.assertSourceEqual(mod2.gone, 9, 10)
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000293
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000294class TestOneliners(GetSourceBase):
295 fodderFile = mod2
296 def test_oneline_lambda(self):
297 # Test inspect.getsource with a one-line lambda function.
298 self.assertSourceEqual(mod2.oll, 25, 25)
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000299
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000300 def test_threeline_lambda(self):
301 # Test inspect.getsource with a three-line lambda function,
302 # where the second and third lines are _not_ indented.
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000303 self.assertSourceEqual(mod2.tll, 28, 30)
304
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000305 def test_twoline_indented_lambda(self):
306 # Test inspect.getsource with a two-line lambda function,
307 # where the second line _is_ indented.
308 self.assertSourceEqual(mod2.tlli, 33, 34)
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000309
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000310 def test_onelinefunc(self):
311 # Test inspect.getsource with a regular one-line function.
312 self.assertSourceEqual(mod2.onelinefunc, 37, 37)
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000313
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000314 def test_manyargs(self):
315 # Test inspect.getsource with a regular function where
316 # the arguments are on two lines and _not_ indented and
317 # the body on the second line with the last arguments.
318 self.assertSourceEqual(mod2.manyargs, 40, 41)
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000319
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000320 def test_twolinefunc(self):
321 # Test inspect.getsource with a regular function where
322 # the body is on two lines, following the argument list and
323 # continued on the next line by a \\.
324 self.assertSourceEqual(mod2.twolinefunc, 44, 45)
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000325
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000326 def test_lambda_in_list(self):
327 # Test inspect.getsource with a one-line lambda function
328 # defined in a list, indented.
329 self.assertSourceEqual(mod2.a[1], 49, 49)
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000330
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000331 def test_anonymous(self):
332 # Test inspect.getsource with a lambda function defined
333 # as argument to another function.
334 self.assertSourceEqual(mod2.anonymous, 55, 55)
335
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000336class TestBuggyCases(GetSourceBase):
337 fodderFile = mod2
338
339 def test_with_comment(self):
340 self.assertSourceEqual(mod2.with_comment, 58, 59)
341
342 def test_multiline_sig(self):
343 self.assertSourceEqual(mod2.multiline_sig[0], 63, 64)
344
Armin Rigodd5c0232005-09-25 11:45:45 +0000345 def test_nested_class(self):
346 self.assertSourceEqual(mod2.func69().func71, 71, 72)
347
348 def test_one_liner_followed_by_non_name(self):
349 self.assertSourceEqual(mod2.func77, 77, 77)
350
351 def test_one_liner_dedent_non_name(self):
352 self.assertSourceEqual(mod2.cls82.func83, 83, 83)
353
354 def test_with_comment_instead_of_docstring(self):
355 self.assertSourceEqual(mod2.func88, 88, 90)
356
Georg Brandl2463f8f2006-08-14 21:34:08 +0000357 def test_method_in_dynamic_class(self):
358 self.assertSourceEqual(mod2.method_in_dynamic_class, 95, 97)
359
R. David Murray87855542009-05-14 16:12:57 +0000360 @unittest.skipIf(
361 not hasattr(unicodedata, '__file__') or
362 unicodedata.__file__[-4:] in (".pyc", ".pyo"),
363 "unicodedata is not an external binary module")
R. David Murray996ba022009-05-13 17:14:11 +0000364 def test_findsource_binary(self):
R. David Murray87855542009-05-14 16:12:57 +0000365 self.assertRaises(IOError, inspect.getsource, unicodedata)
366 self.assertRaises(IOError, inspect.findsource, unicodedata)
R. David Murray996ba022009-05-13 17:14:11 +0000367
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000368# Helper for testing classify_class_attrs.
Tim Peters13b49d32001-09-23 02:00:29 +0000369def attrs_wo_objs(cls):
370 return [t[:3] for t in inspect.classify_class_attrs(cls)]
371
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000372class TestClassesAndFunctions(unittest.TestCase):
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000373 def test_classic_mro(self):
374 # Test classic-class method resolution order.
375 class A: pass
376 class B(A): pass
377 class C(A): pass
378 class D(B, C): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000379
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000380 expected = (D, B, A, C)
381 got = inspect.getmro(D)
382 self.assertEqual(expected, got)
Tim Peters13b49d32001-09-23 02:00:29 +0000383
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000384 def test_newstyle_mro(self):
385 # The same w/ new-class MRO.
386 class A(object): pass
387 class B(A): pass
388 class C(A): pass
389 class D(B, C): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000390
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000391 expected = (D, B, C, A, object)
392 got = inspect.getmro(D)
393 self.assertEqual(expected, got)
Tim Peters13b49d32001-09-23 02:00:29 +0000394
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000395 def assertArgSpecEquals(self, routine, args_e, varargs_e = None,
396 varkw_e = None, defaults_e = None,
397 formatted = None):
398 args, varargs, varkw, defaults = inspect.getargspec(routine)
399 self.assertEqual(args, args_e)
400 self.assertEqual(varargs, varargs_e)
401 self.assertEqual(varkw, varkw_e)
402 self.assertEqual(defaults, defaults_e)
403 if formatted is not None:
404 self.assertEqual(inspect.formatargspec(args, varargs, varkw, defaults),
405 formatted)
Tim Peters13b49d32001-09-23 02:00:29 +0000406
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000407 def test_getargspec(self):
408 self.assertArgSpecEquals(mod.eggs, ['x', 'y'], formatted = '(x, y)')
Tim Peters13b49d32001-09-23 02:00:29 +0000409
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000410 self.assertArgSpecEquals(mod.spam,
411 ['a', 'b', 'c', 'd', ['e', ['f']]],
412 'g', 'h', (3, (4, (5,))),
413 '(a, b, c, d=3, (e, (f,))=(4, (5,)), *g, **h)')
Tim Peters13b49d32001-09-23 02:00:29 +0000414
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000415 def test_getargspec_method(self):
416 class A(object):
417 def m(self):
418 pass
419 self.assertArgSpecEquals(A.m, ['self'])
Tim Peters13b49d32001-09-23 02:00:29 +0000420
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000421 def test_getargspec_sublistofone(self):
Senthil Kumaran3ddc4352010-01-08 18:41:40 +0000422 # Silence Py3k warning
423 with check_warnings():
424 exec 'def sublistOfOne((foo,)): return 1'
425 self.assertArgSpecEquals(sublistOfOne, [['foo']])
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000426
Senthil Kumaran3ddc4352010-01-08 18:41:40 +0000427 exec 'def fakeSublistOfOne((foo)): return 1'
428 self.assertArgSpecEquals(fakeSublistOfOne, ['foo'])
Neal Norwitz33b730e2006-03-27 08:58:23 +0000429
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000430 def test_classify_oldstyle(self):
431 class A:
432 def s(): pass
433 s = staticmethod(s)
434
435 def c(cls): pass
436 c = classmethod(c)
437
438 def getp(self): pass
439 p = property(getp)
440
441 def m(self): pass
442
443 def m1(self): pass
444
445 datablob = '1'
446
447 attrs = attrs_wo_objs(A)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000448 self.assertTrue(('s', 'static method', A) in attrs, 'missing static method')
449 self.assertTrue(('c', 'class method', A) in attrs, 'missing class method')
450 self.assertTrue(('p', 'property', A) in attrs, 'missing property')
451 self.assertTrue(('m', 'method', A) in attrs, 'missing plain method')
452 self.assertTrue(('m1', 'method', A) in attrs, 'missing plain method')
453 self.assertTrue(('datablob', 'data', A) in attrs, 'missing data')
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000454
455 class B(A):
456 def m(self): pass
457
458 attrs = attrs_wo_objs(B)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000459 self.assertTrue(('s', 'static method', A) in attrs, 'missing static method')
460 self.assertTrue(('c', 'class method', A) in attrs, 'missing class method')
461 self.assertTrue(('p', 'property', A) in attrs, 'missing property')
462 self.assertTrue(('m', 'method', B) in attrs, 'missing plain method')
463 self.assertTrue(('m1', 'method', A) in attrs, 'missing plain method')
464 self.assertTrue(('datablob', 'data', A) in attrs, 'missing data')
Tim Peters13b49d32001-09-23 02:00:29 +0000465
466
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000467 class C(A):
468 def m(self): pass
469 def c(self): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000470
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000471 attrs = attrs_wo_objs(C)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000472 self.assertTrue(('s', 'static method', A) in attrs, 'missing static method')
473 self.assertTrue(('c', 'method', C) in attrs, 'missing plain method')
474 self.assertTrue(('p', 'property', A) in attrs, 'missing property')
475 self.assertTrue(('m', 'method', C) in attrs, 'missing plain method')
476 self.assertTrue(('m1', 'method', A) in attrs, 'missing plain method')
477 self.assertTrue(('datablob', 'data', A) in attrs, 'missing data')
Tim Peters13b49d32001-09-23 02:00:29 +0000478
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000479 class D(B, C):
480 def m1(self): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000481
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000482 attrs = attrs_wo_objs(D)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000483 self.assertTrue(('s', 'static method', A) in attrs, 'missing static method')
484 self.assertTrue(('c', 'class method', A) in attrs, 'missing class method')
485 self.assertTrue(('p', 'property', A) in attrs, 'missing property')
486 self.assertTrue(('m', 'method', B) in attrs, 'missing plain method')
487 self.assertTrue(('m1', 'method', D) in attrs, 'missing plain method')
488 self.assertTrue(('datablob', 'data', A) in attrs, 'missing data')
Tim Peters13b49d32001-09-23 02:00:29 +0000489
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000490 # Repeat all that, but w/ new-style classes.
491 def test_classify_newstyle(self):
492 class A(object):
Tim Peters13b49d32001-09-23 02:00:29 +0000493
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000494 def s(): pass
495 s = staticmethod(s)
Tim Peters13b49d32001-09-23 02:00:29 +0000496
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000497 def c(cls): pass
498 c = classmethod(c)
Tim Peters13b49d32001-09-23 02:00:29 +0000499
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000500 def getp(self): pass
501 p = property(getp)
Tim Peters13b49d32001-09-23 02:00:29 +0000502
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000503 def m(self): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000504
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000505 def m1(self): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000506
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000507 datablob = '1'
Tim Peters13b49d32001-09-23 02:00:29 +0000508
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000509 attrs = attrs_wo_objs(A)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000510 self.assertTrue(('s', 'static method', A) in attrs, 'missing static method')
511 self.assertTrue(('c', 'class method', A) in attrs, 'missing class method')
512 self.assertTrue(('p', 'property', A) in attrs, 'missing property')
513 self.assertTrue(('m', 'method', A) in attrs, 'missing plain method')
514 self.assertTrue(('m1', 'method', A) in attrs, 'missing plain method')
515 self.assertTrue(('datablob', 'data', A) in attrs, 'missing data')
Tim Peters13b49d32001-09-23 02:00:29 +0000516
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000517 class B(A):
Tim Peters13b49d32001-09-23 02:00:29 +0000518
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000519 def m(self): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000520
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000521 attrs = attrs_wo_objs(B)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000522 self.assertTrue(('s', 'static method', A) in attrs, 'missing static method')
523 self.assertTrue(('c', 'class method', A) in attrs, 'missing class method')
524 self.assertTrue(('p', 'property', A) in attrs, 'missing property')
525 self.assertTrue(('m', 'method', B) in attrs, 'missing plain method')
526 self.assertTrue(('m1', 'method', A) in attrs, 'missing plain method')
527 self.assertTrue(('datablob', 'data', A) in attrs, 'missing data')
Tim Peters13b49d32001-09-23 02:00:29 +0000528
529
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000530 class C(A):
Tim Peters13b49d32001-09-23 02:00:29 +0000531
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000532 def m(self): pass
533 def c(self): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000534
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000535 attrs = attrs_wo_objs(C)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000536 self.assertTrue(('s', 'static method', A) in attrs, 'missing static method')
537 self.assertTrue(('c', 'method', C) in attrs, 'missing plain method')
538 self.assertTrue(('p', 'property', A) in attrs, 'missing property')
539 self.assertTrue(('m', 'method', C) in attrs, 'missing plain method')
540 self.assertTrue(('m1', 'method', A) in attrs, 'missing plain method')
541 self.assertTrue(('datablob', 'data', A) in attrs, 'missing data')
Tim Peters13b49d32001-09-23 02:00:29 +0000542
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000543 class D(B, C):
Tim Peters13b49d32001-09-23 02:00:29 +0000544
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000545 def m1(self): pass
Tim Peters13b49d32001-09-23 02:00:29 +0000546
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000547 attrs = attrs_wo_objs(D)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000548 self.assertTrue(('s', 'static method', A) in attrs, 'missing static method')
549 self.assertTrue(('c', 'method', C) in attrs, 'missing plain method')
550 self.assertTrue(('p', 'property', A) in attrs, 'missing property')
551 self.assertTrue(('m', 'method', B) in attrs, 'missing plain method')
552 self.assertTrue(('m1', 'method', D) in attrs, 'missing plain method')
553 self.assertTrue(('datablob', 'data', A) in attrs, 'missing data')
Jeremy Hyltonc4bf5ed2003-06-27 18:43:12 +0000554
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000555def test_main():
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000556 run_unittest(TestDecorators, TestRetrievingSourceCode, TestOneliners,
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000557 TestBuggyCases,
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000558 TestInterpreterStack, TestClassesAndFunctions, TestPredicates)
Martin v. Löwis893ffa42003-10-31 15:35:53 +0000559
Johannes Gijsberscb9015d2004-12-12 16:20:22 +0000560if __name__ == "__main__":
561 test_main()