@@ -403,6 +403,13 @@ def restrict_walk_packages(self, path=None):
403
403
finally :
404
404
pkgutil .walk_packages = walk_packages
405
405
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
+
406
413
407
414
class PydocDocTest (unittest .TestCase ):
408
415
@@ -715,6 +722,29 @@ def test_apropos_empty_doc(self):
715
722
finally :
716
723
os .chmod (pkgdir , current_mode )
717
724
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
+
718
748
@unittest .skip ('causes undesireable side-effects (#20128)' )
719
749
def test_modules (self ):
720
750
# See Helper.listmodules().
@@ -891,16 +921,12 @@ def test_url_requests(self):
891
921
892
922
with self .restrict_walk_packages ():
893
923
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 )
897
925
898
926
path = string .__file__
899
927
title = "Pydoc: getfile " + path
900
928
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 )
904
930
905
931
906
932
class TestHelper (unittest .TestCase ):
0 commit comments