[Python-checkins] cpython (merge 3.4 -> 3.5): Issue #18010: Merge pydoc web search fix from 3.4 into 3.5

martin.panter python-checkins at python.org
Thu Nov 5 20:09:57 EST 2015


https://hg.python.org/cpython/rev/8702efa1feb4
changeset:   98979:8702efa1feb4
branch:      3.5
parent:      98975:ce93a386dcb8
parent:      98978:9098731de840
user:        Martin Panter <vadmium+py at gmail.com>
date:        Fri Nov 06 00:51:38 2015 +0000
summary:
  Issue #18010: Merge pydoc web search fix from 3.4 into 3.5

files:
  Lib/pydoc.py           |   4 ++-
  Lib/test/test_pydoc.py |  38 +++++++++++++++++++++++++----
  Misc/NEWS              |   3 ++
  3 files changed, 38 insertions(+), 7 deletions(-)


diff --git a/Lib/pydoc.py b/Lib/pydoc.py
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -2359,7 +2359,9 @@
 
         with warnings.catch_warnings():
             warnings.filterwarnings('ignore') # ignore problems during import
-            ModuleScanner().run(callback, key)
+            def onerror(modname):
+                pass
+            ModuleScanner().run(callback, key, onerror=onerror)
 
         # format page
         def bltinlink(name):
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
@@ -396,6 +396,13 @@
         finally:
             pkgutil.walk_packages = walk_packages
 
+    def call_url_handler(self, url, expected_title):
+        text = pydoc._url_handler(url, "text/html")
+        result = get_html_title(text)
+        # Check the title to ensure an unexpected error page was not returned
+        self.assertEqual(result, expected_title, text)
+        return text
+
 
 class PydocDocTest(unittest.TestCase):
 
@@ -704,6 +711,29 @@
         finally:
             os.chmod(pkgdir, current_mode)
 
+    def test_url_search_package_error(self):
+        # URL handler search should cope with packages that raise exceptions
+        pkgdir = os.path.join(TESTFN, "test_error_package")
+        os.mkdir(pkgdir)
+        init = os.path.join(pkgdir, "__init__.py")
+        with open(init, "wt", encoding="ascii") as f:
+            f.write("""raise ValueError("ouch")\n""")
+        with self.restrict_walk_packages(path=[TESTFN]):
+            # Package has to be importable for the error to have any effect
+            saved_paths = tuple(sys.path)
+            sys.path.insert(0, TESTFN)
+            try:
+                with self.assertRaisesRegex(ValueError, "ouch"):
+                    import test_error_package  # Sanity check
+
+                text = self.call_url_handler("search?key=test_error_package",
+                    "Pydoc: Search Results")
+                found = ('<a href="test_error_package.html">'
+                    'test_error_package</a>')
+                self.assertIn(found, text)
+            finally:
+                sys.path[:] = saved_paths
+
     @unittest.skip('causes undesireable side-effects (#20128)')
     def test_modules(self):
         # See Helper.listmodules().
@@ -880,16 +910,12 @@
 
         with self.restrict_walk_packages():
             for url, title in requests:
-                text = pydoc._url_handler(url, "text/html")
-                result = get_html_title(text)
-                self.assertEqual(result, title, text)
+                self.call_url_handler(url, title)
 
             path = string.__file__
             title = "Pydoc: getfile " + path
             url = "getfile?key=" + path
-            text = pydoc._url_handler(url, "text/html")
-            result = get_html_title(text)
-            self.assertEqual(result, title)
+            self.call_url_handler(url, title)
 
 
 class TestHelper(unittest.TestCase):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -54,6 +54,9 @@
 Library
 -------
 
+- Issue #18010: Fix the pydoc web server's module search function to handle
+  exceptions from importing packages.
+
 - Issue #25554: Got rid of circular references in regular expression parsing.
 
 - Issue #25510: fileinput.FileInput.readline() now returns b'' instead of ''

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


More information about the Python-checkins mailing list