(original) (raw)

diff --git a/Lib/pydoc.py b/Lib/pydoc.py --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -1483,13 +1483,14 @@ def locate(path, forceload=0): else: break if module: object = module - for part in parts[n:]: - try: object = getattr(object, part) - except AttributeError: return None - return object else: - if hasattr(builtins, path): - return getattr(builtins, path) + object = builtins + for part in parts[n:]: + try: + object = getattr(object, part) + except AttributeError: + return None + return object # --------------------------------------- interactive interpreter interface diff --git a/Lib/test/test_pydoc.py b/Lib/test/test_pydoc.py --- a/Lib/test/test_pydoc.py +++ b/Lib/test/test_pydoc.py @@ -1,5 +1,6 @@ import os import sys +import builtins import difflib import inspect import pydoc @@ -407,6 +408,26 @@ class TestDescriptions(unittest.TestCase expected = 'C in module %s object' % __name__ self.assertIn(expected, pydoc.render_doc(c)) + def test_builtin(self): + for name in ('str', 'str.translate', 'builtins.str', + 'builtins.str.translate'): + # test low-level function + self.assertIsNotNone(pydoc.locate(name)) + # test high-level function + try: + pydoc.render_doc(name) + except ImportError: + self.fail('finding the doc of {!r} failed'.format(o)) + + for name in ('notbuiltins', 'strrr', 'strr.translate', + 'str.trrrranslate', 'builtins.strrr', + 'builtins.str.trrranslate'): + self.assertIsNone(pydoc.locate(name)) + self.assertRaises(ImportError, pydoc.render_doc, name) + + + + @unittest.skipUnless(threading, 'Threading required for this test.') class PydocServerTest(unittest.TestCase): diff --git a/Misc/NEWS b/Misc/NEWS --- a/Misc/NEWS +++ b/Misc/NEWS @@ -22,6 +22,9 @@ Core and Builtins Library ------- +- Issue #8887: "pydoc somebuiltin.somemethod" (or help('somebuiltin.somemethod') + in Python code) now finds the doc of the method. + - Issue #11583: Speed up os.path.isdir on Windows by using GetFileAttributes instead of os.stat.