[Python-checkins] cpython (3.2): Make “pydoc somebuiltin.somemethod” work (#8887)

eric.araujo python-checkins at python.org
Fri Jul 29 17:38:42 CEST 2011


http://hg.python.org/cpython/rev/f02a8f906342
changeset:   71600:f02a8f906342
branch:      3.2
parent:      71577:742b827f8337
user:        Éric Araujo <merwok at netwok.org>
date:        Fri Jul 29 17:03:55 2011 +0200
summary:
  Make “pydoc somebuiltin.somemethod” work (#8887)

files:
  Lib/pydoc.py           |  13 +++++++------
  Lib/test/test_pydoc.py |  18 ++++++++++++++++++
  Misc/NEWS              |   3 +++
  3 files changed, 28 insertions(+), 6 deletions(-)


diff --git a/Lib/pydoc.py b/Lib/pydoc.py
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -1482,13 +1482,14 @@
         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
@@ -419,6 +420,23 @@
         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
@@ -41,6 +41,9 @@
 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

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list