summaryrefslogtreecommitdiffstats
path: root/Lib/inspect.py
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2001-09-20 05:13:38 (GMT)
committerTim Peters <tim.peters@gmail.com>2001-09-20 05:13:38 (GMT)
commit536d2262f730dfd0d48ed541d834dd01f7d95562 (patch)
tree68b7d73547961857cda1063efece79a0dd6b4680 /Lib/inspect.py
parent3069d50c18d776737dbcc9670ab2ac60679d09d1 (diff)
downloadcpython-536d2262f730dfd0d48ed541d834dd01f7d95562.zip
cpython-536d2262f730dfd0d48ed541d834dd01f7d95562.tar.gz
cpython-536d2262f730dfd0d48ed541d834dd01f7d95562.tar.bz2
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.
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r--Lib/inspect.py22
1 files changed, 21 insertions, 1 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 5bb6a83..c4c15f5 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -57,6 +57,23 @@ def ismethod(object):
im_self instance to which this method is bound, or None"""
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.
@@ -127,7 +144,10 @@ def isbuiltin(object):
def isroutine(object):
"""Return true if the object is any kind of function or method."""
- return isbuiltin(object) or isfunction(object) or ismethod(object)
+ return (isbuiltin(object)
+ or isfunction(object)
+ or ismethod(object)
+ or ismethoddescriptor(object))
def getmembers(object, predicate=None):
"""Return all members of an object as (name, value) pairs sorted by name.