[Python-checkins] CVS: python/dist/src/Lib inspect.py,1.21,1.22 pydoc.py,1.41,1.42

Tim Peters tim_one@users.sourceforge.net
Wed, 19 Sep 2001 22:13:41 -0700


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

Modified Files:
	inspect.py pydoc.py 
Log Message:
After much thrashing, I believe this is a truly minimal patch to teach
pydoc how to do something sensible with 2.2 descriptors.  To see the
difference, browse __builtin__ via pydoc before and after the patch.


Index: inspect.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/inspect.py,v
retrieving revision 1.21
retrieving revision 1.22
diff -C2 -d -r1.21 -r1.22
*** inspect.py	2001/09/16 08:40:16	1.21
--- inspect.py	2001/09/20 05:13:38	1.22
***************
*** 58,61 ****
--- 58,78 ----
      return isinstance(object, types.MethodType)
  
+ def ismethoddescriptor(object):
+     """Return true if the object is a method descriptor, and ismethod false.
+ 
+     This is new in Python 2.2, and, for example, is true of int.__add__.
+     An object passing this test has a __get__ attribute but not a __set__
+     attribute, but beyond that the set of attributes varies.  __name__ is
+     usually sensible, and __doc__ often is.
+ 
+     Methods implemented via descriptors that also pass the ismethod() test
+     return false from the ismethoddescriptor() test, simply because
+     ismethod() is more informative -- you can, e.g., count on having the
+     im_func attribute (etc) when an object passes the latter."""
+     return (hasattr(object, "__get__")
+             and not hasattr(object, "__set__") # else it's a data descriptor
+             and not ismethod(object)           # mutual exclusion
+             and not isclass(object))
+ 
  def isfunction(object):
      """Return true if the object is a user-defined function.
***************
*** 128,132 ****
  def isroutine(object):
      """Return true if the object is any kind of function or method."""
!     return isbuiltin(object) or isfunction(object) or ismethod(object)
  
  def getmembers(object, predicate=None):
--- 145,152 ----
  def isroutine(object):
      """Return true if the object is any kind of function or method."""
!     return (isbuiltin(object)
!             or isfunction(object)
!             or ismethod(object)
!             or ismethoddescriptor(object))
  
  def getmembers(object, predicate=None):

Index: pydoc.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/pydoc.py,v
retrieving revision 1.41
retrieving revision 1.42
diff -C2 -d -r1.41 -r1.42
*** pydoc.py	2001/07/23 19:48:10	1.41
--- pydoc.py	2001/09/20 05:13:38	1.42
***************
*** 116,122 ****
      return text
  
  def allmethods(cl):
      methods = {}
!     for key, value in inspect.getmembers(cl, inspect.ismethod):
          methods[key] = 1
      for base in cl.__bases__:
--- 116,125 ----
      return text
  
+ def _is_some_method(object):
+     return inspect.ismethod(object) or inspect.ismethoddescriptor(object)
+ 
  def allmethods(cl):
      methods = {}
!     for key, value in inspect.getmembers(cl, _is_some_method):
          methods[key] = 1
      for base in cl.__bases__:
***************
*** 657,661 ****
              title = '<a name="%s"><strong>%s</strong></a> = %s' % (
                  anchor, name, reallink)
!         if inspect.isbuiltin(object):
              argspec = '(...)'
          else:
--- 660,664 ----
              title = '<a name="%s"><strong>%s</strong></a> = %s' % (
                  anchor, name, reallink)
!         if inspect.isbuiltin(object) or inspect.ismethoddescriptor(object):
              argspec = '(...)'
          else:
***************
*** 914,918 ****
                  skipdocs = 1
              title = self.bold(name) + ' = ' + realname
!         if inspect.isbuiltin(object):
              argspec = '(...)'
          else:
--- 917,921 ----
                  skipdocs = 1
              title = self.bold(name) + ' = ' + realname
!         if inspect.isbuiltin(object) or inspect.ismethoddescriptor(object):
              argspec = '(...)'
          else: