Skip to content

Commit ba516e7

Browse files
authored
gh-102541: Hide traceback in help prompt (gh-102614)
1 parent 0316063 commit ba516e7

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

Lib/pydoc.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -1780,10 +1780,15 @@ def render_doc(thing, title='Python Library Documentation: %s', forceload=0,
17801780
return title % desc + '\n\n' + renderer.document(object, name)
17811781

17821782
def doc(thing, title='Python Library Documentation: %s', forceload=0,
1783-
output=None):
1783+
output=None, is_cli=False):
17841784
"""Display text documentation, given an object or a path to an object."""
17851785
if output is None:
1786-
pager(render_doc(thing, title, forceload))
1786+
try:
1787+
pager(render_doc(thing, title, forceload))
1788+
except ImportError as exc:
1789+
if is_cli:
1790+
raise
1791+
print(exc)
17871792
else:
17881793
output.write(render_doc(thing, title, forceload, plaintext))
17891794

@@ -2044,7 +2049,7 @@ def getline(self, prompt):
20442049
self.output.flush()
20452050
return self.input.readline()
20462051

2047-
def help(self, request):
2052+
def help(self, request, is_cli=False):
20482053
if isinstance(request, str):
20492054
request = request.strip()
20502055
if request == 'keywords': self.listkeywords()
@@ -2056,13 +2061,13 @@ def help(self, request):
20562061
elif request in self.symbols: self.showsymbol(request)
20572062
elif request in ['True', 'False', 'None']:
20582063
# special case these keywords since they are objects too
2059-
doc(eval(request), 'Help on %s:')
2064+
doc(eval(request), 'Help on %s:', is_cli=is_cli)
20602065
elif request in self.keywords: self.showtopic(request)
20612066
elif request in self.topics: self.showtopic(request)
2062-
elif request: doc(request, 'Help on %s:', output=self._output)
2063-
else: doc(str, 'Help on %s:', output=self._output)
2067+
elif request: doc(request, 'Help on %s:', output=self._output, is_cli=is_cli)
2068+
else: doc(str, 'Help on %s:', output=self._output, is_cli=is_cli)
20642069
elif isinstance(request, Helper): self()
2065-
else: doc(request, 'Help on %s:', output=self._output)
2070+
else: doc(request, 'Help on %s:', output=self._output, is_cli=is_cli)
20662071
self.output.write('\n')
20672072

20682073
def intro(self):
@@ -2800,7 +2805,7 @@ class BadUsage(Exception): pass
28002805
else:
28012806
writedoc(arg)
28022807
else:
2803-
help.help(arg)
2808+
help.help(arg, is_cli=True)
28042809
except (ImportError, ErrorDuringImport) as value:
28052810
print(value)
28062811
sys.exit(1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hide traceback in :func:`help` prompt, when import failed.

0 commit comments

Comments
 (0)