Skip to content

Commit abeb589

Browse files
corona10Eclips4
andauthored
[3.11] gh-102541: Hide traceback in help prompt (gh-102614). (gh-105830)
(cherry picked from commit ba516e7) Co-authored-by: Kirill Podoprigora <[email protected]>
1 parent 6be46c3 commit abeb589

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

Lib/pydoc.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -1778,10 +1778,15 @@ def render_doc(thing, title='Python Library Documentation: %s', forceload=0,
17781778
return title % desc + '\n\n' + renderer.document(object, name)
17791779

17801780
def doc(thing, title='Python Library Documentation: %s', forceload=0,
1781-
output=None):
1781+
output=None, is_cli=False):
17821782
"""Display text documentation, given an object or a path to an object."""
17831783
if output is None:
1784-
pager(render_doc(thing, title, forceload))
1784+
try:
1785+
pager(render_doc(thing, title, forceload))
1786+
except ImportError as exc:
1787+
if is_cli:
1788+
raise
1789+
print(exc)
17851790
else:
17861791
output.write(render_doc(thing, title, forceload, plaintext))
17871792

@@ -2042,8 +2047,8 @@ def getline(self, prompt):
20422047
self.output.flush()
20432048
return self.input.readline()
20442049

2045-
def help(self, request):
2046-
if type(request) is type(''):
2050+
def help(self, request, is_cli=False):
2051+
if isinstance(request, str):
20472052
request = request.strip()
20482053
if request == 'keywords': self.listkeywords()
20492054
elif request == 'symbols': self.listsymbols()
@@ -2054,13 +2059,13 @@ def help(self, request):
20542059
elif request in self.symbols: self.showsymbol(request)
20552060
elif request in ['True', 'False', 'None']:
20562061
# special case these keywords since they are objects too
2057-
doc(eval(request), 'Help on %s:')
2062+
doc(eval(request), 'Help on %s:', is_cli=is_cli)
20582063
elif request in self.keywords: self.showtopic(request)
20592064
elif request in self.topics: self.showtopic(request)
2060-
elif request: doc(request, 'Help on %s:', output=self._output)
2061-
else: doc(str, 'Help on %s:', output=self._output)
2065+
elif request: doc(request, 'Help on %s:', output=self._output, is_cli=is_cli)
2066+
else: doc(str, 'Help on %s:', output=self._output, is_cli=is_cli)
20622067
elif isinstance(request, Helper): self()
2063-
else: doc(request, 'Help on %s:', output=self._output)
2068+
else: doc(request, 'Help on %s:', output=self._output, is_cli=is_cli)
20642069
self.output.write('\n')
20652070

20662071
def intro(self):
@@ -2798,7 +2803,7 @@ class BadUsage(Exception): pass
27982803
else:
27992804
writedoc(arg)
28002805
else:
2801-
help.help(arg)
2806+
help.help(arg, is_cli=True)
28022807
except (ImportError, ErrorDuringImport) as value:
28032808
print(value)
28042809
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)