@@ -78,6 +78,10 @@ class or function within a module or module in a package. If the
7878
7979
8080# --------------------------------------------------------- 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.'''
8185
8286def pathdirs ():
8387 """Convert sys.path into a list of absolute, existing, unique paths."""
@@ -1738,10 +1742,7 @@ def resolve(thing, forceload=0):
17381742 if isinstance (thing , str ):
17391743 object = locate (thing , forceload )
17401744 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 )
17451746 return object , thing
17461747 else :
17471748 name = getattr (thing , '__name__' , None )
@@ -1775,10 +1776,15 @@ def render_doc(thing, title='Python Library Documentation: %s', forceload=0,
17751776 return title % desc + '\n \n ' + renderer .document (object , name )
17761777
17771778def doc (thing , title = 'Python Library Documentation: %s' , forceload = 0 ,
1778- output = None ):
1779+ output = None , is_cli = False ):
17791780 """Display text documentation, given an object or a path to an object."""
17801781 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 )
17821788 else :
17831789 output .write (render_doc (thing , title , forceload , plaintext ))
17841790
@@ -2039,7 +2045,7 @@ def getline(self, prompt):
20392045 self .output .flush ()
20402046 return self .input .readline ()
20412047
2042- def help (self , request ):
2048+ def help (self , request , is_cli = False ):
20432049 if isinstance (request , str ):
20442050 request = request .strip ()
20452051 if request == 'keywords' : self .listkeywords ()
@@ -2051,13 +2057,13 @@ def help(self, request):
20512057 elif request in self .symbols : self .showsymbol (request )
20522058 elif request in ['True' , 'False' , 'None' ]:
20532059 # 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 )
20552061 elif request in self .keywords : self .showtopic (request )
20562062 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 )
20592065 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 )
20612067 self .output .write ('\n ' )
20622068
20632069 def intro (self ):
@@ -2751,7 +2757,6 @@ def cli():
27512757 """Command-line interface (looks at sys.argv to decide what to do)."""
27522758 import getopt
27532759 class BadUsage (Exception ): pass
2754-
27552760 _adjust_cli_sys_path ()
27562761
27572762 try :
@@ -2795,7 +2800,7 @@ class BadUsage(Exception): pass
27952800 else :
27962801 writedoc (arg )
27972802 else :
2798- help .help (arg )
2803+ help .help (arg , is_cli = True )
27992804 except (ImportError , ErrorDuringImport ) as value :
28002805 print (value )
28012806 sys .exit (1 )
0 commit comments