]> granicus.if.org Git - python/commitdiff
Merge: #17476: make allmethods actually return all methods.
authorR David Murray <rdmurray@bitdance.com>
Tue, 19 Mar 2013 06:47:44 +0000 (02:47 -0400)
committerR David Murray <rdmurray@bitdance.com>
Tue, 19 Mar 2013 06:47:44 +0000 (02:47 -0400)
This fixes a regression relative to Python2.  (In 2, methods on a class were
unbound methods and matched the inspect queries being done, in 3 they are just
functions and so were missed).

This is an undocumented function that pydoc itself does not use, but
I found that numpy at least uses it in its documentation generator.

Original patch by Matt Bachmann.

Lib/pydoc.py
Lib/test/test_pydoc.py
Misc/NEWS

index 292aa46015ecf5e7b6b86469a22d211ff751a4bf..4d681b0efa9776a9aeb44e12406c118ba92376ae 100755 (executable)
@@ -132,7 +132,10 @@ def stripid(text):
     return _re_stripid.sub(r'\1', text)
 
 def _is_some_method(obj):
-    return inspect.ismethod(obj) or inspect.ismethoddescriptor(obj)
+    return (inspect.isfunction(obj) or
+            inspect.ismethod(obj) or
+            inspect.isbuiltin(obj) or
+            inspect.ismethoddescriptor(obj))
 
 def allmethods(cl):
     methods = {}
index aa8baf723ca0df0c2ded1ee473efd97c0b84f86d..8adcdd87a480ce4d2ffcd50ce4540deb99021d4f 100644 (file)
@@ -420,6 +420,30 @@ class PydocDocTest(unittest.TestCase):
             self.assertTrue(pydoc.ispackage(test_dir))
             os.remove(init_path)
 
+    def test_allmethods(self):
+        # issue 17476: allmethods was no longer returning unbound methods.
+        # This test is a bit fragile in the face of changes to object and type,
+        # but I can't think of a better way to do it without duplicating the
+        # logic of the function under test.
+
+        class TestClass(object):
+            def method_returning_true(self):
+                return True
+
+        # What we expect to get back: everything on object...
+        expected = dict(vars(object))
+        # ...plus our unbound method...
+        expected['method_returning_true'] = TestClass.method_returning_true
+        # ...but not the non-methods on object.
+        del expected['__doc__']
+        del expected['__class__']
+        # inspect resolves descriptors on type into methods, but vars doesn't,
+        # so we need to update __subclasshook__.
+        expected['__subclasshook__'] = TestClass.__subclasshook__
+
+        methods = pydoc.allmethods(TestClass)
+        self.assertDictEqual(methods, expected)
+
 
 class PydocImportTest(unittest.TestCase):
 
index d69146337c188ae2bef82b51404ff995702b2bb5..cb5eedf1140e87c46d3f7e75e90e7ba54d89fb6f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -289,6 +289,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #17476: Fixed regression relative to Python2 in undocumented pydoc
+  'allmethods'; it was missing unbound methods on the class.
+
 - Issue #17474: Remove the deprecated methods of Request class.
 
 - Issue #16709: unittest discover order is no-longer filesystem specific. Patch