Skip to content

gh-102541: Hide traceback in help prompt #102614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions Lib/pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1780,10 +1780,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 as exc:
if is_cli:
raise
print(exc)
else:
output.write(render_doc(thing, title, forceload, plaintext))

Expand Down Expand Up @@ -2044,7 +2049,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 @@ -2056,13 +2061,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 @@ -2800,7 +2805,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 :func:`help` prompt, when import failed.