]> granicus.if.org Git - python/commitdiff
Make “pydoc somebuiltin.somemethod” work (#8887)
authorÉric Araujo <merwok@netwok.org>
Fri, 29 Jul 2011 15:03:55 +0000 (17:03 +0200)
committerÉric Araujo <merwok@netwok.org>
Fri, 29 Jul 2011 15:03:55 +0000 (17:03 +0200)
Lib/pydoc.py
Lib/test/test_pydoc.py
Misc/NEWS

index 34b2f5166885a12a9e9e670ab5d696673b7ad19a..8e2e47bd15d0af9ac90f1d6da05ef9c47264faf2 100755 (executable)
@@ -1482,13 +1482,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
 
index 2a21a7eb591916576d66f2170e3c696ade0aa1d1..72f5882cf5e5346d84d567a97edc8af6a5b5bb49 100644 (file)
@@ -1,5 +1,6 @@
 import os
 import sys
+import builtins
 import difflib
 import inspect
 import pydoc
@@ -419,6 +420,23 @@ 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):
index 022c90d2d9ff71943f107a587d0c95f9f6c01e27..1021df73d572d3b36062c14a9ca21915d76048e9 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -41,6 +41,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 #12603: Fix pydoc.synopsis() on files with non-negative st_mtime.
 
 - Issue #12514: Use try/finally to assure the timeit module restores garbage