]> granicus.if.org Git - python/commitdiff
Patch #711902: Cause pydoc to show data descriptor __doc__ strings.
authorMartin v. Löwis <martin@v.loewis.de>
Sat, 3 May 2003 09:09:02 +0000 (09:09 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Sat, 3 May 2003 09:09:02 +0000 (09:09 +0000)
Doc/lib/libinspect.tex
Lib/inspect.py
Lib/pydoc.py
Lib/test/test_inspect.py
Misc/NEWS

index a1293f0e96a8a055a8c2663531b023d12874e6a2..cc41f2f18eda46b158f4608229438587138aa030 100644 (file)
@@ -161,6 +161,31 @@ Note:
   Return true if the object is a user-defined or built-in function or method.
 \end{funcdesc}
 
+\begin{funcdesc}{ismethoddescriptor}{object}
+  Return true if the object is a method descriptor, but not if ismethod() or 
+  isclass() or isfunction() are true.
+
+  This is new as of 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 one of the other
+  tests return false from the ismethoddescriptor() test, simply because
+  the other tests promise more -- you can, e.g., count on having the
+  im_func attribute (etc) when an object passes ismethod().
+\end{funcdesc}
+
+\begin{funcdesc}{isdatadescriptor}{object}
+  Return true if the object is a data descriptor.
+
+  Data descriptors have both a __get__ and a __set__ attribute.  Examples are
+  properties (defined in Python) and getsets and members (defined in C).
+  Typically, data descriptors will also have __name__ and __doc__ attributes 
+  (properties, getsets, and members have both of these attributes), but this 
+  is not guaranteed.
+\end{funcdesc}
+
 \subsection{Retrieving source code
             \label{inspect-source}}
 
index 4baebe0c6f46fcf5c0cc6723ab14a69dcf460a1e..a2ea7390bdbb47bd8849bc61d0cfde1ee10688c4 100644 (file)
@@ -78,6 +78,16 @@ def ismethoddescriptor(object):
             and not isfunction(object)
             and not isclass(object))
 
+def isdatadescriptor(object):
+    """Return true if the object is a data descriptor.
+
+    Data descriptors have both a __get__ and a __set__ attribute.  Examples are
+    properties (defined in Python) and getsets and members (defined in C).
+    Typically, data descriptors will also have __name__ and __doc__ attributes
+    (properties, getsets, and members have both of these attributes), but this
+    is not guaranteed."""
+    return (hasattr(object, "__set__") and hasattr(object, "__get__"))
+
 def isfunction(object):
     """Return true if the object is a user-defined function.
 
index 7f0adddb19a1991ce878be9735341cc56492f490..8e5064a44db16f302f8c322afd2389be269a3b9f 100755 (executable)
@@ -686,7 +686,7 @@ class HTMLDoc(Doc):
                 push(msg)
                 for name, kind, homecls, value in ok:
                     base = self.docother(getattr(object, name), name, mod)
-                    if callable(value):
+                    if callable(value) or inspect.isdatadescriptor(value):
                         doc = getattr(value, "__doc__", None)
                     else:
                         doc = None
@@ -1087,7 +1087,7 @@ class TextDoc(Doc):
                 hr.maybe()
                 push(msg)
                 for name, kind, homecls, value in ok:
-                    if callable(value):
+                    if callable(value) or inspect.isdatadescriptor(value):
                         doc = getattr(value, "__doc__", None)
                     else:
                         doc = None
index d253f26a5fd96f45f451dd0fc54321229f2bbf59..33e0b0d7e172251b83d8cacb010bbc048aa99c79 100644 (file)
@@ -61,6 +61,7 @@ class FesteringGob(MalodorousPervert, ParrotDroppings):
 # isbuiltin, isroutine, getmembers, getdoc, getfile, getmodule,
 # getsourcefile, getcomments, getsource, getclasstree, getargspec,
 # getargvalues, formatargspec, formatargvalues, currentframe, stack, trace
+# isdatadescriptor
 
 from test.test_support import TestFailed, TESTFN
 import sys, imp, os, string
@@ -104,6 +105,8 @@ istest(inspect.ismethod, 'mod.StupidGit.abuse')
 istest(inspect.ismethod, 'git.argue')
 istest(inspect.ismodule, 'mod')
 istest(inspect.istraceback, 'tb')
+istest(inspect.isdatadescriptor, '__builtins__.file.closed')
+istest(inspect.isdatadescriptor, '__builtins__.file.softspace')
 test(inspect.isroutine(mod.spam), 'isroutine(mod.spam)')
 test(inspect.isroutine([].count), 'isroutine([].count)')
 
index a9063e21eb5a99435b7b01607a0a254f7e1e8533..05c9ce26ddfcd361ab54e116475fbb3ce5473338 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -21,6 +21,9 @@ Extension modules
 Library
 -------
 
+- inspect.is{method|data}descriptor was added, to allow pydoc display
+  __doc__ of data descriptors.
+
 - Fixed socket speed loss caused by use of the _socketobject wrapper class
   in socket.py.