Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit 98da9d0

Browse files
committed
Issue python#18010: Merge pydoc web search fix from 3.4 into 3.5
2 parents 7931be4 + 9ad0aae commit 98da9d0

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

Lib/pydoc.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2359,7 +2359,9 @@ def callback(path, modname, desc):
23592359

23602360
with warnings.catch_warnings():
23612361
warnings.filterwarnings('ignore') # ignore problems during import
2362-
ModuleScanner().run(callback, key)
2362+
def onerror(modname):
2363+
pass
2364+
ModuleScanner().run(callback, key, onerror=onerror)
23632365

23642366
# format page
23652367
def bltinlink(name):

Lib/test/test_pydoc.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,13 @@ def restrict_walk_packages(self, path=None):
396396
finally:
397397
pkgutil.walk_packages = walk_packages
398398

399+
def call_url_handler(self, url, expected_title):
400+
text = pydoc._url_handler(url, "text/html")
401+
result = get_html_title(text)
402+
# Check the title to ensure an unexpected error page was not returned
403+
self.assertEqual(result, expected_title, text)
404+
return text
405+
399406

400407
class PydocDocTest(unittest.TestCase):
401408

@@ -704,6 +711,29 @@ def test_apropos_empty_doc(self):
704711
finally:
705712
os.chmod(pkgdir, current_mode)
706713

714+
def test_url_search_package_error(self):
715+
# URL handler search should cope with packages that raise exceptions
716+
pkgdir = os.path.join(TESTFN, "test_error_package")
717+
os.mkdir(pkgdir)
718+
init = os.path.join(pkgdir, "__init__.py")
719+
with open(init, "wt", encoding="ascii") as f:
720+
f.write("""raise ValueError("ouch")\n""")
721+
with self.restrict_walk_packages(path=[TESTFN]):
722+
# Package has to be importable for the error to have any effect
723+
saved_paths = tuple(sys.path)
724+
sys.path.insert(0, TESTFN)
725+
try:
726+
with self.assertRaisesRegex(ValueError, "ouch"):
727+
import test_error_package # Sanity check
728+
729+
text = self.call_url_handler("search?key=test_error_package",
730+
"Pydoc: Search Results")
731+
found = ('<a href="test_error_package.html">'
732+
'test_error_package</a>')
733+
self.assertIn(found, text)
734+
finally:
735+
sys.path[:] = saved_paths
736+
707737
@unittest.skip('causes undesireable side-effects (#20128)')
708738
def test_modules(self):
709739
# See Helper.listmodules().
@@ -880,16 +910,12 @@ def test_url_requests(self):
880910

881911
with self.restrict_walk_packages():
882912
for url, title in requests:
883-
text = pydoc._url_handler(url, "text/html")
884-
result = get_html_title(text)
885-
self.assertEqual(result, title, text)
913+
self.call_url_handler(url, title)
886914

887915
path = string.__file__
888916
title = "Pydoc: getfile " + path
889917
url = "getfile?key=" + path
890-
text = pydoc._url_handler(url, "text/html")
891-
result = get_html_title(text)
892-
self.assertEqual(result, title)
918+
self.call_url_handler(url, title)
893919

894920

895921
class TestHelper(unittest.TestCase):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ Core and Builtins
5454
Library
5555
-------
5656

57+
- Issue #18010: Fix the pydoc web server's module search function to handle
58+
exceptions from importing packages.
59+
5760
- Issue #25554: Got rid of circular references in regular expression parsing.
5861

5962
- Issue #25510: fileinput.FileInput.readline() now returns b'' instead of ''

0 commit comments

Comments
 (0)