@@ -78,6 +78,10 @@ class or function within a module or module in a package. If the
78
78
79
79
80
80
# --------------------------------------------------------- common routines
81
+ missing_pattern = '''\
82
+ No Python documentation found for %r.
83
+ Use help() to get the interactive help utility.
84
+ Use help(str) for help on the str class.'''
81
85
82
86
def pathdirs ():
83
87
"""Convert sys.path into a list of absolute, existing, unique paths."""
@@ -1738,10 +1742,7 @@ def resolve(thing, forceload=0):
1738
1742
if isinstance (thing , str ):
1739
1743
object = locate (thing , forceload )
1740
1744
if object is None :
1741
- raise ImportError ('''\
1742
- No Python documentation found for %r.
1743
- Use help() to get the interactive help utility.
1744
- Use help(str) for help on the str class.''' % thing )
1745
+ raise ImportError (missing_pattern % thing )
1745
1746
return object , thing
1746
1747
else :
1747
1748
name = getattr (thing , '__name__' , None )
@@ -1775,10 +1776,15 @@ def render_doc(thing, title='Python Library Documentation: %s', forceload=0,
1775
1776
return title % desc + '\n \n ' + renderer .document (object , name )
1776
1777
1777
1778
def doc (thing , title = 'Python Library Documentation: %s' , forceload = 0 ,
1778
- output = None ):
1779
+ output = None , is_cli = False ):
1779
1780
"""Display text documentation, given an object or a path to an object."""
1780
1781
if output is None :
1781
- pager (render_doc (thing , title , forceload ))
1782
+ try :
1783
+ pager (render_doc (thing , title , forceload ))
1784
+ except ImportError :
1785
+ if is_cli :
1786
+ raise ImportError (missing_pattern % thing ) from None
1787
+ pager (missing_pattern % thing )
1782
1788
else :
1783
1789
output .write (render_doc (thing , title , forceload , plaintext ))
1784
1790
@@ -2039,7 +2045,7 @@ def getline(self, prompt):
2039
2045
self .output .flush ()
2040
2046
return self .input .readline ()
2041
2047
2042
- def help (self , request ):
2048
+ def help (self , request , is_cli = False ):
2043
2049
if isinstance (request , str ):
2044
2050
request = request .strip ()
2045
2051
if request == 'keywords' : self .listkeywords ()
@@ -2051,13 +2057,13 @@ def help(self, request):
2051
2057
elif request in self .symbols : self .showsymbol (request )
2052
2058
elif request in ['True' , 'False' , 'None' ]:
2053
2059
# special case these keywords since they are objects too
2054
- doc (eval (request ), 'Help on %s:' )
2060
+ doc (eval (request ), 'Help on %s:' , is_cli = is_cli )
2055
2061
elif request in self .keywords : self .showtopic (request )
2056
2062
elif request in self .topics : self .showtopic (request )
2057
- elif request : doc (request , 'Help on %s:' , output = self ._output )
2058
- else : doc (str , 'Help on %s:' , output = self ._output )
2063
+ elif request : doc (request , 'Help on %s:' , output = self ._output , is_cli = is_cli )
2064
+ else : doc (str , 'Help on %s:' , output = self ._output , is_cli = is_cli )
2059
2065
elif isinstance (request , Helper ): self ()
2060
- else : doc (request , 'Help on %s:' , output = self ._output )
2066
+ else : doc (request , 'Help on %s:' , output = self ._output , is_cli = is_cli )
2061
2067
self .output .write ('\n ' )
2062
2068
2063
2069
def intro (self ):
@@ -2751,7 +2757,6 @@ def cli():
2751
2757
"""Command-line interface (looks at sys.argv to decide what to do)."""
2752
2758
import getopt
2753
2759
class BadUsage (Exception ): pass
2754
-
2755
2760
_adjust_cli_sys_path ()
2756
2761
2757
2762
try :
@@ -2795,7 +2800,7 @@ class BadUsage(Exception): pass
2795
2800
else :
2796
2801
writedoc (arg )
2797
2802
else :
2798
- help .help (arg )
2803
+ help .help (arg , is_cli = True )
2799
2804
except (ImportError , ErrorDuringImport ) as value :
2800
2805
print (value )
2801
2806
sys .exit (1 )
0 commit comments