@@ -396,6 +396,13 @@ def restrict_walk_packages(self, path=None):
396
396
finally :
397
397
pkgutil .walk_packages = walk_packages
398
398
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
+
399
406
400
407
class PydocDocTest (unittest .TestCase ):
401
408
@@ -704,6 +711,29 @@ def test_apropos_empty_doc(self):
704
711
finally :
705
712
os .chmod (pkgdir , current_mode )
706
713
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
+
707
737
@unittest .skip ('causes undesireable side-effects (#20128)' )
708
738
def test_modules (self ):
709
739
# See Helper.listmodules().
@@ -880,16 +910,12 @@ def test_url_requests(self):
880
910
881
911
with self .restrict_walk_packages ():
882
912
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 )
886
914
887
915
path = string .__file__
888
916
title = "Pydoc: getfile " + path
889
917
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 )
893
919
894
920
895
921
class TestHelper (unittest .TestCase ):
0 commit comments