Skip to content

Commit e2e0dee

Browse files
committed
Issue #18010: Merge pydoc web search fix from 3.5
2 parents a2149ed + 98da9d0 commit e2e0dee

3 files changed

Lines changed: 38 additions & 7 deletions

File tree

Lib/pydoc.py

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

23712371
with warnings.catch_warnings():
23722372
warnings.filterwarnings('ignore') # ignore problems during import
2373-
ModuleScanner().run(callback, key)
2373+
def onerror(modname):
2374+
pass
2375+
ModuleScanner().run(callback, key, onerror=onerror)
23742376

23752377
# format page
23762378
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().
@@ -896,16 +926,12 @@ def test_url_requests(self):
896926

897927
with self.restrict_walk_packages():
898928
for url, title in requests:
899-
text = pydoc._url_handler(url, "text/html")
900-
result = get_html_title(text)
901-
self.assertEqual(result, title, text)
929+
self.call_url_handler(url, title)
902930

903931
path = string.__file__
904932
title = "Pydoc: getfile " + path
905933
url = "getfile?key=" + path
906-
text = pydoc._url_handler(url, "text/html")
907-
result = get_html_title(text)
908-
self.assertEqual(result, title)
934+
self.call_url_handler(url, title)
909935

910936

911937
class TestHelper(unittest.TestCase):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ Core and Builtins
7272
Library
7373
-------
7474

75+
- Issue #18010: Fix the pydoc web server's module search function to handle
76+
exceptions from importing packages.
77+
7578
- Issue #25554: Got rid of circular references in regular expression parsing.
7679

7780
- Issue #18973: Command-line interface of the calendar module now uses argparse

0 commit comments

Comments
 (0)