Skip to content

Commit

Permalink
pythongh-102541: Hide traceback in help prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
Eclips4 committed Mar 11, 2023
1 parent cbb0aa7 commit bdc9e9b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
31 changes: 18 additions & 13 deletions Lib/pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ class or function within a module or module in a package. If the


# --------------------------------------------------------- common routines
missing_pattern = '''\
No Python documentation found for %r.
Use help() to get the interactive help utility.
Use help(str) for help on the str class.'''

def pathdirs():
"""Convert sys.path into a list of absolute, existing, unique paths."""
Expand Down Expand Up @@ -1738,10 +1742,7 @@ def resolve(thing, forceload=0):
if isinstance(thing, str):
object = locate(thing, forceload)
if object is None:
raise ImportError('''\
No Python documentation found for %r.
Use help() to get the interactive help utility.
Use help(str) for help on the str class.''' % thing)
raise ImportError(missing_pattern % thing)
return object, thing
else:
name = getattr(thing, '__name__', None)
Expand Down Expand Up @@ -1775,10 +1776,15 @@ def render_doc(thing, title='Python Library Documentation: %s', forceload=0,
return title % desc + '\n\n' + renderer.document(object, name)

def doc(thing, title='Python Library Documentation: %s', forceload=0,
output=None):
output=None, is_cli=False):
"""Display text documentation, given an object or a path to an object."""
if output is None:
pager(render_doc(thing, title, forceload))
try:
pager(render_doc(thing, title, forceload))
except ImportError:
if is_cli:
raise ImportError(missing_pattern % thing) from None
pager(missing_pattern % thing)
else:
output.write(render_doc(thing, title, forceload, plaintext))

Expand Down Expand Up @@ -2039,7 +2045,7 @@ def getline(self, prompt):
self.output.flush()
return self.input.readline()

def help(self, request):
def help(self, request, is_cli=False):
if isinstance(request, str):
request = request.strip()
if request == 'keywords': self.listkeywords()
Expand All @@ -2051,13 +2057,13 @@ def help(self, request):
elif request in self.symbols: self.showsymbol(request)
elif request in ['True', 'False', 'None']:
# special case these keywords since they are objects too
doc(eval(request), 'Help on %s:')
doc(eval(request), 'Help on %s:', is_cli=is_cli)
elif request in self.keywords: self.showtopic(request)
elif request in self.topics: self.showtopic(request)
elif request: doc(request, 'Help on %s:', output=self._output)
else: doc(str, 'Help on %s:', output=self._output)
elif request: doc(request, 'Help on %s:', output=self._output, is_cli=is_cli)
else: doc(str, 'Help on %s:', output=self._output, is_cli=is_cli)
elif isinstance(request, Helper): self()
else: doc(request, 'Help on %s:', output=self._output)
else: doc(request, 'Help on %s:', output=self._output, is_cli=is_cli)
self.output.write('\n')

def intro(self):
Expand Down Expand Up @@ -2751,7 +2757,6 @@ def cli():
"""Command-line interface (looks at sys.argv to decide what to do)."""
import getopt
class BadUsage(Exception): pass

_adjust_cli_sys_path()

try:
Expand Down Expand Up @@ -2795,7 +2800,7 @@ class BadUsage(Exception): pass
else:
writedoc(arg)
else:
help.help(arg)
help.help(arg, is_cli=True)
except (ImportError, ErrorDuringImport) as value:
print(value)
sys.exit(1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hide traceback in ``help`` prompt, when import failed.

0 comments on commit bdc9e9b

Please sign in to comment.