diff --git a/Lib/pydoc.py b/Lib/pydoc.py --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -1458,8 +1458,13 @@ def locate(path, forceload=0): except AttributeError: return None return object else: - if hasattr(builtins, path): - return getattr(builtins, path) + if len(parts) > 0: + # this block resolves strings like 'str' or 'str.translate' + if hasattr(builtins, parts[0]): + result = builtins + for name in parts: + result = getattr(result, name) + return result # --------------------------------------- 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 @@ -2,6 +2,7 @@ import sys import os import os.path import difflib +import builtins import subprocess import re import pydoc @@ -344,6 +345,16 @@ class TestDescriptions(unittest.TestCase expected = 'C in module %s object' % __name__ self.assertTrue(expected in pydoc.render_doc(c)) + def test_builtin(self): + objects = [str, str.translate, builtins.str, builtins.str.translate, + 'str', 'str.translate', 'builtins.str', + 'builtins.str.translate'] + for o in objects: + try: + pydoc.render_doc(o) + except ImportError: + self.fail('finding the doc of {!r} failed'.format(o)) + class TestHelper(unittest.TestCase): def test_keywords(self): diff --git a/Misc/NEWS b/Misc/NEWS --- a/Misc/NEWS +++ b/Misc/NEWS @@ -75,6 +75,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 #12175: RawIOBase.readall() now returns None if read() returns None. - Issue #12175: FileIO.readall() now raises a ValueError instead of an IOError