@@ -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
400407class 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
895921class TestHelper (unittest .TestCase ):
0 commit comments