Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 52dfc74

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.
2 parents 6227c69 + 9929bc5 commit 52dfc74

3 files changed

Lines changed: 32 additions & 2 deletions

File tree

Lib/pydoc.py

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

134134
def _is_some_method(obj):
135-
return inspect.ismethod(obj) or inspect.ismethoddescriptor(obj)
135+
return (inspect.isfunction(obj) or
136+
inspect.ismethod(obj) or
137+
inspect.isbuiltin(obj) or
138+
inspect.ismethoddescriptor(obj))
136139

137140
def allmethods(cl):
138141
methods = {}

Lib/test/test_pydoc.py

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

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

399423
class PydocImportTest(unittest.TestCase):
400424

Misc/NEWS

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,10 @@ Core and Builtins
193193
Library
194194
-------
195195

196-
Issue #16880: Do not assume _imp.load_dynamic() is defined in the imp module.
196+
- Issue #17476: Fixed regression relative to Python2 in undocumented pydoc
197+
'allmethods'; it was missing unbound methods on the class.
198+
199+
- Issue #16880: Do not assume _imp.load_dynamic() is defined in the imp module.
197200

198201
- Issue #16389: Fixed a performance regression relative to Python 3.1 in the
199202
caching of compiled regular expressions.

0 commit comments

Comments
 (0)