2004-12-12 12:20:22 -04:00
|
|
|
import sys
|
|
|
|
import unittest
|
|
|
|
import inspect
|
2001-02-27 10:43:21 -04:00
|
|
|
|
2004-12-12 12:20:22 -04:00
|
|
|
from test.test_support import TESTFN, run_unittest
|
2001-02-27 10:43:21 -04:00
|
|
|
|
2004-12-12 12:20:22 -04:00
|
|
|
from test import inspect_fodder as mod
|
|
|
|
from test import inspect_fodder2 as mod2
|
2001-02-27 10:43:21 -04:00
|
|
|
|
|
|
|
# Functions tested in this suite:
|
|
|
|
# ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode,
|
|
|
|
# isbuiltin, isroutine, getmembers, getdoc, getfile, getmodule,
|
|
|
|
# getsourcefile, getcomments, getsource, getclasstree, getargspec,
|
|
|
|
# getargvalues, formatargspec, formatargvalues, currentframe, stack, trace
|
2003-05-03 06:09:02 -03:00
|
|
|
# isdatadescriptor
|
2001-02-27 10:43:21 -04:00
|
|
|
|
2004-12-12 12:20:22 -04:00
|
|
|
modfile = mod.__file__
|
|
|
|
if modfile.endswith('c') or modfile.endswith('o'):
|
|
|
|
modfile = modfile[:-1]
|
2001-02-27 10:43:21 -04:00
|
|
|
|
2004-12-12 12:20:22 -04:00
|
|
|
import __builtin__
|
2001-02-27 10:43:21 -04:00
|
|
|
|
|
|
|
try:
|
|
|
|
1/0
|
|
|
|
except:
|
|
|
|
tb = sys.exc_traceback
|
|
|
|
|
2004-12-12 12:20:22 -04:00
|
|
|
git = mod.StupidGit()
|
|
|
|
|
|
|
|
class IsTestBase(unittest.TestCase):
|
|
|
|
predicates = set([inspect.isbuiltin, inspect.isclass, inspect.iscode,
|
|
|
|
inspect.isframe, inspect.isfunction, inspect.ismethod,
|
|
|
|
inspect.ismodule, inspect.istraceback])
|
2005-01-07 12:01:32 -04:00
|
|
|
|
2004-12-12 12:20:22 -04:00
|
|
|
def istest(self, predicate, exp):
|
|
|
|
obj = eval(exp)
|
|
|
|
self.failUnless(predicate(obj), '%s(%s)' % (predicate.__name__, exp))
|
2005-01-07 12:01:32 -04:00
|
|
|
|
2004-12-12 12:20:22 -04:00
|
|
|
for other in self.predicates - set([predicate]):
|
|
|
|
self.failIf(other(obj), 'not %s(%s)' % (other.__name__, exp))
|
|
|
|
|
|
|
|
class TestPredicates(IsTestBase):
|
|
|
|
def test_eleven(self):
|
|
|
|
# Doc/lib/libinspect.tex claims there are 11 such functions
|
|
|
|
count = len(filter(lambda x:x.startswith('is'), dir(inspect)))
|
|
|
|
self.assertEqual(count, 11, "There are %d (not 11) is* functions" % count)
|
2005-01-07 12:01:32 -04:00
|
|
|
|
2004-12-12 12:20:22 -04:00
|
|
|
def test_excluding_predicates(self):
|
|
|
|
self.istest(inspect.isbuiltin, 'sys.exit')
|
|
|
|
self.istest(inspect.isbuiltin, '[].append')
|
|
|
|
self.istest(inspect.isclass, 'mod.StupidGit')
|
|
|
|
self.istest(inspect.iscode, 'mod.spam.func_code')
|
|
|
|
self.istest(inspect.isframe, 'tb.tb_frame')
|
|
|
|
self.istest(inspect.isfunction, 'mod.spam')
|
|
|
|
self.istest(inspect.ismethod, 'mod.StupidGit.abuse')
|
|
|
|
self.istest(inspect.ismethod, 'git.argue')
|
|
|
|
self.istest(inspect.ismodule, 'mod')
|
|
|
|
self.istest(inspect.istraceback, 'tb')
|
|
|
|
self.istest(inspect.isdatadescriptor, '__builtin__.file.closed')
|
|
|
|
self.istest(inspect.isdatadescriptor, '__builtin__.file.softspace')
|
|
|
|
|
|
|
|
def test_isroutine(self):
|
|
|
|
self.assert_(inspect.isroutine(mod.spam))
|
|
|
|
self.assert_(inspect.isroutine([].count))
|
|
|
|
|
|
|
|
class TestInterpreterStack(IsTestBase):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
unittest.TestCase.__init__(self, *args, **kwargs)
|
2005-01-07 12:01:32 -04:00
|
|
|
|
2004-12-12 12:20:22 -04:00
|
|
|
git.abuse(7, 8, 9)
|
|
|
|
|
|
|
|
def test_abuse_done(self):
|
|
|
|
self.istest(inspect.istraceback, 'git.ex[2]')
|
|
|
|
self.istest(inspect.isframe, 'mod.fr')
|
|
|
|
|
|
|
|
def test_stack(self):
|
|
|
|
self.assert_(len(mod.st) >= 5)
|
|
|
|
self.assertEqual(mod.st[0][1:],
|
2005-01-07 12:01:32 -04:00
|
|
|
(modfile, 16, 'eggs', [' st = inspect.stack()\n'], 0))
|
2004-12-12 12:20:22 -04:00
|
|
|
self.assertEqual(mod.st[1][1:],
|
|
|
|
(modfile, 9, 'spam', [' eggs(b + d, c + f)\n'], 0))
|
|
|
|
self.assertEqual(mod.st[2][1:],
|
|
|
|
(modfile, 43, 'argue', [' spam(a, b, c)\n'], 0))
|
|
|
|
self.assertEqual(mod.st[3][1:],
|
|
|
|
(modfile, 39, 'abuse', [' self.argue(a, b, c)\n'], 0))
|
|
|
|
|
|
|
|
def test_trace(self):
|
|
|
|
self.assertEqual(len(git.tr), 3)
|
|
|
|
self.assertEqual(git.tr[0][1:], (modfile, 43, 'argue',
|
|
|
|
[' spam(a, b, c)\n'], 0))
|
|
|
|
self.assertEqual(git.tr[1][1:], (modfile, 9, 'spam',
|
|
|
|
[' eggs(b + d, c + f)\n'], 0))
|
|
|
|
self.assertEqual(git.tr[2][1:], (modfile, 18, 'eggs',
|
|
|
|
[' q = y / 0\n'], 0))
|
|
|
|
|
|
|
|
def test_frame(self):
|
|
|
|
args, varargs, varkw, locals = inspect.getargvalues(mod.fr)
|
|
|
|
self.assertEqual(args, ['x', 'y'])
|
|
|
|
self.assertEqual(varargs, None)
|
|
|
|
self.assertEqual(varkw, None)
|
|
|
|
self.assertEqual(locals, {'x': 11, 'p': 11, 'y': 14})
|
|
|
|
self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals),
|
|
|
|
'(x=11, y=14)')
|
|
|
|
|
|
|
|
def test_previous_frame(self):
|
|
|
|
args, varargs, varkw, locals = inspect.getargvalues(mod.fr.f_back)
|
|
|
|
self.assertEqual(args, ['a', 'b', 'c', 'd', ['e', ['f']]])
|
|
|
|
self.assertEqual(varargs, 'g')
|
|
|
|
self.assertEqual(varkw, 'h')
|
|
|
|
self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals),
|
|
|
|
'(a=7, b=8, c=9, d=3, (e=4, (f=5,)), *g=(), **h={})')
|
|
|
|
|
|
|
|
class GetSourceBase(unittest.TestCase):
|
|
|
|
# Subclasses must override.
|
|
|
|
fodderFile = None
|
2005-01-07 12:01:32 -04:00
|
|
|
|
2004-12-12 12:20:22 -04:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
unittest.TestCase.__init__(self, *args, **kwargs)
|
|
|
|
|
|
|
|
self.source = file(inspect.getsourcefile(self.fodderFile)).read()
|
|
|
|
|
|
|
|
def sourcerange(self, top, bottom):
|
|
|
|
lines = self.source.split("\n")
|
|
|
|
return "\n".join(lines[top-1:bottom]) + "\n"
|
|
|
|
|
|
|
|
def assertSourceEqual(self, obj, top, bottom):
|
|
|
|
self.assertEqual(inspect.getsource(obj),
|
|
|
|
self.sourcerange(top, bottom))
|
2005-01-07 12:01:32 -04:00
|
|
|
|
2004-12-12 12:20:22 -04:00
|
|
|
class TestRetrievingSourceCode(GetSourceBase):
|
|
|
|
fodderFile = mod
|
2005-01-07 12:01:32 -04:00
|
|
|
|
2004-12-12 12:20:22 -04:00
|
|
|
def test_getclasses(self):
|
|
|
|
classes = inspect.getmembers(mod, inspect.isclass)
|
|
|
|
self.assertEqual(classes,
|
|
|
|
[('FesteringGob', mod.FesteringGob),
|
|
|
|
('MalodorousPervert', mod.MalodorousPervert),
|
|
|
|
('ParrotDroppings', mod.ParrotDroppings),
|
|
|
|
('StupidGit', mod.StupidGit)])
|
|
|
|
tree = inspect.getclasstree([cls[1] for cls in classes], 1)
|
|
|
|
self.assertEqual(tree,
|
2006-04-15 06:13:19 -03:00
|
|
|
[(object, ()),
|
|
|
|
[(mod.ParrotDroppings, (object,)),
|
|
|
|
(mod.StupidGit, (object,)),
|
|
|
|
[(mod.MalodorousPervert, (mod.StupidGit,)),
|
|
|
|
[(mod.FesteringGob, (mod.MalodorousPervert,
|
|
|
|
mod.ParrotDroppings))
|
|
|
|
]
|
2004-12-12 12:20:22 -04:00
|
|
|
]
|
|
|
|
]
|
|
|
|
])
|
2005-01-07 12:01:32 -04:00
|
|
|
|
2004-12-12 12:20:22 -04:00
|
|
|
def test_getfunctions(self):
|
|
|
|
functions = inspect.getmembers(mod, inspect.isfunction)
|
|
|
|
self.assertEqual(functions, [('eggs', mod.eggs),
|
|
|
|
('spam', mod.spam)])
|
|
|
|
|
|
|
|
def test_getdoc(self):
|
|
|
|
self.assertEqual(inspect.getdoc(mod), 'A module docstring.')
|
|
|
|
self.assertEqual(inspect.getdoc(mod.StupidGit),
|
|
|
|
'A longer,\n\nindented\n\ndocstring.')
|
|
|
|
self.assertEqual(inspect.getdoc(git.abuse),
|
|
|
|
'Another\n\ndocstring\n\ncontaining\n\ntabs')
|
|
|
|
|
|
|
|
def test_getcomments(self):
|
|
|
|
self.assertEqual(inspect.getcomments(mod), '# line 1\n')
|
|
|
|
self.assertEqual(inspect.getcomments(mod.StupidGit), '# line 20\n')
|
|
|
|
|
|
|
|
def test_getmodule(self):
|
|
|
|
self.assertEqual(inspect.getmodule(mod.StupidGit), mod)
|
|
|
|
|
|
|
|
def test_getsource(self):
|
|
|
|
self.assertSourceEqual(git.abuse, 29, 39)
|
|
|
|
self.assertSourceEqual(mod.StupidGit, 21, 46)
|
|
|
|
|
|
|
|
def test_getsourcefile(self):
|
2005-01-07 12:01:32 -04:00
|
|
|
self.assertEqual(inspect.getsourcefile(mod.spam), modfile)
|
|
|
|
self.assertEqual(inspect.getsourcefile(git.abuse), modfile)
|
2004-12-12 12:20:22 -04:00
|
|
|
|
|
|
|
def test_getfile(self):
|
|
|
|
self.assertEqual(inspect.getfile(mod.StupidGit), mod.__file__)
|
|
|
|
|
|
|
|
class TestDecorators(GetSourceBase):
|
|
|
|
fodderFile = mod2
|
|
|
|
|
|
|
|
def test_wrapped_decorator(self):
|
|
|
|
self.assertSourceEqual(mod2.wrapped, 14, 17)
|
|
|
|
|
|
|
|
def test_replacing_decorator(self):
|
|
|
|
self.assertSourceEqual(mod2.gone, 9, 10)
|
|
|
|
|
2004-12-12 12:46:28 -04:00
|
|
|
class TestOneliners(GetSourceBase):
|
|
|
|
fodderFile = mod2
|
|
|
|
def test_oneline_lambda(self):
|
|
|
|
# Test inspect.getsource with a one-line lambda function.
|
|
|
|
self.assertSourceEqual(mod2.oll, 25, 25)
|
2005-01-07 12:01:32 -04:00
|
|
|
|
2004-12-12 12:46:28 -04:00
|
|
|
def test_threeline_lambda(self):
|
|
|
|
# Test inspect.getsource with a three-line lambda function,
|
|
|
|
# where the second and third lines are _not_ indented.
|
2005-01-07 12:01:32 -04:00
|
|
|
self.assertSourceEqual(mod2.tll, 28, 30)
|
|
|
|
|
2004-12-12 12:46:28 -04:00
|
|
|
def test_twoline_indented_lambda(self):
|
|
|
|
# Test inspect.getsource with a two-line lambda function,
|
|
|
|
# where the second line _is_ indented.
|
|
|
|
self.assertSourceEqual(mod2.tlli, 33, 34)
|
2005-01-07 12:01:32 -04:00
|
|
|
|
2004-12-12 12:46:28 -04:00
|
|
|
def test_onelinefunc(self):
|
|
|
|
# Test inspect.getsource with a regular one-line function.
|
|
|
|
self.assertSourceEqual(mod2.onelinefunc, 37, 37)
|
2005-01-07 12:01:32 -04:00
|
|
|
|
2004-12-12 12:46:28 -04:00
|
|
|
def test_manyargs(self):
|
|
|
|
# Test inspect.getsource with a regular function where
|
|
|
|
# the arguments are on two lines and _not_ indented and
|
|
|
|
# the body on the second line with the last arguments.
|
|
|
|
self.assertSourceEqual(mod2.manyargs, 40, 41)
|
2005-01-07 12:01:32 -04:00
|
|
|
|
2004-12-12 12:46:28 -04:00
|
|
|
def test_twolinefunc(self):
|
|
|
|
# Test inspect.getsource with a regular function where
|
|
|
|
# the body is on two lines, following the argument list and
|
|
|
|
# continued on the next line by a \\.
|
|
|
|
self.assertSourceEqual(mod2.twolinefunc, 44, 45)
|
2005-01-07 12:01:32 -04:00
|
|
|
|
2004-12-12 12:46:28 -04:00
|
|
|
def test_lambda_in_list(self):
|
|
|
|
# Test inspect.getsource with a one-line lambda function
|
|
|
|
# defined in a list, indented.
|
|
|
|
self.assertSourceEqual(mod2.a[1], 49, 49)
|
2005-01-07 12:01:32 -04:00
|
|
|
|
2004-12-12 12:46:28 -04:00
|
|
|
def test_anonymous(self):
|
|
|
|
# Test inspect.getsource with a lambda function defined
|
|
|
|
# as argument to another function.
|
|
|
|
self.assertSourceEqual(mod2.anonymous, 55, 55)
|
|
|
|
|
2005-03-12 12:37:11 -04:00
|
|
|
class TestBuggyCases(GetSourceBase):
|
|
|
|
fodderFile = mod2
|
|
|
|
|
|
|
|
def test_with_comment(self):
|
|
|
|
self.assertSourceEqual(mod2.with_comment, 58, 59)
|
|
|
|
|
|
|
|
def test_multiline_sig(self):
|
|
|
|
self.assertSourceEqual(mod2.multiline_sig[0], 63, 64)
|
|
|
|
|
2005-09-25 08:45:45 -03:00
|
|
|
def test_nested_class(self):
|
|
|
|
self.assertSourceEqual(mod2.func69().func71, 71, 72)
|
|
|
|
|
|
|
|
def test_one_liner_followed_by_non_name(self):
|
|
|
|
self.assertSourceEqual(mod2.func77, 77, 77)
|
|
|
|
|
|
|
|
def test_one_liner_dedent_non_name(self):
|
|
|
|
self.assertSourceEqual(mod2.cls82.func83, 83, 83)
|
|
|
|
|
|
|
|
def test_with_comment_instead_of_docstring(self):
|
|
|
|
self.assertSourceEqual(mod2.func88, 88, 90)
|
|
|
|
|
2004-12-12 12:20:22 -04:00
|
|
|
# Helper for testing classify_class_attrs.
|
New function classify_class_attrs(). As a number of SF bug reports
point out, pydoc doesn't tell you where class attributes were defined,
gets several new 2.2 features wrong, and isn't aware of some new features
checked in on Thursday <wink>. pydoc is hampered in part because
inspect.py has the same limitations. Alas, I can't think of a way to
fix this within the current architecture of inspect/pydoc: it's simply
not possible in 2.2 to figure out everything needed just from examining
the object you get back from class.attr. You also need the class
context, and the method resolution order, and tests against various things
that simply didn't exist before. OTOH, knowledge of how to do that is
getting quite complex, so doesn't belong in pydoc.
classify_class_attrs takes a different approach, analyzing all
the class attrs "at once", and returning the most interesting stuff for
each, all in one gulp. pydoc needs to be reworked to use this for
classes (instead of the current "filter dir(class) umpteen times against
assorted predicates" approach).
2001-09-22 23:00:29 -03:00
|
|
|
def attrs_wo_objs(cls):
|
|
|
|
return [t[:3] for t in inspect.classify_class_attrs(cls)]
|
|
|
|
|
2005-01-07 12:01:32 -04:00
|
|
|
class TestClassesAndFunctions(unittest.TestCase):
|
2004-12-12 12:20:22 -04:00
|
|
|
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):
|
2006-04-21 07:40:58 -03:00
|
|
|
def sublistOfOne((foo,)): return 1
|
2004-12-12 12:20:22 -04:00
|
|
|
self.assertArgSpecEquals(sublistOfOne, [['foo']])
|
|
|
|
|
2006-04-21 07:40:58 -03:00
|
|
|
def fakeSublistOfOne((foo)): return 1
|
|
|
|
self.assertArgSpecEquals(fakeSublistOfOne, ['foo'])
|
|
|
|
|
2004-12-12 12:20:22 -04:00
|
|
|
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')
|
New function classify_class_attrs(). As a number of SF bug reports
point out, pydoc doesn't tell you where class attributes were defined,
gets several new 2.2 features wrong, and isn't aware of some new features
checked in on Thursday <wink>. pydoc is hampered in part because
inspect.py has the same limitations. Alas, I can't think of a way to
fix this within the current architecture of inspect/pydoc: it's simply
not possible in 2.2 to figure out everything needed just from examining
the object you get back from class.attr. You also need the class
context, and the method resolution order, and tests against various things
that simply didn't exist before. OTOH, knowledge of how to do that is
getting quite complex, so doesn't belong in pydoc.
classify_class_attrs takes a different approach, analyzing all
the class attrs "at once", and returning the most interesting stuff for
each, all in one gulp. pydoc needs to be reworked to use this for
classes (instead of the current "filter dir(class) umpteen times against
assorted predicates" approach).
2001-09-22 23:00:29 -03:00
|
|
|
|
2004-12-12 12:20:22 -04:00
|
|
|
class B(A):
|
New function classify_class_attrs(). As a number of SF bug reports
point out, pydoc doesn't tell you where class attributes were defined,
gets several new 2.2 features wrong, and isn't aware of some new features
checked in on Thursday <wink>. pydoc is hampered in part because
inspect.py has the same limitations. Alas, I can't think of a way to
fix this within the current architecture of inspect/pydoc: it's simply
not possible in 2.2 to figure out everything needed just from examining
the object you get back from class.attr. You also need the class
context, and the method resolution order, and tests against various things
that simply didn't exist before. OTOH, knowledge of how to do that is
getting quite complex, so doesn't belong in pydoc.
classify_class_attrs takes a different approach, analyzing all
the class attrs "at once", and returning the most interesting stuff for
each, all in one gulp. pydoc needs to be reworked to use this for
classes (instead of the current "filter dir(class) umpteen times against
assorted predicates" approach).
2001-09-22 23:00:29 -03:00
|
|
|
|
2004-12-12 12:20:22 -04:00
|
|
|
def m(self): pass
|
New function classify_class_attrs(). As a number of SF bug reports
point out, pydoc doesn't tell you where class attributes were defined,
gets several new 2.2 features wrong, and isn't aware of some new features
checked in on Thursday <wink>. pydoc is hampered in part because
inspect.py has the same limitations. Alas, I can't think of a way to
fix this within the current architecture of inspect/pydoc: it's simply
not possible in 2.2 to figure out everything needed just from examining
the object you get back from class.attr. You also need the class
context, and the method resolution order, and tests against various things
that simply didn't exist before. OTOH, knowledge of how to do that is
getting quite complex, so doesn't belong in pydoc.
classify_class_attrs takes a different approach, analyzing all
the class attrs "at once", and returning the most interesting stuff for
each, all in one gulp. pydoc needs to be reworked to use this for
classes (instead of the current "filter dir(class) umpteen times against
assorted predicates" approach).
2001-09-22 23:00:29 -03:00
|
|
|
|
2004-12-12 12:20:22 -04:00
|
|
|
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')
|
New function classify_class_attrs(). As a number of SF bug reports
point out, pydoc doesn't tell you where class attributes were defined,
gets several new 2.2 features wrong, and isn't aware of some new features
checked in on Thursday <wink>. pydoc is hampered in part because
inspect.py has the same limitations. Alas, I can't think of a way to
fix this within the current architecture of inspect/pydoc: it's simply
not possible in 2.2 to figure out everything needed just from examining
the object you get back from class.attr. You also need the class
context, and the method resolution order, and tests against various things
that simply didn't exist before. OTOH, knowledge of how to do that is
getting quite complex, so doesn't belong in pydoc.
classify_class_attrs takes a different approach, analyzing all
the class attrs "at once", and returning the most interesting stuff for
each, all in one gulp. pydoc needs to be reworked to use this for
classes (instead of the current "filter dir(class) umpteen times against
assorted predicates" approach).
2001-09-22 23:00:29 -03:00
|
|
|
|
|
|
|
|
2004-12-12 12:20:22 -04:00
|
|
|
class C(A):
|
New function classify_class_attrs(). As a number of SF bug reports
point out, pydoc doesn't tell you where class attributes were defined,
gets several new 2.2 features wrong, and isn't aware of some new features
checked in on Thursday <wink>. pydoc is hampered in part because
inspect.py has the same limitations. Alas, I can't think of a way to
fix this within the current architecture of inspect/pydoc: it's simply
not possible in 2.2 to figure out everything needed just from examining
the object you get back from class.attr. You also need the class
context, and the method resolution order, and tests against various things
that simply didn't exist before. OTOH, knowledge of how to do that is
getting quite complex, so doesn't belong in pydoc.
classify_class_attrs takes a different approach, analyzing all
the class attrs "at once", and returning the most interesting stuff for
each, all in one gulp. pydoc needs to be reworked to use this for
classes (instead of the current "filter dir(class) umpteen times against
assorted predicates" approach).
2001-09-22 23:00:29 -03:00
|
|
|
|
2004-12-12 12:20:22 -04:00
|
|
|
def m(self): pass
|
|
|
|
def c(self): pass
|
New function classify_class_attrs(). As a number of SF bug reports
point out, pydoc doesn't tell you where class attributes were defined,
gets several new 2.2 features wrong, and isn't aware of some new features
checked in on Thursday <wink>. pydoc is hampered in part because
inspect.py has the same limitations. Alas, I can't think of a way to
fix this within the current architecture of inspect/pydoc: it's simply
not possible in 2.2 to figure out everything needed just from examining
the object you get back from class.attr. You also need the class
context, and the method resolution order, and tests against various things
that simply didn't exist before. OTOH, knowledge of how to do that is
getting quite complex, so doesn't belong in pydoc.
classify_class_attrs takes a different approach, analyzing all
the class attrs "at once", and returning the most interesting stuff for
each, all in one gulp. pydoc needs to be reworked to use this for
classes (instead of the current "filter dir(class) umpteen times against
assorted predicates" approach).
2001-09-22 23:00:29 -03:00
|
|
|
|
2004-12-12 12:20:22 -04:00
|
|
|
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')
|
New function classify_class_attrs(). As a number of SF bug reports
point out, pydoc doesn't tell you where class attributes were defined,
gets several new 2.2 features wrong, and isn't aware of some new features
checked in on Thursday <wink>. pydoc is hampered in part because
inspect.py has the same limitations. Alas, I can't think of a way to
fix this within the current architecture of inspect/pydoc: it's simply
not possible in 2.2 to figure out everything needed just from examining
the object you get back from class.attr. You also need the class
context, and the method resolution order, and tests against various things
that simply didn't exist before. OTOH, knowledge of how to do that is
getting quite complex, so doesn't belong in pydoc.
classify_class_attrs takes a different approach, analyzing all
the class attrs "at once", and returning the most interesting stuff for
each, all in one gulp. pydoc needs to be reworked to use this for
classes (instead of the current "filter dir(class) umpteen times against
assorted predicates" approach).
2001-09-22 23:00:29 -03:00
|
|
|
|
2004-12-12 12:20:22 -04:00
|
|
|
class D(B, C):
|
New function classify_class_attrs(). As a number of SF bug reports
point out, pydoc doesn't tell you where class attributes were defined,
gets several new 2.2 features wrong, and isn't aware of some new features
checked in on Thursday <wink>. pydoc is hampered in part because
inspect.py has the same limitations. Alas, I can't think of a way to
fix this within the current architecture of inspect/pydoc: it's simply
not possible in 2.2 to figure out everything needed just from examining
the object you get back from class.attr. You also need the class
context, and the method resolution order, and tests against various things
that simply didn't exist before. OTOH, knowledge of how to do that is
getting quite complex, so doesn't belong in pydoc.
classify_class_attrs takes a different approach, analyzing all
the class attrs "at once", and returning the most interesting stuff for
each, all in one gulp. pydoc needs to be reworked to use this for
classes (instead of the current "filter dir(class) umpteen times against
assorted predicates" approach).
2001-09-22 23:00:29 -03:00
|
|
|
|
2004-12-12 12:20:22 -04:00
|
|
|
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')
|
New function classify_class_attrs(). As a number of SF bug reports
point out, pydoc doesn't tell you where class attributes were defined,
gets several new 2.2 features wrong, and isn't aware of some new features
checked in on Thursday <wink>. pydoc is hampered in part because
inspect.py has the same limitations. Alas, I can't think of a way to
fix this within the current architecture of inspect/pydoc: it's simply
not possible in 2.2 to figure out everything needed just from examining
the object you get back from class.attr. You also need the class
context, and the method resolution order, and tests against various things
that simply didn't exist before. OTOH, knowledge of how to do that is
getting quite complex, so doesn't belong in pydoc.
classify_class_attrs takes a different approach, analyzing all
the class attrs "at once", and returning the most interesting stuff for
each, all in one gulp. pydoc needs to be reworked to use this for
classes (instead of the current "filter dir(class) umpteen times against
assorted predicates" approach).
2001-09-22 23:00:29 -03:00
|
|
|
|
2004-12-12 12:20:22 -04:00
|
|
|
def test_main():
|
2004-12-12 12:46:28 -04:00
|
|
|
run_unittest(TestDecorators, TestRetrievingSourceCode, TestOneliners,
|
2005-03-12 12:37:11 -04:00
|
|
|
TestBuggyCases,
|
2004-12-12 12:20:22 -04:00
|
|
|
TestInterpreterStack, TestClassesAndFunctions, TestPredicates)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|