[Python-checkins] CVS: python/dist/src/Lib/test test_inspect.py,1.5,1.6

Tim Peters tim_one@users.sourceforge.net
Sat, 22 Sep 2001 19:00:31 -0700


Update of /cvsroot/python/python/dist/src/Lib/test
In directory usw-pr-cvs1:/tmp/cvs-serv2693/python/Lib/test

Modified Files:
	test_inspect.py 
Log Message:
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).


Index: test_inspect.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_inspect.py,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** test_inspect.py	2001/09/22 06:10:55	1.5
--- test_inspect.py	2001/09/23 02:00:29	1.6
***************
*** 234,235 ****
--- 234,436 ----
  got = inspect.getmro(D)
  test(expected == got, "expected %r mro, got %r", expected, got)
+ 
+ # Test classify_class_attrs.
+ def attrs_wo_objs(cls):
+     return [t[:3] for t in inspect.classify_class_attrs(cls)]
+ 
+ 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)
+ 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', '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 non-dynamic classes.
+ 
+ class A(object):
+     __dynamic__ = 0
+ 
+     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):
+     __dynamic__ = 0
+ 
+     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):
+     __dynamic__ = 0
+ 
+     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):
+     __dynamic__ = 0
+ 
+     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')
+ 
+ # And again, but w/ new-style dynamic classes.
+ 
+ class A(object):
+     __dynamic__ = 1
+ 
+     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):
+     __dynamic__ = 1
+ 
+     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):
+     __dynamic__ = 1
+ 
+     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):
+     __dynamic__ = 1
+ 
+     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')