31
31
from test .support .script_helper import (assert_python_ok ,
32
32
assert_python_failure , spawn_python )
33
33
from test .support import threading_helper
34
- from test .support import (reap_children , captured_output , captured_stdout ,
34
+ from test .support import (reap_children , captured_stdout ,
35
35
captured_stderr , is_emscripten , is_wasi ,
36
36
requires_docstrings , MISSING_C_DOCSTRINGS )
37
37
from test .support .os_helper import (TESTFN , rmtree , unlink )
@@ -675,9 +675,8 @@ def test_help_output_redirect(self, pager_mock):
675
675
help_header = textwrap .dedent (help_header )
676
676
expected_help_pattern = help_header + expected_text_pattern
677
677
678
- with captured_output ('stdout' ) as output , \
679
- captured_output ('stderr' ) as err , \
680
- StringIO () as buf :
678
+ with captured_stdout () as output , captured_stderr () as err :
679
+ buf = StringIO ()
681
680
helper = pydoc .Helper (output = buf )
682
681
helper .help (module )
683
682
result = buf .getvalue ().strip ()
@@ -701,9 +700,8 @@ def test_help_output_redirect_various_requests(self, pager_mock):
701
700
702
701
def run_pydoc_for_request (request , expected_text_part ):
703
702
"""Helper function to run pydoc with its output redirected"""
704
- with captured_output ('stdout' ) as output , \
705
- captured_output ('stderr' ) as err , \
706
- StringIO () as buf :
703
+ with captured_stdout () as output , captured_stderr () as err :
704
+ buf = StringIO ()
707
705
helper = pydoc .Helper (output = buf )
708
706
helper .help (request )
709
707
result = buf .getvalue ().strip ()
@@ -737,6 +735,45 @@ def run_pydoc_for_request(request, expected_text_part):
737
735
run_pydoc_for_request (pydoc .Helper .help , 'Help on function help in module pydoc:' )
738
736
# test for pydoc.Helper() instance skipped because it is always meant to be interactive
739
737
738
+ @unittest .skipIf (hasattr (sys , 'gettrace' ) and sys .gettrace (),
739
+ 'trace function introduces __locals__ unexpectedly' )
740
+ @requires_docstrings
741
+ def test_help_output_pager (self ):
742
+ def run_pydoc_pager (request , what , expected_first_line ):
743
+ with (captured_stdout () as output ,
744
+ captured_stderr () as err ,
745
+ unittest .mock .patch ('pydoc.pager' ) as pager_mock ,
746
+ self .subTest (repr (request ))):
747
+ helper = pydoc .Helper ()
748
+ helper .help (request )
749
+ self .assertEqual ('' , err .getvalue ())
750
+ self .assertEqual ('\n ' , output .getvalue ())
751
+ pager_mock .assert_called_once ()
752
+ result = clean_text (pager_mock .call_args .args [0 ])
753
+ self .assertEqual (result .splitlines ()[0 ], expected_first_line )
754
+ self .assertEqual (pager_mock .call_args .args [1 ], f'Help on { what } ' )
755
+
756
+ run_pydoc_pager ('%' , 'EXPRESSIONS' , 'Operator precedence' )
757
+ run_pydoc_pager ('True' , 'bool object' , 'Help on bool object:' )
758
+ run_pydoc_pager (True , 'bool object' , 'Help on bool object:' )
759
+ run_pydoc_pager ('assert' , 'assert' , 'The "assert" statement' )
760
+ run_pydoc_pager ('TYPES' , 'TYPES' , 'The standard type hierarchy' )
761
+ run_pydoc_pager ('pydoc.Helper.help' , 'pydoc.Helper.help' ,
762
+ 'Help on function help in pydoc.Helper:' )
763
+ run_pydoc_pager (pydoc .Helper .help , 'Helper.help' ,
764
+ 'Help on function help in module pydoc:' )
765
+ run_pydoc_pager ('str' , 'str' , 'Help on class str in module builtins:' )
766
+ run_pydoc_pager (str , 'str' , 'Help on class str in module builtins:' )
767
+ run_pydoc_pager ('str.upper' , 'str.upper' , 'Help on method_descriptor in str:' )
9E7A
768
+ run_pydoc_pager (str .upper , 'str.upper' , 'Help on method_descriptor:' )
769
+ run_pydoc_pager (str .__add__ , 'str.__add__' , 'Help on wrapper_descriptor:' )
770
+ run_pydoc_pager (int .numerator , 'int.numerator' ,
771
+ 'Help on getset descriptor builtins.int.numerator:' )
772
+ run_pydoc_pager (list [int ], 'list' ,
773
+ 'Help on GenericAlias in module builtins:' )
774
+ run_pydoc_pager ('sys' , 'sys' , 'Help on built-in module sys:' )
775
+ run_pydoc_pager (sys , 'sys' , 'Help on built-in module sys:' )
776
+
740
777
def test_showtopic (self ):
741
778
with captured_stdout () as showtopic_io :
742
779
helper = pydoc .Helper ()
@@ -770,9 +807,8 @@ def test_showtopic_output_redirect(self, pager_mock):
770
807
# Helper.showtopic should be redirected
771
808
self .maxDiff = None
772
809
773
- with captured_output ('stdout' ) as output , \
774
- captured_output ('stderr' ) as err , \
775
- StringIO () as buf :
810
+ with captured_stdout () as output , captured_stderr () as err :
811
+ buf = StringIO ()
776
812
helper = pydoc .Helper (output = buf )
777
813
helper .showtopic ('with' )
778
814
result = buf .getvalue ().strip ()
@@ -785,23 +821,23 @@ def test_showtopic_output_redirect(self, pager_mock):
785
821
def test_lambda_with_return_annotation (self ):
786
822
func = lambda a , b , c : 1
787
823
func .__annotations__ = {"return" : int }
788
- with captured_output ( 'stdout' ) as help_io :
824
+ with captured_stdout ( ) as help_io :
789
825
pydoc .help (func )
790
826
helptext = help_io .getvalue ()
791
827
self .assertIn ("lambda (a, b, c) -> int" , helptext )
792
828
793
829
def test_lambda_without_return_annotation (self ):
794
830
func = lambda a , b , c : 1
795
831
func .__annotations__ = {"a" : int , "b" : int , "c" : int }
796
- with captured_output ( 'stdout' ) as help_io :
832
+ with captured_stdout ( ) as help_io :
797
833
pydoc .help (func )
798
834
helptext = help_io .getvalue ()
799
835
self .assertIn ("lambda (a: int, b: int, c: int)" , helptext )
800
836
801
837
def test_lambda_with_return_and_params_annotation (self ):
802
838
func = lambda a , b , c : 1
803
839
func .__annotations__ = {"a" : int , "b" : int , "c" : int , "return" : int }
804
- with captured_output ( 'stdout' ) as help_io :
840
+ with captured_stdout ( ) as help_io :
805
841
pydoc .help (func )
806
842
helptext = help_io .getvalue ()
807
843
self .assertIn ("lambda (a: int, b: int, c: int) -> int" , helptext )
0 commit comments