Kaydet (Commit) cb9015dc authored tarafından Johannes Gijsbers's avatar Johannes Gijsbers

Patch #736962: port test_inspect to unittest. As part of this, move out

the fodder modules to separate files to get rid of the imp.load_source()
trickery.
üst 6b220b03
# line 1
'A module docstring.'
import sys, inspect
# line 5
# line 7
def spam(a, b, c, d=3, (e, (f,))=(4, (5,)), *g, **h):
eggs(b + d, c + f)
# line 11
def eggs(x, y):
"A docstring."
global fr, st
fr = inspect.currentframe()
st = inspect.stack()
p = x
q = y / 0
# line 20
class StupidGit:
"""A longer,
indented
docstring."""
# line 27
def abuse(self, a, b, c):
"""Another
\tdocstring
containing
\ttabs
\t
"""
self.argue(a, b, c)
# line 40
def argue(self, a, b, c):
try:
spam(a, b, c)
except:
self.ex = sys.exc_info()
self.tr = inspect.trace()
# line 48
class MalodorousPervert(StupidGit):
pass
class ParrotDroppings:
pass
class FesteringGob(MalodorousPervert, ParrotDroppings):
pass
# line 1
def wrap(foo=None):
def wrapper(func):
return func
return wrapper
# line 7
def replace(func):
def insteadfunc():
print 'hello'
return insteadfunc
# line 13
@wrap()
@wrap(wrap)
def wrapped():
pass
# line 19
@replace
def gone():
pass
source = '''# line 1 import sys
'A module docstring.' import unittest
import inspect
import sys, inspect
# line 5
# line 7
def spam(a, b, c, d=3, (e, (f,))=(4, (5,)), *g, **h):
eggs(b + d, c + f)
# line 11
def eggs(x, y):
"A docstring."
global fr, st
fr = inspect.currentframe()
st = inspect.stack()
p = x
q = y / 0
# line 20
class StupidGit:
"""A longer,
indented
docstring."""
# line 27
def abuse(self, a, b, c):
"""Another
\tdocstring
containing
\ttabs
\t
"""
self.argue(a, b, c)
# line 40
def argue(self, a, b, c):
try:
spam(a, b, c)
except:
self.ex = sys.exc_info()
self.tr = inspect.trace()
# line 48
class MalodorousPervert(StupidGit):
pass
class ParrotDroppings: from test.test_support import TESTFN, run_unittest
pass
class FesteringGob(MalodorousPervert, ParrotDroppings): from test import inspect_fodder as mod
pass from test import inspect_fodder2 as mod2
'''
# Functions tested in this suite: # Functions tested in this suite:
# ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode, # ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode,
...@@ -63,363 +14,366 @@ class FesteringGob(MalodorousPervert, ParrotDroppings): ...@@ -63,363 +14,366 @@ class FesteringGob(MalodorousPervert, ParrotDroppings):
# getargvalues, formatargspec, formatargvalues, currentframe, stack, trace # getargvalues, formatargspec, formatargvalues, currentframe, stack, trace
# isdatadescriptor # isdatadescriptor
from test.test_support import TestFailed, TESTFN modfile = mod.__file__
import sys, imp, os, string if modfile.endswith('c') or modfile.endswith('o'):
modfile = modfile[:-1]
def test(assertion, message, *args): import __builtin__
if not assertion:
raise TestFailed, message % args
import inspect
file = open(TESTFN, 'w')
file.write(source)
file.close()
# Note that load_source creates file TESTFN+'c' or TESTFN+'o'.
mod = imp.load_source('testmod', TESTFN)
files_to_clean_up = [TESTFN, TESTFN + 'c', TESTFN + 'o']
def istest(func, exp):
obj = eval(exp)
test(func(obj), '%s(%s)' % (func.__name__, exp))
for other in [inspect.isbuiltin, inspect.isclass, inspect.iscode,
inspect.isframe, inspect.isfunction, inspect.ismethod,
inspect.ismodule, inspect.istraceback]:
if other is not func:
test(not other(obj), 'not %s(%s)' % (other.__name__, exp))
git = mod.StupidGit()
try: try:
1/0 1/0
except: except:
tb = sys.exc_traceback tb = sys.exc_traceback
istest(inspect.isbuiltin, 'sys.exit') git = mod.StupidGit()
istest(inspect.isbuiltin, '[].append')
istest(inspect.isclass, 'mod.StupidGit') class IsTestBase(unittest.TestCase):
istest(inspect.iscode, 'mod.spam.func_code') predicates = set([inspect.isbuiltin, inspect.isclass, inspect.iscode,
istest(inspect.isframe, 'tb.tb_frame') inspect.isframe, inspect.isfunction, inspect.ismethod,
istest(inspect.isfunction, 'mod.spam') inspect.ismodule, inspect.istraceback])
istest(inspect.ismethod, 'mod.StupidGit.abuse')
istest(inspect.ismethod, 'git.argue') def istest(self, predicate, exp):
istest(inspect.ismodule, 'mod') obj = eval(exp)
istest(inspect.istraceback, 'tb') self.failUnless(predicate(obj), '%s(%s)' % (predicate.__name__, exp))
import __builtin__
istest(inspect.isdatadescriptor, '__builtin__.file.closed') for other in self.predicates - set([predicate]):
istest(inspect.isdatadescriptor, '__builtin__.file.softspace') self.failIf(other(obj), 'not %s(%s)' % (other.__name__, exp))
test(inspect.isroutine(mod.spam), 'isroutine(mod.spam)')
test(inspect.isroutine([].count), 'isroutine([].count)') class TestPredicates(IsTestBase):
def test_eleven(self):
classes = inspect.getmembers(mod, inspect.isclass) # Doc/lib/libinspect.tex claims there are 11 such functions
test(classes == count = len(filter(lambda x:x.startswith('is'), dir(inspect)))
[('FesteringGob', mod.FesteringGob), self.assertEqual(count, 11, "There are %d (not 11) is* functions" % count)
('MalodorousPervert', mod.MalodorousPervert),
('ParrotDroppings', mod.ParrotDroppings), def test_excluding_predicates(self):
('StupidGit', mod.StupidGit)], 'class list') self.istest(inspect.isbuiltin, 'sys.exit')
tree = inspect.getclasstree(map(lambda x: x[1], classes), 1) self.istest(inspect.isbuiltin, '[].append')
test(tree == self.istest(inspect.isclass, 'mod.StupidGit')
[(mod.ParrotDroppings, ()), self.istest(inspect.iscode, 'mod.spam.func_code')
(mod.StupidGit, ()), self.istest(inspect.isframe, 'tb.tb_frame')
[(mod.MalodorousPervert, (mod.StupidGit,)), self.istest(inspect.isfunction, 'mod.spam')
[(mod.FesteringGob, (mod.MalodorousPervert, mod.ParrotDroppings)) self.istest(inspect.ismethod, 'mod.StupidGit.abuse')
] self.istest(inspect.ismethod, 'git.argue')
] self.istest(inspect.ismodule, 'mod')
], 'class tree') self.istest(inspect.istraceback, 'tb')
self.istest(inspect.isdatadescriptor, '__builtin__.file.closed')
functions = inspect.getmembers(mod, inspect.isfunction) self.istest(inspect.isdatadescriptor, '__builtin__.file.softspace')
test(functions == [('eggs', mod.eggs), ('spam', mod.spam)], 'function list')
def test_isroutine(self):
test(inspect.getdoc(mod) == 'A module docstring.', 'getdoc(mod)') self.assert_(inspect.isroutine(mod.spam))
test(inspect.getcomments(mod) == '# line 1\n', 'getcomments(mod)') self.assert_(inspect.isroutine([].count))
test(inspect.getmodule(mod.StupidGit) == mod, 'getmodule(mod.StupidGit)')
test(inspect.getfile(mod.StupidGit) == TESTFN, 'getfile(mod.StupidGit)') class TestInterpreterStack(IsTestBase):
test(inspect.getsourcefile(mod.spam) == TESTFN, 'getsourcefile(mod.spam)') def __init__(self, *args, **kwargs):
test(inspect.getsourcefile(git.abuse) == TESTFN, 'getsourcefile(git.abuse)') unittest.TestCase.__init__(self, *args, **kwargs)
def sourcerange(top, bottom): git.abuse(7, 8, 9)
lines = string.split(source, '\n')
return string.join(lines[top-1:bottom], '\n') + '\n' def test_abuse_done(self):
self.istest(inspect.istraceback, 'git.ex[2]')
test(inspect.getsource(git.abuse) == sourcerange(29, 39), self.istest(inspect.isframe, 'mod.fr')
'getsource(git.abuse)')
test(inspect.getsource(mod.StupidGit) == sourcerange(21, 46), def test_stack(self):
'getsource(mod.StupidGit)') self.assert_(len(mod.st) >= 5)
test(inspect.getdoc(mod.StupidGit) == self.assertEqual(mod.st[0][1:],
'A longer,\n\nindented\n\ndocstring.', 'getdoc(mod.StupidGit)') (modfile, 16, 'eggs', [' st = inspect.stack()\n'], 0))
test(inspect.getdoc(git.abuse) == self.assertEqual(mod.st[1][1:],
'Another\n\ndocstring\n\ncontaining\n\ntabs', 'getdoc(git.abuse)') (modfile, 9, 'spam', [' eggs(b + d, c + f)\n'], 0))
test(inspect.getcomments(mod.StupidGit) == '# line 20\n', self.assertEqual(mod.st[2][1:],
'getcomments(mod.StupidGit)') (modfile, 43, 'argue', [' spam(a, b, c)\n'], 0))
self.assertEqual(mod.st[3][1:],
git.abuse(7, 8, 9) (modfile, 39, 'abuse', [' self.argue(a, b, c)\n'], 0))
istest(inspect.istraceback, 'git.ex[2]') def test_trace(self):
istest(inspect.isframe, 'mod.fr') self.assertEqual(len(git.tr), 3)
self.assertEqual(git.tr[0][1:], (modfile, 43, 'argue',
test(len(git.tr) == 3, 'trace() length') [' spam(a, b, c)\n'], 0))
test(git.tr[0][1:] == (TESTFN, 43, 'argue', self.assertEqual(git.tr[1][1:], (modfile, 9, 'spam',
[' spam(a, b, c)\n'], 0), [' eggs(b + d, c + f)\n'], 0))
'trace() row 2') self.assertEqual(git.tr[2][1:], (modfile, 18, 'eggs',
test(git.tr[1][1:] == (TESTFN, 9, 'spam', [' eggs(b + d, c + f)\n'], 0), [' q = y / 0\n'], 0))
'trace() row 2')
test(git.tr[2][1:] == (TESTFN, 18, 'eggs', [' q = y / 0\n'], 0), def test_frame(self):
'trace() row 3') args, varargs, varkw, locals = inspect.getargvalues(mod.fr)
self.assertEqual(args, ['x', 'y'])
test(len(mod.st) >= 5, 'stack() length') self.assertEqual(varargs, None)
test(mod.st[0][1:] == self.assertEqual(varkw, None)
(TESTFN, 16, 'eggs', [' st = inspect.stack()\n'], 0), self.assertEqual(locals, {'x': 11, 'p': 11, 'y': 14})
'stack() row 1') self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals),
test(mod.st[1][1:] == '(x=11, y=14)')
(TESTFN, 9, 'spam', [' eggs(b + d, c + f)\n'], 0),
'stack() row 2') def test_previous_frame(self):
test(mod.st[2][1:] == args, varargs, varkw, locals = inspect.getargvalues(mod.fr.f_back)
(TESTFN, 43, 'argue', [' spam(a, b, c)\n'], 0), self.assertEqual(args, ['a', 'b', 'c', 'd', ['e', ['f']]])
'stack() row 3') self.assertEqual(varargs, 'g')
test(mod.st[3][1:] == self.assertEqual(varkw, 'h')
(TESTFN, 39, 'abuse', [' self.argue(a, b, c)\n'], 0), self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals),
'stack() row 4') '(a=7, b=8, c=9, d=3, (e=4, (f=5,)), *g=(), **h={})')
args, varargs, varkw, locals = inspect.getargvalues(mod.fr) class GetSourceBase(unittest.TestCase):
test(args == ['x', 'y'], 'mod.fr args') # Subclasses must override.
test(varargs == None, 'mod.fr varargs') fodderFile = None
test(varkw == None, 'mod.fr varkw')
test(locals == {'x': 11, 'p': 11, 'y': 14}, 'mod.fr locals') def __init__(self, *args, **kwargs):
test(inspect.formatargvalues(args, varargs, varkw, locals) == unittest.TestCase.__init__(self, *args, **kwargs)
'(x=11, y=14)', 'mod.fr formatted argvalues')
self.source = file(inspect.getsourcefile(self.fodderFile)).read()
args, varargs, varkw, locals = inspect.getargvalues(mod.fr.f_back)
test(args == ['a', 'b', 'c', 'd', ['e', ['f']]], 'mod.fr.f_back args') def sourcerange(self, top, bottom):
test(varargs == 'g', 'mod.fr.f_back varargs') lines = self.source.split("\n")
test(varkw == 'h', 'mod.fr.f_back varkw') return "\n".join(lines[top-1:bottom]) + "\n"
test(inspect.formatargvalues(args, varargs, varkw, locals) ==
'(a=7, b=8, c=9, d=3, (e=4, (f=5,)), *g=(), **h={})', def assertSourceEqual(self, obj, top, bottom):
'mod.fr.f_back formatted argvalues') self.assertEqual(inspect.getsource(obj),
self.sourcerange(top, bottom))
for fname in files_to_clean_up:
try: class TestRetrievingSourceCode(GetSourceBase):
os.unlink(fname) fodderFile = mod
except:
pass def test_getclasses(self):
classes = inspect.getmembers(mod, inspect.isclass)
# Test for decorators as well. self.assertEqual(classes,
[('FesteringGob', mod.FesteringGob),
source = r""" ('MalodorousPervert', mod.MalodorousPervert),
def wrap(foo=None): ('ParrotDroppings', mod.ParrotDroppings),
def wrapper(func): ('StupidGit', mod.StupidGit)])
return func tree = inspect.getclasstree([cls[1] for cls in classes], 1)
return wrapper self.assertEqual(tree,
[(mod.ParrotDroppings, ()),
def replace(func): (mod.StupidGit, ()),
def insteadfunc(): [(mod.MalodorousPervert, (mod.StupidGit,)),
print 'hello' [(mod.FesteringGob, (mod.MalodorousPervert,
return insteadfunc mod.ParrotDroppings))
]
# two decorators, one with argument ]
@wrap() ])
@wrap(wrap)
def wrapped(): def test_getfunctions(self):
pass functions = inspect.getmembers(mod, inspect.isfunction)
self.assertEqual(functions, [('eggs', mod.eggs),
@replace ('spam', mod.spam)])
def gone():
pass""" def test_getdoc(self):
self.assertEqual(inspect.getdoc(mod), 'A module docstring.')
file = open(TESTFN + "2", "w") self.assertEqual(inspect.getdoc(mod.StupidGit),
file.write(source) 'A longer,\n\nindented\n\ndocstring.')
file.close() self.assertEqual(inspect.getdoc(git.abuse),
files_to_clean_up = [TESTFN + "2", TESTFN + '2c', TESTFN + '2o'] 'Another\n\ndocstring\n\ncontaining\n\ntabs')
mod2 = imp.load_source("testmod3", TESTFN + "2") def test_getcomments(self):
self.assertEqual(inspect.getcomments(mod), '# line 1\n')
test(inspect.getsource(mod2.wrapped) == sourcerange(13, 16), self.assertEqual(inspect.getcomments(mod.StupidGit), '# line 20\n')
"inspect.getsource(mod.wrapped)")
test(inspect.getsource(mod2.gone) == sourcerange(8, 9), def test_getmodule(self):
"inspect.getsource(mod.gone)") self.assertEqual(inspect.getmodule(mod.StupidGit), mod)
for fname in files_to_clean_up: def test_getsource(self):
try: self.assertSourceEqual(git.abuse, 29, 39)
os.unlink(fname) self.assertSourceEqual(mod.StupidGit, 21, 46)
except:
pass def test_getsourcefile(self):
self.assertEqual(inspect.getsourcefile(mod.spam), modfile)
# Test classic-class method resolution order. self.assertEqual(inspect.getsourcefile(git.abuse), modfile)
class A: pass
class B(A): pass def test_getfile(self):
class C(A): pass self.assertEqual(inspect.getfile(mod.StupidGit), mod.__file__)
class D(B, C): pass
class TestDecorators(GetSourceBase):
expected = (D, B, A, C) fodderFile = mod2
got = inspect.getmro(D)
test(expected == got, "expected %r mro, got %r", expected, got) def test_wrapped_decorator(self):
self.assertSourceEqual(mod2.wrapped, 14, 17)
# The same w/ new-class MRO.
class A(object): pass def test_replacing_decorator(self):
class B(A): pass self.assertSourceEqual(mod2.gone, 9, 10)
class C(A): pass
class D(B, C): pass # Helper for testing classify_class_attrs.
expected = (D, B, C, A, object)
got = inspect.getmro(D)
test(expected == got, "expected %r mro, got %r", expected, got)
# Test classify_class_attrs.
def attrs_wo_objs(cls): def attrs_wo_objs(cls):
return [t[:3] for t in inspect.classify_class_attrs(cls)] return [t[:3] for t in inspect.classify_class_attrs(cls)]
class A: class TestClassesAndFunctions(unittest.TestCase):
def s(): pass def test_classic_mro(self):
s = staticmethod(s) # Test classic-class method resolution order.
class A: pass
class B(A): pass
class C(A): pass
class D(B, C): pass
expected = (D, B, A, C)
got = inspect.getmro(D)
self.assertEqual(expected, got)
def test_newstyle_mro(self):
# The same w/ new-class MRO.
class A(object): pass
class B(A): pass
class C(A): pass
class D(B, C): pass
expected = (D, B, C, A, object)
got = inspect.getmro(D)
self.assertEqual(expected, got)
def assertArgSpecEquals(self, routine, args_e, varargs_e = None,
varkw_e = None, defaults_e = None,
formatted = None):
args, varargs, varkw, defaults = inspect.getargspec(routine)
self.assertEqual(args, args_e)
self.assertEqual(varargs, varargs_e)
self.assertEqual(varkw, varkw_e)
self.assertEqual(defaults, defaults_e)
if formatted is not None:
self.assertEqual(inspect.formatargspec(args, varargs, varkw, defaults),
formatted)
def test_getargspec(self):
self.assertArgSpecEquals(mod.eggs, ['x', 'y'], formatted = '(x, y)')
self.assertArgSpecEquals(mod.spam,
['a', 'b', 'c', 'd', ['e', ['f']]],
'g', 'h', (3, (4, (5,))),
'(a, b, c, d=3, (e, (f,))=(4, (5,)), *g, **h)')
def test_getargspec_method(self):
class A(object):
def m(self):
pass
self.assertArgSpecEquals(A.m, ['self'])
def test_getargspec_sublistofone(self):
def sublistOfOne((foo)): return 1
self.assertArgSpecEquals(sublistOfOne, [['foo']])
def test_classify_oldstyle(self):
class A:
def s(): pass
s = staticmethod(s)
def c(cls): pass
c = classmethod(c)
def getp(self): pass
p = property(getp)
def m(self): pass
def m1(self): pass
datablob = '1'
attrs = attrs_wo_objs(A)
self.assert_(('s', 'static method', A) in attrs, 'missing static method')
self.assert_(('c', 'class method', A) in attrs, 'missing class method')
self.assert_(('p', 'property', A) in attrs, 'missing property')
self.assert_(('m', 'method', A) in attrs, 'missing plain method')
self.assert_(('m1', 'method', A) in attrs, 'missing plain method')
self.assert_(('datablob', 'data', A) in attrs, 'missing data')
class B(A):
def m(self): pass
attrs = attrs_wo_objs(B)
self.assert_(('s', 'static method', A) in attrs, 'missing static method')
self.assert_(('c', 'class method', A) in attrs, 'missing class method')
self.assert_(('p', 'property', A) in attrs, 'missing property')
self.assert_(('m', 'method', B) in attrs, 'missing plain method')
self.assert_(('m1', 'method', A) in attrs, 'missing plain method')
self.assert_(('datablob', 'data', A) in attrs, 'missing data')
class C(A):
def m(self): pass
def c(self): pass
attrs = attrs_wo_objs(C)
self.assert_(('s', 'static method', A) in attrs, 'missing static method')
self.assert_(('c', 'method', C) in attrs, 'missing plain method')
self.assert_(('p', 'property', A) in attrs, 'missing property')
self.assert_(('m', 'method', C) in attrs, 'missing plain method')
self.assert_(('m1', 'method', A) in attrs, 'missing plain method')
self.assert_(('datablob', 'data', A) in attrs, 'missing data')
class D(B, C):
def m1(self): pass
attrs = attrs_wo_objs(D)
self.assert_(('s', 'static method', A) in attrs, 'missing static method')
self.assert_(('c', 'class method', A) in attrs, 'missing class method')
self.assert_(('p', 'property', A) in attrs, 'missing property')
self.assert_(('m', 'method', B) in attrs, 'missing plain method')
self.assert_(('m1', 'method', D) in attrs, 'missing plain method')
self.assert_(('datablob', 'data', A) in attrs, 'missing data')
# Repeat all that, but w/ new-style classes.
def test_classify_newstyle(self):
class A(object):
def s(): pass
s = staticmethod(s)
def c(cls): pass
c = classmethod(c)
def getp(self): pass
p = property(getp)
def m(self): pass
def m1(self): pass
datablob = '1'
attrs = attrs_wo_objs(A)
self.assert_(('s', 'static method', A) in attrs, 'missing static method')
self.assert_(('c', 'class method', A) in attrs, 'missing class method')
self.assert_(('p', 'property', A) in attrs, 'missing property')
self.assert_(('m', 'method', A) in attrs, 'missing plain method')
self.assert_(('m1', 'method', A) in attrs, 'missing plain method')
self.assert_(('datablob', 'data', A) in attrs, 'missing data')
def c(cls): pass class B(A):
c = classmethod(c)
def getp(self): pass def m(self): pass
p = property(getp)
def m(self): pass attrs = attrs_wo_objs(B)
self.assert_(('s', 'static method', A) in attrs, 'missing static method')
self.assert_(('c', 'class method', A) in attrs, 'missing class method')
self.assert_(('p', 'property', A) in attrs, 'missing property')
self.assert_(('m', 'method', B) in attrs, 'missing plain method')
self.assert_(('m1', 'method', A) in attrs, 'missing plain method')
self.assert_(('datablob', 'data', A) in attrs, 'missing data')
def m1(self): pass
datablob = '1' class C(A):
attrs = attrs_wo_objs(A) def m(self): pass
test(('s', 'static method', A) in attrs, 'missing static method') def c(self): pass
test(('c', 'class method', A) in attrs, 'missing class method')
test(('p', 'property', A) in attrs, 'missing property')
test(('m', 'method', A) in attrs, 'missing plain method')
test(('m1', 'method', A) in attrs, 'missing plain method')
test(('datablob', 'data', A) in attrs, 'missing data')
class B(A): attrs = attrs_wo_objs(C)
def m(self): pass self.assert_(('s', 'static method', A) in attrs, 'missing static method')
self.assert_(('c', 'method', C) in attrs, 'missing plain method')
self.assert_(('p', 'property', A) in attrs, 'missing property')
self.assert_(('m', 'method', C) in attrs, 'missing plain method')
self.assert_(('m1', 'method', A) in attrs, 'missing plain method')
self.assert_(('datablob', 'data', A) in attrs, 'missing data')
attrs = attrs_wo_objs(B) class D(B, C):
test(('s', 'static method', A) in attrs, 'missing static method')
test(('c', 'class method', A) in attrs, 'missing class method')
test(('p', 'property', A) in attrs, 'missing property')
test(('m', 'method', B) in attrs, 'missing plain method')
test(('m1', 'method', A) in attrs, 'missing plain method')
test(('datablob', 'data', A) in attrs, 'missing data')
def m1(self): pass
attrs = attrs_wo_objs(D)
self.assert_(('s', 'static method', A) in attrs, 'missing static method')
self.assert_(('c', 'method', C) in attrs, 'missing plain method')
self.assert_(('p', 'property', A) in attrs, 'missing property')
self.assert_(('m', 'method', B) in attrs, 'missing plain method')
self.assert_(('m1', 'method', D) in attrs, 'missing plain method')
self.assert_(('datablob', 'data', A) in attrs, 'missing data')
class C(A): def test_main():
def m(self): pass run_unittest(TestDecorators, TestRetrievingSourceCode,
def c(self): pass TestInterpreterStack, TestClassesAndFunctions, TestPredicates)
attrs = attrs_wo_objs(C) if __name__ == "__main__":
test(('s', 'static method', A) in attrs, 'missing static method') test_main()
test(('c', 'method', C) in attrs, 'missing plain method')
test(('p', 'property', A) in attrs, 'missing property')
test(('m', 'method', C) in attrs, 'missing plain method')
test(('m1', 'method', A) in attrs, 'missing plain method')
test(('datablob', 'data', A) in attrs, 'missing data')
class D(B, C):
def m1(self): pass
attrs = attrs_wo_objs(D)
test(('s', 'static method', A) in attrs, 'missing static method')
test(('c', 'class method', A) in attrs, 'missing class method')
test(('p', 'property', A) in attrs, 'missing property')
test(('m', 'method', B) in attrs, 'missing plain method')
test(('m1', 'method', D) in attrs, 'missing plain method')
test(('datablob', 'data', A) in attrs, 'missing data')
# Repeat all that, but w/ new-style classes.
class A(object):
def s(): pass
s = staticmethod(s)
def c(cls): pass
c = classmethod(c)
def getp(self): pass
p = property(getp)
def m(self): pass
def m1(self): pass
datablob = '1'
attrs = attrs_wo_objs(A)
test(('s', 'static method', A) in attrs, 'missing static method')
test(('c', 'class method', A) in attrs, 'missing class method')
test(('p', 'property', A) in attrs, 'missing property')
test(('m', 'method', A) in attrs, 'missing plain method')
test(('m1', 'method', A) in attrs, 'missing plain method')
test(('datablob', 'data', A) in attrs, 'missing data')
class B(A):
def m(self): pass
attrs = attrs_wo_objs(B)
test(('s', 'static method', A) in attrs, 'missing static method')
test(('c', 'class method', A) in attrs, 'missing class method')
test(('p', 'property', A) in attrs, 'missing property')
test(('m', 'method', B) in attrs, 'missing plain method')
test(('m1', 'method', A) in attrs, 'missing plain method')
test(('datablob', 'data', A) in attrs, 'missing data')
class C(A):
def m(self): pass
def c(self): pass
attrs = attrs_wo_objs(C)
test(('s', 'static method', A) in attrs, 'missing static method')
test(('c', 'method', C) in attrs, 'missing plain method')
test(('p', 'property', A) in attrs, 'missing property')
test(('m', 'method', C) in attrs, 'missing plain method')
test(('m1', 'method', A) in attrs, 'missing plain method')
test(('datablob', 'data', A) in attrs, 'missing data')
class D(B, C):
def m1(self): pass
attrs = attrs_wo_objs(D)
test(('s', 'static method', A) in attrs, 'missing static method')
test(('c', 'method', C) in attrs, 'missing plain method')
test(('p', 'property', A) in attrs, 'missing property')
test(('m', 'method', B) in attrs, 'missing plain method')
test(('m1', 'method', D) in attrs, 'missing plain method')
test(('datablob', 'data', A) in attrs, 'missing data')
args, varargs, varkw, defaults = inspect.getargspec(mod.eggs)
test(args == ['x', 'y'], 'mod.eggs args')
test(varargs == None, 'mod.eggs varargs')
test(varkw == None, 'mod.eggs varkw')
test(defaults == None, 'mod.eggs defaults')
test(inspect.formatargspec(args, varargs, varkw, defaults) ==
'(x, y)', 'mod.eggs formatted argspec')
args, varargs, varkw, defaults = inspect.getargspec(mod.spam)
test(args == ['a', 'b', 'c', 'd', ['e', ['f']]], 'mod.spam args')
test(varargs == 'g', 'mod.spam varargs')
test(varkw == 'h', 'mod.spam varkw')
test(defaults == (3, (4, (5,))), 'mod.spam defaults')
test(inspect.formatargspec(args, varargs, varkw, defaults) ==
'(a, b, c, d=3, (e, (f,))=(4, (5,)), *g, **h)',
'mod.spam formatted argspec')
args, varargs, varkw, defaults = inspect.getargspec(A.m)
test(args == ['self'], 'A.m args')
test(varargs is None, 'A.m varargs')
test(varkw is None, 'A.m varkw')
test(defaults is None, 'A.m defaults')
# Doc/lib/libinspect.tex claims there are 11 such functions
count = len(filter(lambda x:x.startswith('is'), dir(inspect)))
test(count == 11, "There are %d (not 11) is* functions", count)
def sublistOfOne((foo)): return 1
args, varargs, varkw, defaults = inspect.getargspec(sublistOfOne)
test(args == [['foo']], 'sublistOfOne args')
test(varargs is None, 'sublistOfOne varargs')
test(varkw is None, 'sublistOfOne varkw')
test(defaults is None, 'sublistOfOn defaults')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment