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

Commit 9ad0aae

Browse files
committed
Issue python#18010: Fix pydoc web server search to handle package exceptions
Implementation by Antoine Pitrou.
1 parent ade0412 commit 9ad0aae

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
@@ -2355,7 +2355,9 @@ def callback(path, modname, desc):
23552355

23562356
with warnings.catch_warnings():
23572357
warnings.filterwarnings('ignore') # ignore problems during import
2358-
ModuleScanner().run(callback, key)
2358+
def onerror(modname):
2359+
pass
2360+
ModuleScanner().run(callback, key, onerror=onerror)
23592361

23602362
# format page
23612363
def bltinlink(name):

Lib/test/test_pydoc.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,13 @@ def restrict_walk_packages(self, path=None):
403403
finally:
404404
pkgutil.walk_packages = walk_packages
405405

406+
def call_url_handler(self, url, expected_title):
407+
text = pydoc._url_handler(url, "text/html")
408+
result = get_html_title(text)
409+
# Check the title to ensure an unexpected error page was not returned
410+
self.assertEqual(result, expected_title, text)
411+
return text
412+
406413

407414
class PydocDocTest(unittest.TestCase):
408415

@@ -715,6 +722,29 @@ def test_apropos_empty_doc(self):
715722
finally:
716723
os.chmod(pkgdir, current_mode)
717724

725+
def test_url_search_package_error(self):
726+
# URL handler search should cope with packages that raise exceptions
727+
pkgdir = os.path.join(TESTFN, "test_error_package")
728+
os.mkdir(pkgdir)
729+
init = os.path.join(pkgdir, "__init__.py")
730+
with open(init, "wt", encoding="ascii") as f:
731+
f.write("""raise ValueError("ouch")\n""")
732+
with self.restrict_walk_packages(path=[TESTFN]):
733+
# Package has to be importable for the error to have any effect
734+
saved_paths = tuple(sys.path)
735+
sys.path.insert(0, TESTFN)
736+
try:
737+
with self.assertRaisesRegex(ValueError, "ouch"):
738+
import test_error_package # Sanity check
739+
740+
text = self.call_url_handler("search?key=test_error_package",
741+
"Pydoc: Search Results")
742+
found = ('<a href="test_error_package.html">'
743+
'test_error_package</a>')
744+
self.assertIn(found, text)
745+
finally:
746+
sys.path[:] = saved_paths
747+
718748
@unittest.skip('causes undesireable side-effects (#20128)')
719749
def test_modules(self):
720750
# See Helper.listmodules().
@@ -891,16 +921,12 @@ def test_url_requests(self):
891921

892922
with self.restrict_walk_packages():
893923
for url, title in requests:
894-
text = pydoc._url_handler(url, "text/html")
895-
result = get_html_title(text)
896-
self.assertEqual(result, title, text)
924+
self.call_url_handler(url, title)
897925

898926
path = string.__file__
899927
title = "Pydoc: getfile " + path
900928
url = "getfile?key=" + path
901-
text = pydoc._url_handler(url, "text/html")
902-
result = get_html_title(text)
903-
self.assertEqual(result, title)
929+
self.call_url_handler(url, title)
904930

905931

906932
class TestHelper(unittest.TestCase):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ Core and Builtins
9696
Library
9797
-------
9898

99+
- Issue #18010: Fix the pydoc web server's module search function to handle
100+
exceptions from importing packages.
101+
99102
- Issue #25510: fileinput.FileInput.readline() now returns b'' instead of ''
100103
at the end if the FileInput was opened with binary mode.
101104
Patch by Ryosuke Ito.

0 commit comments

Comments
 (0)