diff options
author | R David Murray <rdmurray@bitdance.com> | 2013-03-19 02:32:35 -0400 |
---|---|---|
committer | R David Murray <rdmurray@bitdance.com> | 2013-03-19 02:32:35 -0400 |
commit | 52dfc74080a8bc6949ca713d862c9324ac62ca5c (patch) | |
tree | 6ec1a18089f33775ef8fd6eb454eac04f6e742b5 | |
parent | 6227c6952e5b9ab4c41f027b5e971b6812e24efa (diff) | |
parent | 9929bc543a014f3c7f764d79a614fc8a9751d7ce (diff) | |
download | cpython-git-52dfc74080a8bc6949ca713d862c9324ac62ca5c.tar.gz |
#17476: make allmethods actually return all methods.
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.
-rwxr-xr-x | Lib/pydoc.py | 5 | ||||
-rw-r--r-- | Lib/test/test_pydoc.py | 24 | ||||
-rw-r--r-- | Misc/NEWS | 5 |
3 files changed, 32 insertions, 2 deletions
diff --git a/Lib/pydoc.py b/Lib/pydoc.py index fa531e9051..9dce07976d 100755 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -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 = {} diff --git a/Lib/test/test_pydoc.py b/Lib/test/test_pydoc.py index d98a526a59..542a29bc99 100644 --- a/Lib/test/test_pydoc.py +++ b/Lib/test/test_pydoc.py @@ -395,6 +395,30 @@ class PydocDocTest(unittest.TestCase): synopsis = pydoc.synopsis(TESTFN, {}) self.assertEqual(synopsis, 'line 1: h\xe9') + 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): @@ -193,7 +193,10 @@ Core and Builtins Library ------- -Issue #16880: Do not assume _imp.load_dynamic() is defined in the imp module. +- Issue #17476: Fixed regression relative to Python2 in undocumented pydoc + 'allmethods'; it was missing unbound methods on the class. + +- Issue #16880: Do not assume _imp.load_dynamic() is defined in the imp module. - Issue #16389: Fixed a performance regression relative to Python 3.1 in the caching of compiled regular expressions. |