]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Backport rev 2.132 (note: some earlier bugfix candidates still TBD).
authorGuido van Rossum <guido@python.org>
Thu, 18 Apr 2002 00:37:10 +0000 (00:37 +0000)
committerGuido van Rossum <guido@python.org>
Thu, 18 Apr 2002 00:37:10 +0000 (00:37 +0000)
SF bug 542984.

Change type_get_doc (the get function for __doc__) to look in tp_dict
more often, and if it finds a descriptor in tp_dict, to call it (with
a NULL instance).  This means you can add a __doc__ descriptor to a
new-style class that returns instance docs when called on an instance,
and class docs when called on a class -- or the same docs in either
case, but lazily computed.

I'll also check this into the 2.2 maintenance branch.

Lib/test/test_descr.py

index 22b9d2f39a2c5a04e0c7bdf5af44ca4c8571621c..953046957daa280b61ae04f7799b715084d6a4d7 100644 (file)
@@ -2843,6 +2843,25 @@ def modules():
     m.foo = 1
     vereq(m.__dict__, {"foo": 1})
 
+def docdescriptor():
+    # SF bug 542984
+    if verbose: print "Testing __doc__ descriptor..."
+    class DocDescr(object):
+        def __get__(self, object, otype):
+            if object:
+                object = object.__class__.__name__ + ' instance'
+            if otype:
+                otype = otype.__name__
+            return 'object=%s; type=%s' % (object, otype)
+    class OldClass:
+        __doc__ = DocDescr()
+    class NewClass(object):
+        __doc__ = DocDescr()
+    vereq(OldClass.__doc__, 'object=None; type=OldClass')
+    vereq(OldClass().__doc__, 'object=OldClass instance; type=OldClass')
+    vereq(NewClass.__doc__, 'object=None; type=NewClass')
+    vereq(NewClass().__doc__, 'object=NewClass instance; type=NewClass')
+
 def test_main():
     class_docstrings()
     lists()
@@ -2900,6 +2919,7 @@ def test_main():
     deepcopyrecursive()
     modules()
     pickleslots()
+    docdescriptor()
     if verbose: print "All OK"
 
 if __name__ == "__main__":