Skip to content

Commit 9929bc5

Browse files
committed
#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.
1 parent a846f5a commit 9929bc5

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

Lib/pydoc.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,10 @@ def stripid(text):
137137
return _re_stripid.sub(r'\1', text)
138138

139139
def _is_some_method(obj):
140-
return inspect.ismethod(obj) or inspect.ismethoddescriptor(obj)
140+
return (inspect.isfunction(obj) or
141+
inspect.ismethod(obj) or
142+
inspect.isbuiltin(obj) or
143+
inspect.ismethoddescriptor(obj))
141144

142145
def allmethods(cl):
143146
methods = {}

Lib/test/test_pydoc.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,30 @@ def test_synopsis(self):
389389
synopsis = pydoc.synopsis(TESTFN, {})
390390
self.assertEqual(synopsis, 'line 1: h\xe9')
391391

392+
def test_allmethods(self):
393+
# issue 17476: allmethods was no longer returning unbound methods.
394+
# This test is a bit fragile in the face of changes to object and type,
395+
# but I can't think of a better way to do it without duplicating the
396+
# logic of the function under test.
397+
398+
class TestClass(object):
399+
def method_returning_true(self):
400+
return True
401+
402+
# What we expect to get back: everything on object...
403+
expected = dict(vars(object))
404+
# ...plus our unbound method...
405+
expected['method_returning_true'] = TestClass.method_returning_true
406+
# ...but not the non-methods on object.
407+
del expected['__doc__']
408+
del expected['__class__']
409+
# inspect resolves descriptors on type into methods, but vars doesn't,
410+
# so we need to update __subclasshook__.
411+
expected['__subclasshook__'] = TestClass.__subclasshook__
412+
413+
methods = pydoc.allmethods(TestClass)
414+
self.assertDictEqual(methods, expected)
415+
392416

393417
class PydocImportTest(unittest.TestCase):
394418

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ Core and Builtins
233233
Library
234234
-------
235235

236+
- Issue #17476: Fixed regression relative to Python2 in undocumented pydoc
237+
'allmethods'; it was missing unbound methods on the class.
238+
236239
- Issue #16389: Fixed a performance regression relative to Python 3.1 in the
237240
caching of compiled regular expressions.
238241

0 commit comments

Comments
 (0)